2014-03-28 19:21:55 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/docker/registry"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2014-08-10 00:31:59 -04:00
|
|
|
type DockerCli struct {
|
|
|
|
proto string
|
|
|
|
addr string
|
|
|
|
configFile *registry.ConfigFile
|
|
|
|
in io.ReadCloser
|
|
|
|
out io.Writer
|
|
|
|
err io.Writer
|
|
|
|
isTerminal bool
|
|
|
|
terminalFd uintptr
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
scheme string
|
|
|
|
}
|
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:44:15 -04:00
|
|
|
func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) {
|
|
|
|
camelArgs := make([]string, len(args))
|
|
|
|
for i, s := range args {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
|
2014-05-19 17:18:53 -04:00
|
|
|
}
|
2014-09-16 13:44:15 -04:00
|
|
|
methodName := "Cmd" + strings.Join(camelArgs, "")
|
2014-03-28 19:21:55 -04:00
|
|
|
method := reflect.ValueOf(cli).MethodByName(methodName)
|
|
|
|
if !method.IsValid() {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return method.Interface().(func(...string) error), true
|
|
|
|
}
|
|
|
|
|
2014-08-10 00:33:19 -04:00
|
|
|
// Cmd executes the specified command
|
|
|
|
func (cli *DockerCli) Cmd(args ...string) error {
|
2014-09-16 13:44:15 -04:00
|
|
|
if len(args) > 1 {
|
|
|
|
method, exists := cli.getMethod(args[:2]...)
|
|
|
|
if exists {
|
|
|
|
return method(args[2:]...)
|
|
|
|
}
|
|
|
|
}
|
2014-03-28 19:21:55 -04:00
|
|
|
if len(args) > 0 {
|
|
|
|
method, exists := cli.getMethod(args[0])
|
|
|
|
if !exists {
|
2014-08-27 14:59:13 -04:00
|
|
|
fmt.Println("Error: Command not found:", args[0])
|
|
|
|
return cli.CmdHelp(args[1:]...)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
return method(args[1:]...)
|
|
|
|
}
|
|
|
|
return cli.CmdHelp(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet {
|
|
|
|
flags := flag.NewFlagSet(name, flag.ContinueOnError)
|
|
|
|
flags.Usage = func() {
|
2014-08-24 21:53:31 -04:00
|
|
|
options := ""
|
|
|
|
if flags.FlagCountUndeprecated() > 0 {
|
|
|
|
options = "[OPTIONS] "
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.err, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description)
|
2014-03-28 19:21:55 -04:00
|
|
|
flags.PrintDefaults()
|
2014-08-27 14:59:13 -04:00
|
|
|
os.Exit(2)
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) LoadConfigFile() (err error) {
|
|
|
|
cli.configFile, err = registry.LoadConfig(os.Getenv("HOME"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(cli.err, "WARNING: %s\n", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string, tlsConfig *tls.Config) *DockerCli {
|
|
|
|
var (
|
|
|
|
isTerminal = false
|
|
|
|
terminalFd uintptr
|
2014-05-02 15:49:12 -04:00
|
|
|
scheme = "http"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2014-05-02 15:49:12 -04:00
|
|
|
if tlsConfig != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
2014-03-28 19:21:55 -04:00
|
|
|
if in != nil {
|
2014-05-21 17:11:57 -04:00
|
|
|
if file, ok := out.(*os.File); ok {
|
2014-03-28 19:21:55 -04:00
|
|
|
terminalFd = file.Fd()
|
|
|
|
isTerminal = term.IsTerminal(terminalFd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = out
|
|
|
|
}
|
|
|
|
return &DockerCli{
|
|
|
|
proto: proto,
|
|
|
|
addr: addr,
|
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
err: err,
|
|
|
|
isTerminal: isTerminal,
|
|
|
|
terminalFd: terminalFd,
|
|
|
|
tlsConfig: tlsConfig,
|
2014-05-02 15:49:12 -04:00
|
|
|
scheme: scheme,
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
}
|