浏览代码

Move the config into own package and outside of cli

Djordje Lukic 5 年之前
父节点
当前提交
22d4d250a9
共有 7 个文件被更改,包括 20 次插入20 次删除
  1. 2 2
      cli/cmd/context/use.go
  2. 3 10
      cli/config/flags.go
  3. 5 7
      cli/main.go
  4. 1 1
      cli/main_test.go
  5. 0 0
      config/config.go
  6. 0 0
      config/config_test.go
  7. 9 0
      config/keys.go

+ 2 - 2
cli/cmd/context/use.go

@@ -33,7 +33,7 @@ import (
 
 	"github.com/spf13/cobra"
 
-	cliconfig "github.com/docker/api/cli/config"
+	"github.com/docker/api/config"
 	"github.com/docker/api/context/store"
 )
 
@@ -56,7 +56,7 @@ func runUse(ctx context.Context, name string) error {
 			return err
 		}
 	}
-	if err := cliconfig.WriteCurrentContext(cliconfig.Dir(ctx), name); err != nil {
+	if err := config.WriteCurrentContext(config.Dir(ctx), name); err != nil {
 		return err
 	}
 	fmt.Println(name)

+ 3 - 10
cli/config/flags.go

@@ -32,15 +32,8 @@ import (
 	"path/filepath"
 
 	"github.com/spf13/pflag"
-)
 
-const (
-	// ConfigFileName is the name of config file
-	ConfigFileName = "config.json"
-	// ConfigFileDir is the default folder where the config file is stored
-	ConfigFileDir = ".docker"
-	// ConfigFlagName is the name of the config flag
-	ConfigFlagName = "config"
+	"github.com/docker/api/config"
 )
 
 // ConfigFlags are the global CLI flags
@@ -51,7 +44,7 @@ type ConfigFlags struct {
 
 // AddConfigFlags adds persistent (global) flags
 func (c *ConfigFlags) AddConfigFlags(flags *pflag.FlagSet) {
-	flags.StringVar(&c.Config, ConfigFlagName, confDir(), "Location of the client config files `DIRECTORY`")
+	flags.StringVar(&c.Config, config.ConfigFlagName, confDir(), "Location of the client config files `DIRECTORY`")
 }
 
 func confDir() string {
@@ -60,5 +53,5 @@ func confDir() string {
 		return env
 	}
 	home, _ := os.UserHomeDir()
-	return filepath.Join(home, ConfigFileDir)
+	return filepath.Join(home, config.ConfigFileDir)
 }

+ 5 - 7
cli/main.go

@@ -38,10 +38,6 @@ import (
 	"syscall"
 	"time"
 
-	"github.com/docker/api/cli/cmd/login"
-
-	"github.com/docker/api/cli/dockerclassic"
-
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -54,9 +50,11 @@ import (
 	"github.com/docker/api/cli/cmd"
 	"github.com/docker/api/cli/cmd/compose"
 	contextcmd "github.com/docker/api/cli/cmd/context"
+	"github.com/docker/api/cli/cmd/login"
 	"github.com/docker/api/cli/cmd/run"
-	cliconfig "github.com/docker/api/cli/config"
+	"github.com/docker/api/cli/dockerclassic"
 	cliopts "github.com/docker/api/cli/options"
+	"github.com/docker/api/config"
 	apicontext "github.com/docker/api/context"
 	"github.com/docker/api/context/store"
 )
@@ -151,7 +149,7 @@ func main() {
 		fatal(errors.New("config path cannot be empty"))
 	}
 	configDir := opts.Config
-	ctx = cliconfig.WithDir(ctx, configDir)
+	ctx = config.WithDir(ctx, configDir)
 
 	currentContext, err := determineCurrentContext(opts.Context, configDir)
 	if err != nil {
@@ -206,7 +204,7 @@ func newSigContext() (context.Context, func()) {
 func determineCurrentContext(flag string, configDir string) (string, error) {
 	res := flag
 	if res == "" {
-		config, err := cliconfig.LoadFile(configDir)
+		config, err := config.LoadFile(configDir)
 		if err != nil {
 			return "", err
 		}

+ 1 - 1
cli/main_test.go

@@ -40,7 +40,7 @@ import (
 
 	"github.com/stretchr/testify/require"
 
-	"github.com/docker/api/cli/config"
+	"github.com/docker/api/config"
 )
 
 var contextSetConfig = []byte(`{

+ 0 - 0
cli/config/config.go → config/config.go


+ 0 - 0
cli/config/config_test.go → config/config_test.go


+ 9 - 0
cli/config/keys.go → config/keys.go

@@ -27,6 +27,15 @@
 
 package config
 
+const (
+	// ConfigFileName is the name of config file
+	ConfigFileName = "config.json"
+	// ConfigFileDir is the default folder where the config file is stored
+	ConfigFileDir = ".docker"
+	// ConfigFlagName is the name of the config flag
+	ConfigFlagName = "config"
+)
+
 const (
 	// currentContextKey is the key used in the Docker config file to set the
 	// default context