Ho do you test command line arguments?
I can do this:
func TestMainFunc(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
dir := filepath.Dir(filename)
os.Args[1] = dir
main()
}
But then I override "test.v" in an argument list of:
os.Args[0]={string} "/private/var/folder/.../___appname.go"
os.Args[1]={string} "test.v"
os.Args[2]={string} "-test.panicontext0"
os.Args[3]={string} "^QTestMainFunc\E$"
I have no idea what these arguments are doing, but I suspect it is not good to override any of them - so how can you test arguments?
To expand upon Adrian's comment a bit, I would separate flag parsing from configuration entirely. Here's an example:
type Config struct {
verbose bool
greeting string
level int
}
func parseArgs(progname string, args []string) (config *Config, output string, err error) {
flags := flag.NewFlagSet(progname, flag.ContinueOnError)
var buf bytes.Buffer
flags.SetOutput(&buf)
var conf Config
flags.BoolVar(&conf.verbose, "verbose", false, "set verbosity")
flags.StringVar(&conf.greeting, "greeting", "", "set greeting")
flags.IntVar(&conf.level, "level", 0, "set level")
err = flags.Parse(args)
if err != nil {
return nil, buf.String(), err
}
return &conf, "", nil
}
The parseArgs function parses your program's configuration from command-line arguments, and is easily testable in isolation from the rest of the program.
So the flow in main would be:
func main() {
conf, output, err := parseArgs(os.Args[0], os.Args[1:])
if err != nil {
// ...
}
realMain(conf)
}
And now you can test parseArgs and realMain completely separately in unit tests.
This also opens up the possibility to have multiple sources for configuration: e.g. you could also read it from env vars or config files, or command-line. Multiple readers could populate a Config struct, and all would be testable in isolation.
Related
I’ve the following go code which works, im creating a VTS property which is used in some files under the same package
File A is creating VTS which should be used in all of the functions below (in different files under the same package)
File A
package foo
var VTS = initSettings()
func initSettings() *cli.EnvSettings {
conf := cli.New()
conf.RepositoryCache = "/tmp"
return conf
}
In file B im using it like
package foo
func Get(url string, conf *action.Configuration) (*chart.Chart, error) {
cmd := action.NewInstall(conf)
// Here see the last parameters
chartLocation, err := cmd.ChartPathOptions.LocateChart(url, VTS)
return loader.Load(chartLocation)
}
File C
package foo
func Upgrade(ns, name, url string, vals map[string]interface{}, conf *action.Configuration) (*release.Release, error) {
…
if url == "" {
ch = rel.Chart
} else {
cp, err := client.ChartPathOptions.LocateChart(url, VTS)
if err != nil {
return nil, err
}
ch, err = loader.Load(cp)
}
And in additional files under the same package.
Is there a cleaner way to initiate the VTS and use it in different files instead of package variable ?
I've tried something like
func Settings() *cli.EnvSettings {
cfg := cli.New()
cfg.RepositoryCache = "/tmp"
return cfg
}
and pass it as param but I got error
func GetChart(url string, Settings func(), cfg *action.Configuration) (*chart.Chart, error) {
Just add a *cli.EnvSettings as an additional parameter toGet() and Upgrade(), and then have the caller pass VTS as an argument.
File A
package foo
func initSettings() *cli.EnvSettings {
conf := cli.New()
conf.RepositoryCache = "/tmp"
return conf
}
File B
package foo
func Get(url string, conf *action.Configuration, vts *cli.EnvSettings) (*chart.Chart, error) {
cmd := action.NewInstall(conf)
// Here see the last parameters
chartLocation, err := cmd.ChartPathOptions.LocateChart(url, vts)
return loader.Load(chartLocation)
}
File C
package foo
func Upgrade(ns, name, url string, vals map[string]interface{}, conf *action.Configuration, vts *cli.EnvSettings) (*release.Release, error) {
…
if url == "" {
ch = rel.Chart
} else {
cp, err := client.ChartPathOptions.LocateChart(url, vts)
if err != nil {
return nil, err
}
ch, err = loader.Load(cp)
}
File D: Some other file of a higher level package
...
vts := foo.initSettings()
foo.Get(myUrl, myConf, vts)
Of course, if you want to call foo.Get() or foo.Update() from several other files and packages throughout your project, and if you want all of those calls to use the same *cli.EnvSettings object, you'll likely have to construct VTS from a higher level and pass it around through more functions (i.e. continue the pattern).
In general, this is a form of dependency injection, where foo.Get() and foo.Update() are the "clients" and VTS is the "service". One big advantage of function parameters over package variables is testability. It is difficult to test how foo.Get() behaves with different *cli.EnvSettings objects if said *cli.EnvSettings object is a global / package variable. It's far easier for the tests to decide what *cli.EnvSettings objects to use, and then pass them to foo.Get().
One disadvantage of this pattern is that you can end up with functions with many parameters if they require many injected services, and they can become a bit unwieldy. However, if a function or object truly depends on many services that are truly independent, then there's really no work-around to this anyways. It's usually better to have a function with many parameters than a function that is very difficult to test.
The sketch below is a command line application written using Cobra and Go. I'd like to throw an error if the value of flag1 doesn't match the regex ^\s+\/\s+. How do I do that?
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var flag1 string
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cobra-sketch",
Short: "Sketch for Cobra flags",
Long: "Sketch for Cobra flags",
Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Flag1 is %s\n", flag1)},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra-sketch.yaml)")
rootCmd.PersistentFlags().StringVar(&flag1, "flag1", "", "Value of Flag 1")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)
// Search config in home directory with name ".cobra-sketch" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cobra-sketch")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
Let's say a user runs the command like this: cobra-sketch --flag1 "hello". "hello" will be stored in the var flag1 string variable you have assigned to the flag, to check if the input matches any regexp, you can do:
var rootCmd = &cobra.Command{
Use: "cobra-sketch",
...
RunE: func(cmd *cobra.Command, args []string) error {
// You can also use MustCompile if you are sure the regular expression
// is valid, it panics instead of returning an error
re, err := regexp.Compile(`^\s+\/\s+`)
if err != nil {
return err // Handle error
}
if !regexp.MatchString(flag1) {
return fmt.Errorf("invalid value: %q", flag1)
}
fmt.Printf("Flag1 is %s\n", flag1)
return nil
},
}
Using cobra, if my app is invoked without a specific action (but arguments), I'd like to run a default command:
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "mbmd",
Short: "ModBus Measurement Daemon",
Long: "Easily read and distribute data from ModBus meters and grid inverters",
Run: func(cmd *cobra.Command, args []string) {
run(cmd, args)
},
}
However, since the root command doesn't have all arguments the child command has this fails as it's apparently now aware of the child command's arguments:
❯ go run main.go -d sma:126#localhost:5061 --api 127.1:8081 -v
Error: unknown shorthand flag: 'd' in -d
as opposed to:
❯ go run main.go run -d sma:126#localhost:5061 --api 127.1:8081 -v
2019/07/29 20:58:10 mbmd unknown version (unknown commit)
How can I programmatically instantiate/invoke a child command?
Here is another solution:
cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
if err == nil && cmd.Use == rootCmd.Use && cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp {
args := append([]string{defaultCmd.Use}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Replace defaultCmd with one you want to be default
This part cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp keeps help command working for root command if no arguments was set
March 2021: You might consider a workaround as the one presented in spf13/cobra issue 823
func subCommands() (commandNames []string) {
for _, command := range cmd.Commands() {
commandNames = append(commandNames, append(command.Aliases, command.Name())...)
}
return
}
func setDefaultCommandIfNonePresent() {
if len(os.Args) > 1 {
potentialCommand := os.Args[1]
for _, command := range subCommands() {
if command == potentialCommand {
return
}
}
os.Args = append([]string{os.Args[0], "<default subcommand>"}, os.Args[1:]...)
}
}
func main() {
setDefaultCommandIfNonePresent()
if err := cmd.Execute(); err != nil {
zap.S().Error(err)
os.Exit(1)
}
}
The difference here is that it checks if len(os.Args) > 1 before changing the default subcommand.
This means that, if ran without any arguments, it will print the default help command (with all of the subcommands).
Otherwise, if supplied any arguments, it will use the subcommand.
So, it will display the main 'help' without arguments, and the subcommand's help if supplied '-h'/'--help'.
Or (Oct. 2021), from the author of PR 823:
Latest solve for this is the following:
main.go
func main() {
// Define the default sub command 'defCmd' here. If user doesn't submit
// using a default command, we'll use what is here.
defCmd:="mydefaultcmd"
cmd.Execute(defCmd)
}
root.go
func Execute(defCmd string) {
var cmdFound bool
cmd :=rootCmd.Commands()
for _,a:=range cmd{
for _,b:=range os.Args[1:] {
if a.Name()==b {
cmdFound=true
break
}
}
}
if !cmdFound {
args:=append([]string{defCmd}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
I'm writing a kubectl plugin to authenticate users, and I would like to prompt the user for a password after the plugin is invoked. From what I understand, it's fairly trivial to get input from STDIN, but I'm struggling seeing messages written to STDOUT. Currently my code looks like this:
In cmd/kubectl-myauth.go:
// This is mostly boilerplate, but it's needed for the MRE
// https://stackoverflow.com/help/minimal-reproducible-example
package myauth
import (...)
func main() {
pflag.CommandLine = pflag.NewFlagSet("kubectl-myauth", pflag.ExitOnError)
root := cmd.NewCmdAuthOp(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
In pkg/cmd/auth.go:
package cmd
...
type AuthOpOptions struct {
configFlags *genericclioptions.ConfigFlags
resultingContext *api.Context
rawConfig api.Config
args []string
...
genericclioptions.IOStreams
}
func NewAuthOpOptions(streams genericclioptions.IOStreams) *AuthOpOptions {
return &AuthOpOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
func NewCmdAuthOp(streams genericclioptions.IOStreams) *cobra.Command {
o := NewAuthOpOptions(streams)
cmd := &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
return o.Run()
},
}
return cmd
}
func (o *AuthOpOptions) Run() error {
pass, err := getPassword(o)
if err != nil {
return err
}
// Do Auth Stuff
// Eventually print an ExecCredential to STDOUT
return nil
}
func getPassword(o *AuthOpOptions) (string, error) {
var reader *bufio.Reader
reader = nil
pass := ""
for pass == "" {
// THIS IS AN IMPORTANT LINE [1]
fmt.Fprintf(o.IOStreams.Out, "Password with which to authenticate:\n")
// THE REST OF THIS IS STILL IMPORTANT, BUT LESS SO [2]
if reader == nil {
// The first time through, initialize the reader
reader = bufio.NewReader(o.IOStreams.In)
}
pass, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
pass = strings.Trim(pass, "\r\n")
if pass == "" {
// ALSO THIS LINE IS IMPORTANT [3]
fmt.Fprintf(o.IOStreams.Out, `Read password was empty string.
Please input a valid password.
`)
}
}
return pass, nil
}
This works the way that I expect when running from outside of the kubectl context - namely, it prints the string, prompts for input, and continues. However, from inside the kubectl context, I believe the print between the first two all-caps comments ([1] and [2]) is being swallowed by kubectl listening on STDOUT. I can get around this by printing to STDERR, but that feels... wrong. Is there a way that I can bypass kubectl's consumption of STDOUT to communicate with the user?
TL;DR: kubectl appears to be swallowing all of STDOUT for kubectl plugins, but I want to prompt the user for input - is there a simple way to do this?
Sorry I have no better answer than "Works for me" :-) Here are the steps:
git clone https://github.com/kubernetes/kubernetes.git
duplicate sample-cli-plugin as test-cli-plugin (this involves fixing import-restrictions.yaml, rules-godeps.yaml and rules.yaml under staging/publishing - maybe not necessary, but it's safer this way)
change kubectl-ns.go to kubectl-test.go:
package main
import (
"os"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/test-cli-plugin/pkg/cmd"
)
func main() {
flags := pflag.NewFlagSet("kubectl-test", pflag.ExitOnError)
pflag.CommandLine = flags
root := cmd.NewCmdTest(genericclioptions.IOStreams{In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
change ns.go to test.go:
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
type TestOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
}
func NewTestOptions(streams genericclioptions.IOStreams) *TestOptions {
return &TestOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
func NewCmdTest(streams genericclioptions.IOStreams) *cobra.Command {
o := NewTestOptions(streams)
cmd := &cobra.Command{
Use: "test",
Short: "Test plugin",
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
o.Run()
return nil
},
}
return cmd
}
func (o *TestOptions) Run() error {
fmt.Fprintf(os.Stderr, "Testing Fprintf Stderr\n")
fmt.Fprintf(os.Stdout, "Testing Fprintf Stdout\n")
fmt.Printf("Testing Printf\n")
fmt.Fprintf(o.IOStreams.Out, "Testing Fprintf o.IOStreams.Out\n")
return nil
}
fix BUILD files accordingly
build the plugin
run make
copy kubectl-test to /usr/local/bin
run the compiled kubectl binary:
~/k8s/_output/bin$ ./kubectl test
Testing Fprintf Stderr
Testing Fprintf Stdout
Testing Printf
Testing Fprintf o.IOStreams.Out
Within a template, how can I achieve this?
{{$var := template "my-template"}}
I just get "unexpected <template> in operand".
There is no "builtin" action for getting the result of a template execution, but you may do it by registering a function which does that.
You can register functions with the Template.Funcs() function, you may execute a named template with Template.ExecuteTemplate() and you may use a bytes.Buffer as the target (direct template execution result into a buffer).
Here is a complete example:
var t *template.Template
func execTempl(name string) (string, error) {
buf := &bytes.Buffer{}
err := t.ExecuteTemplate(buf, name, nil)
return buf.String(), err
}
func main() {
t = template.Must(template.New("").Funcs(template.FuncMap{
"execTempl": execTempl,
}).Parse(tmpl))
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
}
}
const tmpl = `{{define "my-template"}}my-template content{{end}}
See result:
{{$var := execTempl "my-template"}}
{{$var}}
`
Output (try it on the Go Playground):
See result:
my-template content
The "my-template" template is executed by the registered function execTempl(), and the result is returned as a string, which is stored in the $var template variable, which then is simply added to the output, but you may use it to pass to other functions if you want to.