Store config dir in CLI context

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2020-05-20 15:55:05 +02:00
parent b7fb7e2e84
commit 058e6203a7
4 changed files with 25 additions and 12 deletions

View File

@ -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(),
)

View File

@ -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)

View File

@ -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{}

View File

@ -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"))
}