Skip to content

Commit

Permalink
swctl: config helper wrap 'agentctl --help' and merge with swctl (#144)
Browse files Browse the repository at this point in the history
* swctl: config helper wrap 'agentctl --help' and merge 'swctl config flags' to it

Signed-off-by: Daniel Béreš <[email protected]>

* replace occurencies of agentctl with swctl for config --help

Signed-off-by: Daniel Béreš <[email protected]>

---------

Signed-off-by: Daniel Béreš <[email protected]>
  • Loading branch information
Giluerre authored Oct 23, 2023
1 parent 7aa1cc3 commit 6209a1d
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions cmd/swctl/app/cmd_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app

import (
"fmt"
"strings"

"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -19,29 +22,48 @@ 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 {
func runConfigCmd(cli Cli, opts ConfigCmdOptions, cmd *cobra.Command) error {
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, 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 @@ -56,3 +78,20 @@ func runConfigCmd(cli Cli, opts ConfigCmdOptions) error {
color.Fprintln(cli.Out(), stdout)
return nil
}

func mergeHelpers(cmd *cobra.Command, cli Cli, args []string) (string, string, bool, error) {
flags := cmd.LocalFlags()
bufb := flags.FlagUsages()

stdout, stderr, err := cli.Exec("agentctl config", args, false)
if err != nil {
return "", "", true, err
}
stdout = strings.ReplaceAll(stdout, "agentctl", "swctl")

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 6209a1d

Please sign in to comment.