mirror of
https://github.com/docker/compose.git
synced 2025-07-31 01:24:15 +02:00
Add connection flags to root command
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
parent
ceefdfabec
commit
d26783c322
@ -20,9 +20,14 @@ import (
|
|||||||
gocontext "context"
|
gocontext "context"
|
||||||
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
type currentContextKey struct{}
|
type currentContextKey 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 {
|
||||||
@ -34,3 +39,25 @@ func CurrentContext(ctx context.Context) string {
|
|||||||
cc, _ := ctx.Value(currentContextKey{}).(string)
|
cc, _ := ctx.Value(currentContextKey{}).(string)
|
||||||
return cc
|
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
|
||||||
|
}
|
||||||
|
34
cli/main.go
34
cli/main.go
@ -47,6 +47,9 @@ import (
|
|||||||
"github.com/docker/compose-cli/cli/mobycli"
|
"github.com/docker/compose-cli/cli/mobycli"
|
||||||
cliopts "github.com/docker/compose-cli/cli/options"
|
cliopts "github.com/docker/compose-cli/cli/options"
|
||||||
|
|
||||||
|
cliconfig "github.com/docker/cli/cli/config"
|
||||||
|
cliflags "github.com/docker/cli/cli/flags"
|
||||||
|
|
||||||
// Backend registrations
|
// Backend registrations
|
||||||
_ "github.com/docker/compose-cli/aci"
|
_ "github.com/docker/compose-cli/aci"
|
||||||
_ "github.com/docker/compose-cli/ecs"
|
_ "github.com/docker/compose-cli/ecs"
|
||||||
@ -151,10 +154,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
flags := root.Flags()
|
flags := root.Flags()
|
||||||
flags.StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")")
|
opts.InstallFlags(flags)
|
||||||
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.AddConfigFlags(flags)
|
opts.AddConfigFlags(flags)
|
||||||
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
|
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
|
||||||
|
|
||||||
@ -184,8 +184,8 @@ func main() {
|
|||||||
ctx, cancel := newSigContext()
|
ctx, cancel := newSigContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// --host and --version should immediately be forwarded to the original cli
|
// --version should immediately be forwarded to the original cli
|
||||||
if opts.Host != "" || opts.Version {
|
if opts.Version {
|
||||||
mobycli.Exec(root)
|
mobycli.Exec(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +214,29 @@ func main() {
|
|||||||
volume.Command(ctype),
|
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)
|
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)
|
ctx = store.WithContextStore(ctx, s)
|
||||||
|
|
||||||
if err = root.ExecuteContext(ctx); err != nil {
|
if err = root.ExecuteContext(ctx); err != nil {
|
||||||
|
@ -17,16 +17,14 @@
|
|||||||
package options
|
package options
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apicontext "github.com/docker/compose-cli/api/context"
|
|
||||||
cliconfig "github.com/docker/compose-cli/cli/config"
|
cliconfig "github.com/docker/compose-cli/cli/config"
|
||||||
|
|
||||||
|
cliflags "github.com/docker/cli/cli/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GlobalOpts contains the global CLI options
|
// GlobalOpts contains the global CLI options
|
||||||
type GlobalOpts struct {
|
type GlobalOpts struct {
|
||||||
apicontext.ContextFlags
|
|
||||||
cliconfig.ConfigFlags
|
cliconfig.ConfigFlags
|
||||||
Debug bool
|
cliflags.CommonOptions
|
||||||
LogLevel string
|
Version bool
|
||||||
Version bool
|
|
||||||
Host string
|
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,12 @@ package local
|
|||||||
import (
|
import (
|
||||||
"context"
|
"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/backend"
|
||||||
"github.com/docker/compose-cli/api/cloud"
|
"github.com/docker/compose-cli/api/cloud"
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/compose-cli/api/containers"
|
"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/resources"
|
||||||
"github.com/docker/compose-cli/api/secrets"
|
"github.com/docker/compose-cli/api/secrets"
|
||||||
"github.com/docker/compose-cli/api/volumes"
|
"github.com/docker/compose-cli/api/volumes"
|
||||||
@ -42,7 +42,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func service(ctx context.Context) (backend.Service, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -39,10 +39,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type containerService struct {
|
type containerService struct {
|
||||||
apiClient *client.Client
|
apiClient client.APIClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
|
func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
|
||||||
|
|
||||||
c, err := cs.apiClient.ContainerInspect(ctx, id)
|
c, err := cs.apiClient.ContainerInspect(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return containers.Container{}, err
|
return containers.Container{}, err
|
||||||
|
@ -29,7 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type volumeService struct {
|
type volumeService struct {
|
||||||
apiClient *client.Client
|
apiClient client.APIClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vs *volumeService) List(ctx context.Context) ([]volumes.Volume, error) {
|
func (vs *volumeService) List(ctx context.Context) ([]volumes.Volume, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user