|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "reflect" |
| 10 | + "strings" |
| 11 | + "text/template" |
| 12 | + |
| 13 | + flag "github.com/docker/docker/pkg/mflag" |
| 14 | + "github.com/docker/docker/pkg/term" |
| 15 | + "github.com/docker/docker/registry" |
| 16 | + "github.com/docker/libtrust" |
| 17 | +) |
| 18 | + |
| 19 | +type DockerCli struct { |
| 20 | + proto string |
| 21 | + addr string |
| 22 | + configFile *registry.ConfigFile |
| 23 | + in io.ReadCloser |
| 24 | + out io.Writer |
| 25 | + err io.Writer |
| 26 | + key libtrust.PrivateKey |
| 27 | + tlsConfig *tls.Config |
| 28 | + scheme string |
| 29 | + // inFd holds file descriptor of the client's STDIN, if it's a valid file |
| 30 | + inFd uintptr |
| 31 | + // outFd holds file descriptor of the client's STDOUT, if it's a valid file |
| 32 | + outFd uintptr |
| 33 | + // isTerminalIn describes if client's STDIN is a TTY |
| 34 | + isTerminalIn bool |
| 35 | + // isTerminalOut describes if client's STDOUT is a TTY |
| 36 | + isTerminalOut bool |
| 37 | +} |
| 38 | + |
| 39 | +var funcMap = template.FuncMap{ |
| 40 | + "json": func(v interface{}) string { |
| 41 | + a, _ := json.Marshal(v) |
| 42 | + return string(a) |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) { |
| 47 | + camelArgs := make([]string, len(args)) |
| 48 | + for i, s := range args { |
| 49 | + if len(s) == 0 { |
| 50 | + return nil, false |
| 51 | + } |
| 52 | + camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:]) |
| 53 | + } |
| 54 | + methodName := "Cmd" + strings.Join(camelArgs, "") |
| 55 | + method := reflect.ValueOf(cli).MethodByName(methodName) |
| 56 | + if !method.IsValid() { |
| 57 | + return nil, false |
| 58 | + } |
| 59 | + return method.Interface().(func(...string) error), true |
| 60 | +} |
| 61 | + |
| 62 | +// Cmd executes the specified command |
| 63 | +func (cli *DockerCli) Cmd(args ...string) error { |
| 64 | + if len(args) > 1 { |
| 65 | + method, exists := cli.getMethod(args[:2]...) |
| 66 | + if exists { |
| 67 | + return method(args[2:]...) |
| 68 | + } |
| 69 | + } |
| 70 | + if len(args) > 0 { |
| 71 | + method, exists := cli.getMethod(args[0]) |
| 72 | + if !exists { |
| 73 | + fmt.Println("Error: Command not found:", args[0]) |
| 74 | + return cli.CmdHelp(args[1:]...) |
| 75 | + } |
| 76 | + return method(args[1:]...) |
| 77 | + } |
| 78 | + return cli.CmdHelp(args...) |
| 79 | +} |
| 80 | + |
| 81 | +func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet { |
| 82 | + flags := flag.NewFlagSet(name, flag.ContinueOnError) |
| 83 | + flags.Usage = func() { |
| 84 | + options := "" |
| 85 | + if flags.FlagCountUndeprecated() > 0 { |
| 86 | + options = "[OPTIONS] " |
| 87 | + } |
| 88 | + fmt.Fprintf(cli.err, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description) |
| 89 | + flags.PrintDefaults() |
| 90 | + os.Exit(2) |
| 91 | + } |
| 92 | + return flags |
| 93 | +} |
| 94 | + |
| 95 | +func (cli *DockerCli) LoadConfigFile() (err error) { |
| 96 | + cli.configFile, err = registry.LoadConfig(os.Getenv("HOME")) |
| 97 | + if err != nil { |
| 98 | + fmt.Fprintf(cli.err, "WARNING: %s\n", err) |
| 99 | + } |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +func NewDockerCli(in io.ReadCloser, out, err io.Writer, key libtrust.PrivateKey, proto, addr string, tlsConfig *tls.Config) *DockerCli { |
| 104 | + var ( |
| 105 | + inFd uintptr |
| 106 | + outFd uintptr |
| 107 | + isTerminalIn = false |
| 108 | + isTerminalOut = false |
| 109 | + scheme = "http" |
| 110 | + ) |
| 111 | + |
| 112 | + if tlsConfig != nil { |
| 113 | + scheme = "https" |
| 114 | + } |
| 115 | + |
| 116 | + if in != nil { |
| 117 | + if file, ok := in.(*os.File); ok { |
| 118 | + inFd = file.Fd() |
| 119 | + isTerminalIn = term.IsTerminal(inFd) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if out != nil { |
| 124 | + if file, ok := out.(*os.File); ok { |
| 125 | + outFd = file.Fd() |
| 126 | + isTerminalOut = term.IsTerminal(outFd) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if err == nil { |
| 131 | + err = out |
| 132 | + } |
| 133 | + |
| 134 | + return &DockerCli{ |
| 135 | + proto: proto, |
| 136 | + addr: addr, |
| 137 | + in: in, |
| 138 | + out: out, |
| 139 | + err: err, |
| 140 | + key: key, |
| 141 | + inFd: inFd, |
| 142 | + outFd: outFd, |
| 143 | + isTerminalIn: isTerminalIn, |
| 144 | + isTerminalOut: isTerminalOut, |
| 145 | + tlsConfig: tlsConfig, |
| 146 | + scheme: scheme, |
| 147 | + } |
| 148 | +} |
0 commit comments