瀏覽代碼

Store config dir in CLI context

Signed-off-by: Christopher Crone <[email protected]>
Christopher Crone 5 年之前
父節點
當前提交
058e6203a7
共有 4 個文件被更改,包括 25 次插入12 次删除
  1. 2 4
      cli/cmd/context/context.go
  2. 4 5
      cli/cmd/context/use.go
  3. 14 0
      cli/config/config.go
  4. 5 3
      cli/main.go

+ 2 - 4
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(),
 	)
 

+ 4 - 5
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)

+ 14 - 0
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{}

+ 5 - 3
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"))
 	}