mirror of https://github.com/docker/cli.git
Convert dockerd to use cobra and pflag
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
39c47a0e24
commit
d08dd40a88
|
@ -1,11 +1,13 @@
|
||||||
package flags
|
package flags
|
||||||
|
|
||||||
import flag "github.com/docker/docker/pkg/mflag"
|
import (
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
// ClientFlags represents flags for the docker client.
|
// ClientFlags represents flags for the docker client.
|
||||||
type ClientFlags struct {
|
type ClientFlags struct {
|
||||||
FlagSet *flag.FlagSet
|
FlagSet *pflag.FlagSet
|
||||||
Common *CommonFlags
|
Common *CommonOptions
|
||||||
PostParse func()
|
PostParse func()
|
||||||
|
|
||||||
ConfigDir string
|
ConfigDir string
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/cliconfig"
|
"github.com/docker/docker/cliconfig"
|
||||||
"github.com/docker/docker/opts"
|
"github.com/docker/docker/opts"
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
"github.com/docker/go-connections/tlsconfig"
|
"github.com/docker/go-connections/tlsconfig"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -21,8 +21,8 @@ const (
|
||||||
DefaultKeyFile = "key.pem"
|
DefaultKeyFile = "key.pem"
|
||||||
// DefaultCertFile is the default filename for the cert pem file
|
// DefaultCertFile is the default filename for the cert pem file
|
||||||
DefaultCertFile = "cert.pem"
|
DefaultCertFile = "cert.pem"
|
||||||
// TLSVerifyKey is the default flag name for the tls verification option
|
// FlagTLSVerify is the flag name for the tls verification option
|
||||||
TLSVerifyKey = "tlsverify"
|
FlagTLSVerify = "tlsverify"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -30,11 +30,8 @@ var (
|
||||||
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
||||||
)
|
)
|
||||||
|
|
||||||
// CommonFlags are flags common to both the client and the daemon.
|
// CommonOptions are options common to both the client and the daemon.
|
||||||
type CommonFlags struct {
|
type CommonOptions struct {
|
||||||
FlagSet *flag.FlagSet
|
|
||||||
PostParse func()
|
|
||||||
|
|
||||||
Debug bool
|
Debug bool
|
||||||
Hosts []string
|
Hosts []string
|
||||||
LogLevel string
|
LogLevel string
|
||||||
|
@ -44,62 +41,60 @@ type CommonFlags struct {
|
||||||
TrustKey string
|
TrustKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitCommonFlags initializes flags common to both client and daemon
|
// NewCommonOptions returns a new CommonOptions
|
||||||
func InitCommonFlags() *CommonFlags {
|
func NewCommonOptions() *CommonOptions {
|
||||||
var commonFlags = &CommonFlags{FlagSet: new(flag.FlagSet)}
|
return &CommonOptions{
|
||||||
|
TLSOptions: &tlsconfig.Options{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstallFlags adds flags for the common options on the FlagSet
|
||||||
|
func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
|
||||||
if dockerCertPath == "" {
|
if dockerCertPath == "" {
|
||||||
dockerCertPath = cliconfig.ConfigDir()
|
dockerCertPath = cliconfig.ConfigDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
commonFlags.PostParse = func() { postParseCommon(commonFlags) }
|
flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode")
|
||||||
|
flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", "Set the logging level")
|
||||||
|
flags.BoolVar(&commonOpts.TLS, "tls", false, "Use TLS; implied by --tlsverify")
|
||||||
|
flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
|
||||||
|
|
||||||
cmd := commonFlags.FlagSet
|
// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
|
||||||
|
|
||||||
cmd.BoolVar(&commonFlags.Debug, []string{"D", "-debug"}, false, "Enable debug mode")
|
tlsOptions := commonOpts.TLSOptions
|
||||||
cmd.StringVar(&commonFlags.LogLevel, []string{"l", "-log-level"}, "info", "Set the logging level")
|
flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
|
||||||
cmd.BoolVar(&commonFlags.TLS, []string{"-tls"}, false, "Use TLS; implied by --tlsverify")
|
flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
|
||||||
cmd.BoolVar(&commonFlags.TLSVerify, []string{"-tlsverify"}, dockerTLSVerify, "Use TLS and verify the remote")
|
flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
|
||||||
|
|
||||||
// TODO use flag flag.String([]string{"i", "-identity"}, "", "Path to libtrust key file")
|
hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
|
||||||
|
flags.VarP(hostOpt, "-host", "H", "Daemon socket(s) to connect to")
|
||||||
var tlsOptions tlsconfig.Options
|
|
||||||
commonFlags.TLSOptions = &tlsOptions
|
|
||||||
cmd.StringVar(&tlsOptions.CAFile, []string{"-tlscacert"}, filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
|
|
||||||
cmd.StringVar(&tlsOptions.CertFile, []string{"-tlscert"}, filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
|
|
||||||
cmd.StringVar(&tlsOptions.KeyFile, []string{"-tlskey"}, filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
|
|
||||||
|
|
||||||
cmd.Var(opts.NewNamedListOptsRef("hosts", &commonFlags.Hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to")
|
|
||||||
return commonFlags
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func postParseCommon(commonFlags *CommonFlags) {
|
// SetDefaultOptions sets default values for options after flag parsing is
|
||||||
cmd := commonFlags.FlagSet
|
// complete
|
||||||
|
func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
|
||||||
SetDaemonLogLevel(commonFlags.LogLevel)
|
|
||||||
|
|
||||||
// Regardless of whether the user sets it to true or false, if they
|
// Regardless of whether the user sets it to true or false, if they
|
||||||
// specify --tlsverify at all then we need to turn on tls
|
// specify --tlsverify at all then we need to turn on tls
|
||||||
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
|
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
|
||||||
// to check that here as well
|
// to check that here as well
|
||||||
if cmd.IsSet("-"+TLSVerifyKey) || commonFlags.TLSVerify {
|
if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
|
||||||
commonFlags.TLS = true
|
commonOpts.TLS = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if !commonFlags.TLS {
|
if !commonOpts.TLS {
|
||||||
commonFlags.TLSOptions = nil
|
commonOpts.TLSOptions = nil
|
||||||
} else {
|
} else {
|
||||||
tlsOptions := commonFlags.TLSOptions
|
tlsOptions := commonOpts.TLSOptions
|
||||||
tlsOptions.InsecureSkipVerify = !commonFlags.TLSVerify
|
tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
|
||||||
|
|
||||||
// Reset CertFile and KeyFile to empty string if the user did not specify
|
// Reset CertFile and KeyFile to empty string if the user did not specify
|
||||||
// the respective flags and the respective default files were not found.
|
// the respective flags and the respective default files were not found.
|
||||||
if !cmd.IsSet("-tlscert") {
|
if !flags.Changed("tlscert") {
|
||||||
if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
|
if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
|
||||||
tlsOptions.CertFile = ""
|
tlsOptions.CertFile = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !cmd.IsSet("-tlskey") {
|
if !flags.Changed("tlskey") {
|
||||||
if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
|
if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
|
||||||
tlsOptions.KeyFile = ""
|
tlsOptions.KeyFile = ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue