From 3a442da2c0d1c3164bee7e2a9822424389bcd1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A9re=C5=A1?= Date: Tue, 17 Oct 2023 04:48:29 -0700 Subject: [PATCH] swctl: config helper wrap 'agentctl --help' and merge 'swctl config flags' to it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Béreš --- cmd/swctl/app/cmd_config.go | 71 +++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/cmd/swctl/app/cmd_config.go b/cmd/swctl/app/cmd_config.go index 027f1d4..390b667 100644 --- a/cmd/swctl/app/cmd_config.go +++ b/cmd/swctl/app/cmd_config.go @@ -2,6 +2,7 @@ package app import ( "fmt" + "strings" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -20,6 +21,7 @@ type ConfigCmdOptions struct { func (opts *ConfigCmdOptions) InstallFlags(flagset *pflag.FlagSet) { flagset.BoolVar(&opts.ShowInternal, "show-internal", false, "Add Stonework internal configuration to output if possible") + } func NewConfigCmd(cli Cli) *cobra.Command { @@ -27,22 +29,48 @@ func NewConfigCmd(cli Cli) *cobra.Command { opts ConfigCmdOptions ) cmd := &cobra.Command{ - Use: "config [flags] ACTION", - Short: "Manage config of StoneWork components", - Args: cobra.ArbitraryArgs, - // DisableFlagParsing: true, + Use: "config [flags] ACTION", + Short: "Manage config of StoneWork components", + Args: cobra.ArbitraryArgs, + DisableFlagParsing: true, RunE: func(cmd *cobra.Command, args []string) error { opts.Args = args - return runConfigCmd(cli, opts) + return runConfigCmd(cli, opts, cmd) }, } opts.InstallFlags(cmd.PersistentFlags()) return cmd } -func runConfigCmd(cli Cli, opts ConfigCmdOptions) error { +type temporaryFlag struct { + // full name flag + key string + // full line + value string +} + +func runConfigCmd(cli Cli, opts ConfigCmdOptions, cmd *cobra.Command) error { + var ParsedFlags []temporaryFlag args := opts.Args + if slices.Contains(args, "--help") || slices.Contains(args, "-h") { + // add Local flags to stdout of agentctl + stdout, stderr, shouldReturn, returnValue := mergeHelpers(cmd, ParsedFlags, cli, args) + if shouldReturn { + return returnValue + } + + fmt.Fprintln(cli.Err(), stderr) + fmt.Fprintln(cli.Out(), stdout) + return nil + } + + if slices.Contains(args, "--show-internal") { + opts.ShowInternal = true + idx := slices.Index[string](args, "--show-internal") + args = append(args[:idx], args[idx+1:]...) + } + if slices.Contains(args, "get") && !opts.ShowInternal { hideInternalFlag := "--labels=\"!" + puntmgr.InternalConfigLabelKey + "=" + puntmgr.InternalConfigLabelValue + "\"" args = append(args, hideInternalFlag) @@ -57,3 +85,34 @@ func runConfigCmd(cli Cli, opts ConfigCmdOptions) error { fmt.Fprintln(cli.Out(), stdout) return nil } + +func mergeHelpers(cmd *cobra.Command, ParsedFlags []temporaryFlag, cli Cli, args []string) (string, string, bool, error) { + flags := cmd.LocalFlags() + bufb := flags.FlagUsages() + + for _, v := range strings.Split(bufb, "\n") { + var flag temporaryFlag + ls := strings.ReplaceAll(v, ",", " ") + tokenized := strings.Fields(ls) + for _, token := range tokenized { + if strings.Contains(token, "--") { + flag.key = token + flag.value = v + break + } + } + ParsedFlags = append(ParsedFlags, flag) + } + _ = ParsedFlags + + stdout, stderr, err := cli.Exec("agentctl config", args) + if err != nil { + return "", "", true, err + } + + globalsIndex := strings.Index(stdout, "GLOBALS:") + if globalsIndex != -1 { + stdout = stdout[:globalsIndex] + fmt.Sprintf("Flags:\n%s", bufb) + stdout[globalsIndex:] + } + return stdout, stderr, false, nil +}