Skip to content

Commit

Permalink
Populate all command flags from config (#257)
Browse files Browse the repository at this point in the history
* Populate all command flags from config

* populate global flags
  • Loading branch information
feedmeapples authored Aug 16, 2022
1 parent 5bb74a9 commit 5034497
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func NewCliApp() *cli.App {
promptContinueWithoutConfig()
}
}
populateFlags(app.Commands, app.Flags)
useDynamicCommands(app)

return app
Expand Down
27 changes: 27 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
16 changes: 8 additions & 8 deletions cli/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5034497

Please sign in to comment.