github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/flags/common.go (about) 1 package flags 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 cliconfig "github.com/docker/cli/cli/config" 9 "github.com/docker/cli/opts" 10 "github.com/docker/go-connections/tlsconfig" 11 "github.com/sirupsen/logrus" 12 "github.com/spf13/pflag" 13 ) 14 15 const ( 16 // DefaultCaFile is the default filename for the CA pem file 17 DefaultCaFile = "ca.pem" 18 // DefaultKeyFile is the default filename for the key pem file 19 DefaultKeyFile = "key.pem" 20 // DefaultCertFile is the default filename for the cert pem file 21 DefaultCertFile = "cert.pem" 22 // FlagTLSVerify is the flag name for the TLS verification option 23 FlagTLSVerify = "tlsverify" 24 ) 25 26 var ( 27 dockerCertPath = os.Getenv("DOCKER_CERT_PATH") 28 dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != "" 29 dockerTLS = os.Getenv("DOCKER_TLS") != "" 30 ) 31 32 // CommonOptions are options common to both the client and the daemon. 33 type CommonOptions struct { 34 Debug bool 35 Hosts []string 36 LogLevel string 37 TLS bool 38 TLSVerify bool 39 TLSOptions *tlsconfig.Options 40 Context string 41 } 42 43 // NewCommonOptions returns a new CommonOptions 44 func NewCommonOptions() *CommonOptions { 45 return &CommonOptions{} 46 } 47 48 // InstallFlags adds flags for the common options on the FlagSet 49 func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) { 50 if dockerCertPath == "" { 51 dockerCertPath = cliconfig.Dir() 52 } 53 54 flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode") 55 flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`) 56 flags.BoolVar(&commonOpts.TLS, "tls", dockerTLS, "Use TLS; implied by --tlsverify") 57 flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote") 58 59 // TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file") 60 61 commonOpts.TLSOptions = &tlsconfig.Options{ 62 CAFile: filepath.Join(dockerCertPath, DefaultCaFile), 63 CertFile: filepath.Join(dockerCertPath, DefaultCertFile), 64 KeyFile: filepath.Join(dockerCertPath, DefaultKeyFile), 65 } 66 tlsOptions := commonOpts.TLSOptions 67 flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA") 68 flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file") 69 flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file") 70 71 // opts.ValidateHost is not used here, so as to allow connection helpers 72 hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, nil) 73 flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to") 74 flags.StringVarP(&commonOpts.Context, "context", "c", "", 75 `Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")`) 76 } 77 78 // SetDefaultOptions sets default values for options after flag parsing is 79 // complete 80 func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) { 81 // Regardless of whether the user sets it to true or false, if they 82 // specify --tlsverify at all then we need to turn on TLS 83 // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need 84 // to check that here as well 85 if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify { 86 commonOpts.TLS = true 87 } 88 89 if !commonOpts.TLS { 90 commonOpts.TLSOptions = nil 91 } else { 92 tlsOptions := commonOpts.TLSOptions 93 tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify 94 95 // Reset CertFile and KeyFile to empty string if the user did not specify 96 // the respective flags and the respective default files were not found. 97 if !flags.Changed("tlscert") { 98 if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) { 99 tlsOptions.CertFile = "" 100 } 101 } 102 if !flags.Changed("tlskey") { 103 if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) { 104 tlsOptions.KeyFile = "" 105 } 106 } 107 } 108 } 109 110 // SetLogLevel sets the logrus logging level 111 func SetLogLevel(logLevel string) { 112 if logLevel != "" { 113 lvl, err := logrus.ParseLevel(logLevel) 114 if err != nil { 115 fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel) 116 os.Exit(1) 117 } 118 logrus.SetLevel(lvl) 119 } else { 120 logrus.SetLevel(logrus.InfoLevel) 121 } 122 }