diff --git a/api/context/context.go b/api/context/context.go index b1138c31..6d03e159 100644 --- a/api/context/context.go +++ b/api/context/context.go @@ -20,9 +20,14 @@ import ( gocontext "context" "golang.org/x/net/context" + + configfile "github.com/docker/cli/cli/config/configfile" + cliflags "github.com/docker/cli/cli/flags" ) type currentContextKey struct{} +type cliOptionsKey struct{} +type configKey struct{} // WithCurrentContext sets the name of the current docker context func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context { @@ -34,3 +39,25 @@ func CurrentContext(ctx context.Context) string { cc, _ := ctx.Value(currentContextKey{}).(string) return cc } + +// WithCliOptions sets CLI options +func WithCliOptions(ctx gocontext.Context, options cliflags.CommonOptions) context.Context { + return context.WithValue(ctx, cliOptionsKey{}, options) +} + +// CliOptions returns the current context name +func CliOptions(ctx context.Context) cliflags.CommonOptions { + cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions) + return cc +} + +// WithConfig sets docker config +func WithConfig(ctx gocontext.Context, config configfile.ConfigFile) context.Context { + return context.WithValue(ctx, configKey{}, config) +} + +// Config returns the docker config +func Config(ctx context.Context) configfile.ConfigFile { + cc, _ := ctx.Value(cliOptionsKey{}).(configfile.ConfigFile) + return cc +} diff --git a/cli/main.go b/cli/main.go index eb59c005..f4c52b2d 100644 --- a/cli/main.go +++ b/cli/main.go @@ -47,6 +47,9 @@ import ( "github.com/docker/compose-cli/cli/mobycli" cliopts "github.com/docker/compose-cli/cli/options" + cliconfig "github.com/docker/cli/cli/config" + cliflags "github.com/docker/cli/cli/flags" + // Backend registrations _ "github.com/docker/compose-cli/aci" _ "github.com/docker/compose-cli/ecs" @@ -151,10 +154,7 @@ func main() { }) flags := root.Flags() - flags.StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")") - flags.BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs") - flags.StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to") - opts.AddContextFlags(flags) + opts.InstallFlags(flags) opts.AddConfigFlags(flags) flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit") @@ -184,8 +184,8 @@ func main() { ctx, cancel := newSigContext() defer cancel() - // --host and --version should immediately be forwarded to the original cli - if opts.Host != "" || opts.Version { + // --version should immediately be forwarded to the original cli + if opts.Version { mobycli.Exec(root) } @@ -214,7 +214,29 @@ func main() { volume.Command(ctype), ) + configFile, err := cliconfig.Load(opts.Config) + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to load docker config: %s\n", opts.Config) + os.Exit(1) + } + ctx = apicontext.WithConfig(ctx, *configFile) ctx = apicontext.WithCurrentContext(ctx, currentContext) + + cnxOptions := cliflags.CommonOptions{ + Context: opts.Context, + Debug: opts.Debug, + Hosts: opts.Hosts, + LogLevel: opts.LogLevel, + TLS: opts.TLS, + TLSVerify: opts.TLSVerify, + } + if len(opts.Hosts) == 0 { + cnxOptions.Context = currentContext + } + if opts.TLSVerify { + cnxOptions.TLSOptions = opts.TLSOptions + } + ctx = apicontext.WithCliOptions(ctx, cnxOptions) ctx = store.WithContextStore(ctx, s) if err = root.ExecuteContext(ctx); err != nil { diff --git a/cli/options/options.go b/cli/options/options.go index 764c9cb5..c65e62ae 100644 --- a/cli/options/options.go +++ b/cli/options/options.go @@ -17,16 +17,14 @@ package options import ( - apicontext "github.com/docker/compose-cli/api/context" cliconfig "github.com/docker/compose-cli/cli/config" + + cliflags "github.com/docker/cli/cli/flags" ) // GlobalOpts contains the global CLI options type GlobalOpts struct { - apicontext.ContextFlags cliconfig.ConfigFlags - Debug bool - LogLevel string - Version bool - Host string + cliflags.CommonOptions + Version bool } diff --git a/local/backend.go b/local/backend.go index 0e5ea277..5e2f042f 100644 --- a/local/backend.go +++ b/local/backend.go @@ -19,12 +19,12 @@ package local import ( "context" - "github.com/docker/docker/client" - + "github.com/docker/cli/cli/command" "github.com/docker/compose-cli/api/backend" "github.com/docker/compose-cli/api/cloud" "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/containers" + apicontext "github.com/docker/compose-cli/api/context" "github.com/docker/compose-cli/api/resources" "github.com/docker/compose-cli/api/secrets" "github.com/docker/compose-cli/api/volumes" @@ -42,7 +42,10 @@ func init() { } func service(ctx context.Context) (backend.Service, error) { - apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + options := apicontext.CliOptions(ctx) + config := apicontext.Config(ctx) + + apiClient, err := command.NewAPIClientFromFlags(&options, &config) if err != nil { return nil, err } diff --git a/local/containers.go b/local/containers.go index e3e72d25..f2b28738 100644 --- a/local/containers.go +++ b/local/containers.go @@ -39,10 +39,11 @@ import ( ) type containerService struct { - apiClient *client.Client + apiClient client.APIClient } func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) { + c, err := cs.apiClient.ContainerInspect(ctx, id) if err != nil { return containers.Container{}, err diff --git a/local/volumes.go b/local/volumes.go index 2fbf08b7..9b49aaf4 100644 --- a/local/volumes.go +++ b/local/volumes.go @@ -29,7 +29,7 @@ import ( ) type volumeService struct { - apiClient *client.Client + apiClient client.APIClient } func (vs *volumeService) List(ctx context.Context) ([]volumes.Volume, error) {