Add connection flags to root command

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-03-02 15:21:54 +01:00
parent ceefdfabec
commit d26783c322
6 changed files with 68 additions and 17 deletions

View File

@ -20,9 +20,14 @@ import (
gocontext "context"
"golang.org/x/net/context"
configfile "github.com/docker/cli/cli/config/configfile"
cliflags "github.com/docker/cli/cli/flags"
)
type currentContextKey struct{}
type cliOptionsKey struct{}
type configKey struct{}
// WithCurrentContext sets the name of the current docker context
func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context {
@ -34,3 +39,25 @@ func CurrentContext(ctx context.Context) string {
cc, _ := ctx.Value(currentContextKey{}).(string)
return cc
}
// WithCliOptions sets CLI options
func WithCliOptions(ctx gocontext.Context, options cliflags.CommonOptions) context.Context {
return context.WithValue(ctx, cliOptionsKey{}, options)
}
// CliOptions returns the current context name
func CliOptions(ctx context.Context) cliflags.CommonOptions {
cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions)
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

@ -47,6 +47,9 @@ import (
"github.com/docker/compose-cli/cli/mobycli"
cliopts "github.com/docker/compose-cli/cli/options"
cliconfig "github.com/docker/cli/cli/config"
cliflags "github.com/docker/cli/cli/flags"
// Backend registrations
_ "github.com/docker/compose-cli/aci"
_ "github.com/docker/compose-cli/ecs"
@ -151,10 +154,7 @@ func main() {
})
flags := root.Flags()
flags.StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")")
flags.BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
flags.StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
opts.AddContextFlags(flags)
opts.InstallFlags(flags)
opts.AddConfigFlags(flags)
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
@ -184,8 +184,8 @@ func main() {
ctx, cancel := newSigContext()
defer cancel()
// --host and --version should immediately be forwarded to the original cli
if opts.Host != "" || opts.Version {
// --version should immediately be forwarded to the original cli
if opts.Version {
mobycli.Exec(root)
}
@ -214,7 +214,29 @@ func main() {
volume.Command(ctype),
)
configFile, err := cliconfig.Load(opts.Config)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to load docker config: %s\n", opts.Config)
os.Exit(1)
}
ctx = apicontext.WithConfig(ctx, *configFile)
ctx = apicontext.WithCurrentContext(ctx, currentContext)
cnxOptions := cliflags.CommonOptions{
Context: opts.Context,
Debug: opts.Debug,
Hosts: opts.Hosts,
LogLevel: opts.LogLevel,
TLS: opts.TLS,
TLSVerify: opts.TLSVerify,
}
if len(opts.Hosts) == 0 {
cnxOptions.Context = currentContext
}
if opts.TLSVerify {
cnxOptions.TLSOptions = opts.TLSOptions
}
ctx = apicontext.WithCliOptions(ctx, cnxOptions)
ctx = store.WithContextStore(ctx, s)
if err = root.ExecuteContext(ctx); err != nil {

View File

@ -17,16 +17,14 @@
package options
import (
apicontext "github.com/docker/compose-cli/api/context"
cliconfig "github.com/docker/compose-cli/cli/config"
cliflags "github.com/docker/cli/cli/flags"
)
// GlobalOpts contains the global CLI options
type GlobalOpts struct {
apicontext.ContextFlags
cliconfig.ConfigFlags
Debug bool
LogLevel string
Version bool
Host string
cliflags.CommonOptions
Version bool
}

View File

@ -19,12 +19,12 @@ package local
import (
"context"
"github.com/docker/docker/client"
"github.com/docker/cli/cli/command"
"github.com/docker/compose-cli/api/backend"
"github.com/docker/compose-cli/api/cloud"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/containers"
apicontext "github.com/docker/compose-cli/api/context"
"github.com/docker/compose-cli/api/resources"
"github.com/docker/compose-cli/api/secrets"
"github.com/docker/compose-cli/api/volumes"
@ -42,7 +42,10 @@ func init() {
}
func service(ctx context.Context) (backend.Service, error) {
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
options := apicontext.CliOptions(ctx)
config := apicontext.Config(ctx)
apiClient, err := command.NewAPIClientFromFlags(&options, &config)
if err != nil {
return nil, err
}

View File

@ -39,10 +39,11 @@ import (
)
type containerService struct {
apiClient *client.Client
apiClient client.APIClient
}
func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
c, err := cs.apiClient.ContainerInspect(ctx, id)
if err != nil {
return containers.Container{}, err

View File

@ -29,7 +29,7 @@ import (
)
type volumeService struct {
apiClient *client.Client
apiClient client.APIClient
}
func (vs *volumeService) List(ctx context.Context) ([]volumes.Volume, error) {