2014-03-28 23:21:55 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
flag "github.com/dotcloud/docker/pkg/mflag"
|
|
|
|
"github.com/dotcloud/docker/pkg/term"
|
|
|
|
"github.com/dotcloud/docker/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
|
2014-05-19 21:18:53 +00:00
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2014-03-28 23:21:55 +00:00
|
|
|
methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
|
|
|
|
method := reflect.ValueOf(cli).MethodByName(methodName)
|
|
|
|
if !method.IsValid() {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return method.Interface().(func(...string) error), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) ParseCommands(args ...string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
method, exists := cli.getMethod(args[0])
|
|
|
|
if !exists {
|
|
|
|
fmt.Println("Error: Command not found:", args[0])
|
|
|
|
return cli.CmdHelp(args[1:]...)
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
fmt.Fprintf(cli.err, "\nUsage: docker %s %s\n\n%s\n\n", name, signature, description)
|
|
|
|
flags.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
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 19:49:12 +00:00
|
|
|
scheme = "http"
|
2014-03-28 23:21:55 +00:00
|
|
|
)
|
|
|
|
|
2014-05-02 19:49:12 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
2014-03-28 23:21:55 +00:00
|
|
|
if in != nil {
|
2014-05-21 21:11:57 +00:00
|
|
|
if file, ok := out.(*os.File); ok {
|
2014-03-28 23:21:55 +00: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 19:49:12 +00:00
|
|
|
scheme: scheme,
|
2014-03-28 23:21:55 +00: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
|
2014-05-02 19:49:12 +00:00
|
|
|
scheme string
|
2014-03-28 23:21:55 +00:00
|
|
|
}
|