Require a flag as the first argument in a Cobra command - go

I'm trying to create a Cobra command that uses a flag to inform the action of the command, specifically a configuration command that can either add or remove a configured setting. For example
cli> prog_name config --set config_var var_vlue
cli> prog_name config --unset config_var var_value
Is there a way to do this in Cobra? I have been reading through the documentation and haven't found any way to validate that a flag is the first value in the command. I've seen information about positional arguments, but from what I've read it sounds like flags aren't considered arguments, so they wouldn't be covered by positional arguments.
I'd imagine I can do this in my PreRunE function and do the validation manually, but if there's a way to set this in Cobra I think that'd most likely be better, since I'd prefer Cobra to be doing that parsing and matching rather than me have to be comparing specific values in os.Args to "--set" and "--unset" or something similar.

It seems like the best option is to just use a subcommand for this instead of a flag.

You can solve this by consulting this link.
In short what you need is the Flags() function. You can find documentation here.
package main
import (
"fmt"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "testprog",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("rootCmd called")
},
}
var subCmd = &cobra.Command{
Use: "sub",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(args)
},
}
func main() {
rootCmd.AddCommand(subCmd)
flags := subCmd.Flags()
// not necessary in your case
flags.SetInterspersed(false)
// Bool defines a bool flag with specified name,
// default value, and usage string. The return value
// is the address of a bool variable that stores
// the value of the flag.
flags.Bool("test", false, "test flag")
rootCmd.Execute()
}
Let's see what happens in the terminal:
> ./cobraApp sub --test a
> [a]

Related

golang testing command line arguments

I want to test different (correct/incorrect) command line arguments passed to my CLI program, but I am not sure how to achieve this with go/testing package because I am getting flag redefined error. Looks like it happens because flag.Parse() can be called only once. What is the proper approach to test different command line arguments passed into the go program? Is there is any way to define something like setup()/teardown() or run every case in isolation (but in the same file)?
Here is my code:
Function to test:
func (p *Params) Parse() (*Params, error) {
param1Ptr := flag.String("param1", "default", "param1 desc")
param2Ptr := flag.String("param2", "default", "param1 desc")
...
...
flag.Parse()
...
}
Test file:
package main
import (
"os"
"testing"
)
func TestParam1(t *testing.T) {
os.Args = []string{"cmd", "-param1", "incorrect", "-param2", "correct"}
params := Params{}
_, err := params.Parse()
...
...
}
func TestParam2(t *testing.T) {
os.Args = []string{"cmd", "-param1", "correct", "-param2", "incorrect"}
params := Params{}
_, err := params.Parse()
...
...
}
Don't use the global FlagSet object in the flags package. Create your own FlagSet as a field of Params: https://golang.org/pkg/flag/#FlagSet
All that flag.String et al do is pass through the function call to a global FlagSet object in the flag package (specifically flag.CommandLine is the variable). This is easy to use but not a generally good practice. Using your own flagset would avoid the issues you described as well as other potential side effects from using global variables.
Clear the global FlagSet before each test using:
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
See How to unset flags Visited on command line in GoLang for Tests

GO: Run cli command with wrong args

I use cobra to create CLI command tool.
everything is looking OK except the error handling
what I want that if command was sent by mistake (wrong args or wrong input) return std.err instead of std.out
to simplify the secnario I've created this which demonstrate my use-case
package main
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
RootCmd = &cobra.Command{
Use: "myApp",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("ROOT verbose = %d, args = %v\n", args)
},
}
provideCmd = &cobra.Command{
Use: "provide",
Run: nil,
}
appCmd = &cobra.Command{
Use: "apps",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if name != "myapp" {
err := errors.New("app name doesnt exist")
return err
}
return nil
},
SilenceUsage: true,
}
)
func init() {
// Add the application command to app command
provideCmd.AddCommand(appCmd)
// add provide command to root command
RootCmd.AddCommand(provideCmd)
}
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
Now if I compile the binary and run exec.Command against the binary everything is working as expected. but if I want to test the error scenario like mycli provide apps apps1
I want to see that returned in std.err and not at std.out
When I execute mycli provide apps myapp everything should be OK
but if I run mycli provide apps myapp2 I want to get std.err and not std.out , which is not the case here ...what am I missing here ?
https://play.golang.org/p/B00z4eZ7Sj-
Your sample already prints the error both to stdout and stderr.
By default the cobra package prints any errors it encounters to stderr, unless you specifically change that.
So running
./main provide apps something 2> ./stderr.txt creates a text file with the following content (this is what cobra writes to stderr without your intervention):
Error: app name doesnt exist
And running ./main provide apps something > ./stdout.txt - creates a text file with the following content (you printed that yourself with fmt.Println(err), the second line from the bottom in your code):
app name doesnt exist
Which means default behaviour prints errors both to stdout and stderr.
As Devin has advised you, changing the last line to os.Stderr.WriteString(err) or
fmt.Fprintln(os.Stderr, err) (the one I would use) will make your project to print everything to stderr only, which means printing errors twice:
Error: app name doesnt exist
app name doesnt exist
It might be useful to know that cobra allows you some control of error printing behaviour. For example, you can tell a cobra command which stream to print to:
command.SetOutput(os.Stdout) // Defaults to os.Stderr
you could also prevent printing of errors:
command.SilenceErrors = true
or prevent printing of usage text:
command.SilenceUsage = true

How to Unmarshall Viper config value to struct containing array of string properly?

I noticed that this is perhaps a bug when viper tries to unmarshall to struct. To explain it better, consider this:
I have a cli command like below
dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2
Here is my command line source code
package cmd
import (
"fmt"
"github.com/spf13/viper"
"github.com/spf13/cobra"
)
// SubmitBugOpts is a set of flags being exposed by this Deploy command
type SubmitBugOpts struct {
Name string `mapstructure:"bug-name"`
ReasonTags []string `mapstructure:"tags"`
}
var (
submitBugOpts = SubmitBugOpts{}
)
func submitBugRun(cmd *cobra.Command, args []string) {
fmt.Printf("Bug Name is %+v\n", submitBugOpts.Name)
fmt.Printf("List of tags is %+v\n", submitBugOpts.ReasonTags)
fmt.Printf("Length of tags is %d\n", len(submitBugOpts.ReasonTags))
for index, el := range submitBugOpts.ReasonTags {
fmt.Printf("tag[%d] = %s\n", index, el)
}
}
var submitBugCmd = &cobra.Command{
Use: "submit-bug",
Short: "Deploy/Install a helm chart to Kubernetes cluster",
Run: submitBugRun,
PreRun: func(cmd *cobra.Command, args []string) {
pFlags := cmd.PersistentFlags()
viper.BindPFlag("bug-name", pFlags.Lookup("name"))
viper.BindPFlag("tags", pFlags.Lookup("tag"))
fmt.Printf("Viper all setting value: %+v\n", viper.AllSettings())
fmt.Printf("Before unmarshall: %+v\n", submitBugOpts)
viper.Unmarshal(&submitBugOpts)
fmt.Printf("After unmarshall: %+v\n", submitBugOpts)
},
}
func init() {
rootCmd.AddCommand(submitBugCmd)
pFlags := submitBugCmd.PersistentFlags()
pFlags.StringVar(&submitBugOpts.Name, "name", "", "the bug name")
pFlags.StringArrayVar(&submitBugOpts.ReasonTags, "tag", nil, "the bug's reason tag. You can define it multiple times")
submitBugCmd.MarkPersistentFlagRequired("name")
submitBugCmd.MarkPersistentFlagRequired("tag")
}
I run this command:
dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2
And the output is below
Viper all setting value: map[bug-name:Bug 1 tags:[reason1,reason2]]
Before unmarshall: {Name:Bug 1 ReasonTags:[reason1 reason2]}
After unmarshall: {Name:Bug 1 ReasonTags:[[reason1 reason2]]}
Bug Name is Bug 1
List of tags is [[reason1 reason2]]
Length of tags is 2
tag[0] = [reason1
tag[1] = reason2]
I expect the viper.Unmarshall() will correctly omit the [ for submitBugOpts.ReasonTags [0] and omit the ] for submitBugOpts.ReasonTags[1]. So the expected value of submitBugOpts.ReasonTags doesn't contains any [ and ] .
Any pointer how to fix this? I've submitted this issue on viper repo: https://github.com/spf13/viper/issues/527. However I am asking on SO just in case you guys know how to handle this too.
After digging into codes of github.com/spf13/{cobra,viper,pflag}for quite a while, I finally find the problem.
When you call pFlags.StringArrayVar(&submitBugOpts.ReasonTags, "tag", nil, ...), the ReasonTags, of course, binds to a wrapper of pflag.stringArrayValue. source.
And when you call viper.Unmarshall, viper uses v.Get to get the value binds to ReasonTags and then it calls v.find.
In v.find, after the value is found, it uses the wrapper's ValueType() method to determine its type, the wrapper then calls the Type method of the wrapped type, pflag.stringArrayValue, and returns "stringArray". source
But viper only handle "stringSlice" as a special case, so the value gets to default part of type switch, which uses its ValueString() method - making it into a string, with "[" and "]" on both side. source
And when finally unmarshalling, as your output param, ReasonTags is of []string, the program simply splitted the string and set it into the field.
As for solution, if you are ok with prohibitting tag to contain ,, simply change StringArrayVar to StringSliceVar, but this will results in --tag "Yet, the problem re-occurs" into []string{"Yet"," the problem re-occrus"}.
If that is critical, you'll need to ask the developers of viper to create a case for stringArray.

How to pass a flag to a command in go lang?

I have been trying to run a command and parse the output in golang. Here is a sample of what I am trying to do:
package main
import (
"fmt"
"os/exec"
)
func main() {
out,err := exec.Command("ls -ltr").Output()
if err != nil {
fmt.Println("Error: %s", err)
}
fmt.Printf("%s",out)
}
Now, when I am trying to run "ls -ltr", I get this error:
Error: %s exec: "ls -ltr": executable file not found in $PATH
So, basically go is looking for whole "ls -ltr" in PATH. And it's not there obviously. Is there any way I can pass a flag to any argument?TIA.
You pass arguments to the program by passing more arguments to the function - it's variadic:
out,err := exec.Command("ls","-ltr").Output()
https://golang.org/pkg/os/exec/#Command
This is a pretty common convention with exec-style functions which you will see in most languages. The other common pattern is builders.
Sometimes the layout of arguments you need to pass won't be known at compile-time (though it's not a good idea to send arbitrary commands to the system - stay safe!). If you want to pass an unknown number of arguments, you can use an array with some special syntax:
// Populate myArguments however you like
myArguments := []string{"bar","baz"}
// Pass myArguments with "..." to use variadic behaviour
out,err := exec.Command("foo", myArguments...).Output()

Explanation of Flags in Go

Can anyone explain flags in Go?
flag.Parse()
var omitNewline = flag.Bool("n", false, "don't print final newline")
flags are a common way to specify options for command-line programs.
package main
import (
"flag"
"fmt"
)
var (
env *string
port *int
)
// Basic flag declarations are available for string, integer, and boolean options.
func init() {
env = flag.String("env", "development", "a string")
port = flag.Int("port", 3000, "an int")
}
func main() {
// Once all flags are declared, call flag.Parse() to execute the command-line parsing.
flag.Parse()
// Here we’ll just dump out the parsed options and any trailing positional
// arguments. Note that we need to dereference the points with e.g. *evn to
// get the actual option values.
fmt.Println("env:", *env)
fmt.Println("port:", *port)
}
Run Programs:
go run main.go
Try out the run program by first giving it without flags. Note that if you omit flags they automatically take their default values.
go run command-line-flags.go --env production --port 2000
If you provide a flag with specified value then default will overwrite by passed one.
See http://golang.org/pkg/flag/ for a full description.
The arguments for flag.Bool are (name string, value bool, usage string)
name is the argument to look for, value is the default value and
usage describes the flag's purpose for a -help argument or similar, and is displayed with flag.Usage().
For more detailed example check here
flag is used to parse command line arguments. If you pass "-n" as a command line argument, omitNewLine will be set to true. It's explained a bit farther in the tutorial :
Having imported the flag package, line 12 creates a global variable to hold the value of echo's -n flag. The variable omitNewline has type *bool, pointer to bool.
Personally, I prefer the Var type functions, as they take a reference, rather
than returning a reference. That way you can use the variable without
dereferencing:
package main
import "flag"
func main() {
var omitNewline bool
flag.BoolVar(&omitNewline, "n", false, "don't print final newline")
flag.Parse()
println(omitNewline)
}
https://golang.org/pkg/flag#BoolVar

Resources