Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-03-05 14:29:42 +01:00
parent f0fd18eec9
commit 6458b97089
3 changed files with 19 additions and 30 deletions

View File

@ -21,13 +21,11 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
configfile "github.com/docker/cli/cli/config/configfile"
cliflags "github.com/docker/cli/cli/flags" cliflags "github.com/docker/cli/cli/flags"
) )
type currentContextKey struct{} type currentContextKey struct{}
type cliOptionsKey struct{} type cliOptionsKey struct{}
type configKey struct{}
// WithCurrentContext sets the name of the current docker context // WithCurrentContext sets the name of the current docker context
func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context { func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context {
@ -50,14 +48,3 @@ func CliOptions(ctx context.Context) cliflags.CommonOptions {
cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions) cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions)
return cc 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
}

View File

@ -195,10 +195,6 @@ func main() {
ctx = config.WithDir(ctx, configDir) ctx = config.WithDir(ctx, configDir)
currentContext := determineCurrentContext(opts.Context, configDir) currentContext := determineCurrentContext(opts.Context, configDir)
if len(opts.Hosts) > 0 {
opts.Context = ""
currentContext = "default"
}
s, err := store.New(configDir) s, err := store.New(configDir)
if err != nil { if err != nil {
@ -216,6 +212,11 @@ func main() {
compose.Command(ctype), compose.Command(ctype),
volume.Command(ctype), volume.Command(ctype),
) )
if ctype == store.LocalContextType {
if len(opts.Hosts) > 0 {
opts.Context = ""
currentContext = "default"
}
cnxOptions := cliflags.CommonOptions{ cnxOptions := cliflags.CommonOptions{
Context: opts.Context, Context: opts.Context,
@ -230,6 +231,7 @@ func main() {
cnxOptions.TLSOptions = opts.TLSOptions cnxOptions.TLSOptions = opts.TLSOptions
} }
ctx = apicontext.WithCliOptions(ctx, cnxOptions) ctx = apicontext.WithCliOptions(ctx, cnxOptions)
}
ctx = apicontext.WithCurrentContext(ctx, currentContext) ctx = apicontext.WithCurrentContext(ctx, currentContext)
ctx = store.WithContextStore(ctx, s) ctx = store.WithContextStore(ctx, s)

View File

@ -396,15 +396,15 @@ func TestLegacy(t *testing.T) {
}) })
t.Run("host flag", func(t *testing.T) { t.Run("host flag", func(t *testing.T) {
stderr := "nonexistent: Name or service not known" stderr := "dial tcp: lookup nonexistent"
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
stderr = "error during connect: Get http://nonexitent:123" stderr = "error during connect: Get http://nonexitent:123"
} }
res := c.RunDockerOrExitError("-H", "tcp://nonexistent:123", "version") res := c.RunDockerOrExitError("-H", "tcp://nonexistent:123", "version")
res.Assert(t, icmd.Expected{ res.Assert(t, icmd.Expected{
ExitCode: 1, ExitCode: 1,
Err: stderr,
}) })
assert.Assert(t, strings.Contains(res.Stderr(), stderr), res.Stderr())
}) })
t.Run("existing contexts delegate", func(t *testing.T) { t.Run("existing contexts delegate", func(t *testing.T) {