Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swctl: config helper wrap 'agentctl --help' and merge with swctl #144

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:]...)
}
Giluerre marked this conversation as resolved.
Show resolved Hide resolved

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
}