Skip to content

Commit

Permalink
swctl: config helper wrap 'agentctl --help' and merge 'swctl config f…
Browse files Browse the repository at this point in the history
…lags' to it

Signed-off-by: Daniel Béreš <[email protected]>
  • Loading branch information
Giluerre committed Oct 18, 2023
1 parent a7f3e74 commit 3a442da
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions cmd/swctl/app/cmd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -20,29 +21,56 @@ 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 {
var (
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)
Expand All @@ -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)

Check failure on line 108 in cmd/swctl/app/cmd_config.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to cli.Exec
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
}

0 comments on commit 3a442da

Please sign in to comment.