Skip to content

Commit

Permalink
Fix to properly parse key=value pair from args flag
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Vivien <[email protected]>
  • Loading branch information
vladimirvivien committed Jan 15, 2021
1 parent e174dd1 commit 5a26dc3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions cmd/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func newBuildinfoCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "version",
Short: "prints the crash-diagnostics version",
Long: "prints the crash-diagnostics version and other build info",
Short: "prints version",
Long: "Prints version information for the program",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Version:%s\nGitSHA: %s\n", buildinfo.Version, buildinfo.GitSHA)
fmt.Printf("Version: %s\nGitSHA: %s\n", buildinfo.Version, buildinfo.GitSHA)
return nil
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/crash-dianostics.go → cmd/crashd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package cmd

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/vmware-tanzu/crash-diagnostics/buildinfo"
)

Expand All @@ -28,8 +28,8 @@ func crashDiagnosticsCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: CliName,
Short: fmt.Sprintf("%s helps to troubleshoot kubernetes cluster", CliName),
Long: fmt.Sprintf("%s collects diagnostics from an unresponsive Kubernetes cluster", CliName),
Short: "runs the crashd program",
Long: "Runs the crashd program to execute script that interacts with Kubernetes clusters",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return preRun(flags)
},
Expand Down
19 changes: 10 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -31,21 +32,21 @@ func newRunCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "run <file-name>",
Short: "Executes a diagnostics script file",
Long: "Executes a diagnostics script and collects its output as an archive bundle",
Short: "runs a script file",
Long: "Parses and executes the specified script file",
RunE: func(cmd *cobra.Command, args []string) error {
return run(flags, args[0])
},
}
cmd.Flags().StringToStringVar(&flags.args, "args", flags.args, "comma-separated key=value arguments to pass to the diagnostics file")
cmd.Flags().StringVar(&flags.argsFile, "args-file", flags.argsFile, "path to the file having key=value arguments to pass to the diagnostics file")
cmd.Flags().StringToStringVar(&flags.args, "args", flags.args, "comma-separated key=value pairs passed to the script (i.e. --args 'key0=val0,key1=val1')")
cmd.Flags().StringVar(&flags.argsFile, "args-file", flags.argsFile, "path to a file containing key=value argument pairs that are passed to the script file")
return cmd
}

func run(flags *runFlags, path string) error {
file, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "script file not found: %s", path)
return errors.Wrapf(err, "failed to open script file: %s", path)
}
defer file.Close()

Expand All @@ -65,18 +66,18 @@ func run(flags *runFlags, path string) error {
// It builds the map from the args-file as well as the args flag passed to
// the run command.
func processScriptArguments(flags *runFlags) (map[string]string, error) {
// initialize the script arguments map
scriptArgs := map[string]string{}

// populates the scriptArgs map from the scriptArgs-file
// get args from script args file
err := util.ReadArgsFile(flags.argsFile, scriptArgs)
if err != nil && flags.argsFile != ArgsFile {
return nil, errors.Wrapf(err, "failed to parse scriptArgs file: %s", flags.argsFile)
}

// values specified by the args flag override the values from the args-file flag
// any value specified by the args flag overrides
// value with same key in the args-file
for k, v := range flags.args {
scriptArgs[k] = v
scriptArgs[strings.TrimSpace(k)] = strings.TrimSpace(v)
}

return scriptArgs, nil
Expand Down

0 comments on commit 5a26dc3

Please sign in to comment.