| ... | ... |
@@ -4,12 +4,8 @@ import ( |
| 4 | 4 |
"../future" |
| 5 | 5 |
"../rcli" |
| 6 | 6 |
"io" |
| 7 |
- "io/ioutil" |
|
| 8 | 7 |
"log" |
| 9 | 8 |
"os" |
| 10 |
- "os/exec" |
|
| 11 |
- "path" |
|
| 12 |
- "path/filepath" |
|
| 13 | 9 |
) |
| 14 | 10 |
|
| 15 | 11 |
// Run docker in "simple mode": run a single command and return. |
| ... | ... |
@@ -55,72 +51,3 @@ func SimpleMode(args []string) error {
|
| 55 | 55 |
} |
| 56 | 56 |
return nil |
| 57 | 57 |
} |
| 58 |
- |
|
| 59 |
-// Run docker in "interactive mode": run a bash-compatible shell capable of running docker commands. |
|
| 60 |
-func InteractiveMode(scripts ...string) error {
|
|
| 61 |
- // Determine path of current docker binary |
|
| 62 |
- dockerPath, err := exec.LookPath(os.Args[0]) |
|
| 63 |
- if err != nil {
|
|
| 64 |
- return err |
|
| 65 |
- } |
|
| 66 |
- dockerPath, err = filepath.Abs(dockerPath) |
|
| 67 |
- if err != nil {
|
|
| 68 |
- return err |
|
| 69 |
- } |
|
| 70 |
- |
|
| 71 |
- // Create a temp directory |
|
| 72 |
- tmp, err := ioutil.TempDir("", "docker-shell")
|
|
| 73 |
- if err != nil {
|
|
| 74 |
- return err |
|
| 75 |
- } |
|
| 76 |
- defer os.RemoveAll(tmp) |
|
| 77 |
- |
|
| 78 |
- // For each command, create an alias in temp directory |
|
| 79 |
- // FIXME: generate this list dynamically with introspection of some sort |
|
| 80 |
- // It might make sense to merge docker and dockerd to keep that introspection |
|
| 81 |
- // within a single binary. |
|
| 82 |
- for _, cmd := range []string{
|
|
| 83 |
- "help", |
|
| 84 |
- "run", |
|
| 85 |
- "ps", |
|
| 86 |
- "pull", |
|
| 87 |
- "put", |
|
| 88 |
- "rm", |
|
| 89 |
- "kill", |
|
| 90 |
- "wait", |
|
| 91 |
- "stop", |
|
| 92 |
- "start", |
|
| 93 |
- "restart", |
|
| 94 |
- "logs", |
|
| 95 |
- "diff", |
|
| 96 |
- "commit", |
|
| 97 |
- "attach", |
|
| 98 |
- "info", |
|
| 99 |
- "tar", |
|
| 100 |
- "web", |
|
| 101 |
- "images", |
|
| 102 |
- "docker", |
|
| 103 |
- } {
|
|
| 104 |
- if err := os.Symlink(dockerPath, path.Join(tmp, cmd)); err != nil {
|
|
| 105 |
- return err |
|
| 106 |
- } |
|
| 107 |
- } |
|
| 108 |
- |
|
| 109 |
- // Run $SHELL with PATH set to temp directory |
|
| 110 |
- rcfile, err := ioutil.TempFile("", "docker-shell-rc")
|
|
| 111 |
- if err != nil {
|
|
| 112 |
- return err |
|
| 113 |
- } |
|
| 114 |
- defer os.Remove(rcfile.Name()) |
|
| 115 |
- io.WriteString(rcfile, "enable -n help\n") |
|
| 116 |
- os.Setenv("PATH", tmp+":"+os.Getenv("PATH"))
|
|
| 117 |
- os.Setenv("PS1", "\\h docker> ")
|
|
| 118 |
- shell := exec.Command("/bin/bash", append([]string{"--rcfile", rcfile.Name()}, scripts...)...)
|
|
| 119 |
- shell.Stdin = os.Stdin |
|
| 120 |
- shell.Stdout = os.Stdout |
|
| 121 |
- shell.Stderr = os.Stderr |
|
| 122 |
- if err := shell.Run(); err != nil {
|
|
| 123 |
- return err |
|
| 124 |
- } |
|
| 125 |
- return nil |
|
| 126 |
-} |
| ... | ... |
@@ -2,28 +2,12 @@ package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"../client" |
| 5 |
- "flag" |
|
| 6 | 5 |
"log" |
| 7 | 6 |
"os" |
| 8 |
- "path" |
|
| 9 | 7 |
) |
| 10 | 8 |
|
| 11 | 9 |
func main() {
|
| 12 |
- if cmd := path.Base(os.Args[0]); cmd == "docker" {
|
|
| 13 |
- fl_shell := flag.Bool("i", false, "Interactive mode")
|
|
| 14 |
- flag.Parse() |
|
| 15 |
- if *fl_shell {
|
|
| 16 |
- if err := client.InteractiveMode(flag.Args()...); err != nil {
|
|
| 17 |
- log.Fatal(err) |
|
| 18 |
- } |
|
| 19 |
- } else {
|
|
| 20 |
- if err := client.SimpleMode(os.Args[1:]); err != nil {
|
|
| 21 |
- log.Fatal(err) |
|
| 22 |
- } |
|
| 23 |
- } |
|
| 24 |
- } else {
|
|
| 25 |
- if err := client.SimpleMode(append([]string{cmd}, os.Args[1:]...)); err != nil {
|
|
| 26 |
- log.Fatal(err) |
|
| 27 |
- } |
|
| 10 |
+ if err := client.SimpleMode(os.Args[1:]); err != nil {
|
|
| 11 |
+ log.Fatal(err) |
|
| 28 | 12 |
} |
| 29 | 13 |
} |