diff --git a/cli/cmd/context/context.go b/cli/cmd/context/context.go index 26edb363f..1d344dca8 100644 --- a/cli/cmd/context/context.go +++ b/cli/cmd/context/context.go @@ -31,12 +31,10 @@ import ( "github.com/spf13/cobra" "github.com/docker/api/cli/cmd/context/login" - - cliopts "github.com/docker/api/cli/options" ) // Command manages contexts -func Command(opts *cliopts.GlobalOpts) *cobra.Command { +func Command() *cobra.Command { cmd := &cobra.Command{ Use: "context", Short: "Manage contexts", @@ -47,7 +45,7 @@ func Command(opts *cliopts.GlobalOpts) *cobra.Command { listCommand(), removeCommand(), showCommand(), - useCommand(opts), + useCommand(), login.Command(), ) diff --git a/cli/cmd/context/use.go b/cli/cmd/context/use.go index 5a0c36001..0d1ce2b14 100644 --- a/cli/cmd/context/use.go +++ b/cli/cmd/context/use.go @@ -34,22 +34,21 @@ import ( "github.com/spf13/cobra" cliconfig "github.com/docker/api/cli/config" - cliopts "github.com/docker/api/cli/options" "github.com/docker/api/context/store" ) -func useCommand(opts *cliopts.GlobalOpts) *cobra.Command { +func useCommand() *cobra.Command { return &cobra.Command{ Use: "use CONTEXT", Short: "Set the default context", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return runUse(cmd.Context(), opts.Config, args[0]) + return runUse(cmd.Context(), args[0]) }, } } -func runUse(ctx context.Context, configDir string, name string) error { +func runUse(ctx context.Context, name string) error { s := store.ContextStore(ctx) // Match behavior of existing CLI if name != store.DefaultContextName { @@ -57,7 +56,7 @@ func runUse(ctx context.Context, configDir string, name string) error { return err } } - if err := cliconfig.WriteCurrentContext(configDir, name); err != nil { + if err := cliconfig.WriteCurrentContext(cliconfig.Dir(ctx), name); err != nil { return err } fmt.Println(name) diff --git a/cli/config/config.go b/cli/config/config.go index f473b4139..49058a7d3 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -28,6 +28,7 @@ package config import ( + "context" "encoding/json" "io/ioutil" "os" @@ -38,6 +39,19 @@ import ( "github.com/docker/api/context/store" ) +type dirKey struct{} + +// WithDir sets the config directory path in the context +func WithDir(ctx context.Context, path string) context.Context { + return context.WithValue(ctx, dirKey{}, path) +} + +// Dir returns the config directory path +func Dir(ctx context.Context) string { + cd, _ := ctx.Value(dirKey{}).(string) + return cd +} + // LoadFile loads the docker configuration func LoadFile(dir string) (*File, error) { f := &File{} diff --git a/cli/main.go b/cli/main.go index 7bff9a07a..110b80290 100644 --- a/cli/main.go +++ b/cli/main.go @@ -101,7 +101,7 @@ func main() { } root.AddCommand( - contextcmd.Command(&opts), + contextcmd.Command(), cmd.PsCommand(), cmd.ServeCommand(), run.Command(), @@ -136,13 +136,15 @@ func main() { if opts.Config == "" { fatal(errors.New("config path cannot be empty")) } + configDir := opts.Config + ctx = cliconfig.WithDir(ctx, configDir) - currentContext, err := determineCurrentContext(opts.Context, opts.Config) + currentContext, err := determineCurrentContext(opts.Context, configDir) if err != nil { fatal(errors.New("unable to determine current context")) } - s, err := store.New(store.WithRoot(opts.Config)) + s, err := store.New(store.WithRoot(configDir)) if err != nil { fatal(errors.Wrap(err, "unable to create context store")) }