From 58386b02bb537c54d0a5c9e5b42909827f0395fd Mon Sep 17 00:00:00 2001 From: feedmeapples Date: Fri, 12 Aug 2022 23:02:08 -0400 Subject: [PATCH] Populate all command flags from config --- cli/app.go | 30 ++++++++++++++++++++++++++++++ cli/factory.go | 16 ++++++++-------- cli/util.go | 16 +--------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/cli/app.go b/cli/app.go index ea79e00b..f1d4ef67 100644 --- a/cli/app.go +++ b/cli/app.go @@ -148,11 +148,41 @@ func NewCliApp() *cli.App { promptContinueWithoutConfig() } } + populateFlagsFromConfig(app.Commands) useDynamicCommands(app) return app } +func populateFlagsFromConfig(commands []*cli.Command) { + for _, command := range commands { + populateFlagsFromConfig(command.Subcommands) + command.Before = withConfigValues(command) + } +} + +func withConfigValues(command *cli.Command) func(ctx *cli.Context) error { + return func(ctx *cli.Context) error { + for _, flag := range command.Flags { + name := flag.Names()[0] + if !ctx.IsSet(name) { + switch flag.(type) { + case *cli.StringFlag: + { + value, _ := tctlConfig.GetByCurrentEnvironment(name) + if value != "" { + ctx.Set(name, value) + } + } + } + } + + } + + return nil + } +} + func configureSDK(ctx *cli.Context) error { endpoint := ctx.String(FlagCodecEndpoint) if endpoint != "" { diff --git a/cli/factory.go b/cli/factory.go index 1f44ebc8..5d028480 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 18a8b0e9..a18087d8 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 {