diff --git a/cli/app.go b/cli/app.go index df10ef3..4cbf37c 100644 --- a/cli/app.go +++ b/cli/app.go @@ -148,6 +148,7 @@ func NewCliApp() *cli.App { promptContinueWithoutConfig() } } + populateFlags(app.Commands, app.Flags) useDynamicCommands(app) return app diff --git a/cli/config.go b/cli/config.go index c54ef2b..488b8c0 100644 --- a/cli/config.go +++ b/cli/config.go @@ -126,3 +126,30 @@ func createAlias(c *cli.Context) error { fmt.Printf("%v: %v\n", color.Magenta(c, "%v", config.KeyAliases), alias) return nil } + +func populateFlags(commands []*cli.Command, globalFlags []cli.Flag) { + for _, command := range commands { + populateFlags(command.Subcommands, globalFlags) + command.Before = populateFlagsFunc(command, globalFlags) + } +} + +func populateFlagsFunc(command *cli.Command, globalFlags []cli.Flag) func(ctx *cli.Context) error { + return func(ctx *cli.Context) error { + flags := append(command.Flags, globalFlags...) + for _, flag := range flags { + name := flag.Names()[0] + + for _, c := range ctx.Lineage() { + if !c.IsSet(name) { + value, _ := tctlConfig.GetByCurrentEnvironment(name) + if value != "" { + c.Set(name, value) + } + } + } + } + + return nil + } +} diff --git a/cli/factory.go b/cli/factory.go index c626181..cfe9733 100644 --- a/cli/factory.go +++ b/cli/factory.go @@ -103,7 +103,7 @@ func (b *clientFactory) OperatorClient(c *cli.Context) operatorservice.OperatorS // SDKClient builds an SDK client. func (b *clientFactory) SDKClient(c *cli.Context, namespace string) sdkclient.Client { - hostPort := readFlagOrConfig(c, FlagAddress) + hostPort := c.String(FlagAddress) if hostPort == "" { hostPort = localHostPort } @@ -159,7 +159,7 @@ func errorInterceptor() grpc.UnaryClientInterceptor { } func (b *clientFactory) createGRPCConnection(c *cli.Context) (*grpc.ClientConn, error) { - hostPort := readFlagOrConfig(c, FlagAddress) + hostPort := c.String(FlagAddress) if hostPort == "" { hostPort = localHostPort } @@ -197,16 +197,16 @@ func (b *clientFactory) createGRPCConnection(c *cli.Context) (*grpc.ClientConn, } func (b *clientFactory) createTLSConfig(c *cli.Context) (*tls.Config, error) { - certPath := readFlagOrConfig(c, FlagTLSCertPath) - keyPath := readFlagOrConfig(c, FlagTLSKeyPath) - caPath := readFlagOrConfig(c, FlagTLSCaPath) - disableHostNameVerificationS := readFlagOrConfig(c, FlagTLSDisableHostVerification) + certPath := c.String(FlagTLSCertPath) + keyPath := c.String(FlagTLSKeyPath) + caPath := c.String(FlagTLSCaPath) + disableHostNameVerificationS := c.String(FlagTLSDisableHostVerification) disableHostNameVerification, err := strconv.ParseBool(disableHostNameVerificationS) if err != nil { return nil, fmt.Errorf("unable to read TLS disable host verification flag: %s", err) } - serverName := readFlagOrConfig(c, FlagTLSServerName) + serverName := c.String(FlagTLSServerName) var host string var cert *tls.Certificate @@ -233,7 +233,7 @@ func (b *clientFactory) createTLSConfig(c *cli.Context) (*tls.Config, error) { if serverName != "" { host = serverName } else { - hostPort := readFlagOrConfig(c, FlagAddress) + hostPort := c.String(FlagAddress) if hostPort == "" { hostPort = localHostPort } diff --git a/cli/util.go b/cli/util.go index f2750c5..5bb2940 100644 --- a/cli/util.go +++ b/cli/util.go @@ -366,27 +366,13 @@ func getSDKClient(c *cli.Context) (sdkclient.Client, error) { } func getRequiredGlobalOption(c *cli.Context, optionName string) (string, error) { - value := readFlagOrConfig(c, optionName) + value := c.String(optionName) if len(value) == 0 { return "", fmt.Errorf("option is required: %s", optionName) } return value, nil } -func readFlagOrConfig(c *cli.Context, key string) string { - if c.IsSet(key) { - return c.String(key) - } - - cVal, _ := tctlConfig.GetByCurrentEnvironment(key) - - if cVal != "" { - return cVal - } - - return c.String(key) -} - func formatTime(t time.Time, onlyTime bool) string { var result string if onlyTime {