diff --git a/cmd/completion.go b/cmd/completion.go index ea8b2bc..052857f 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -88,7 +88,7 @@ func init() { // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: - // completionCmd.PersistentFlags().String("foo", "", "A help for foo") + // completionCmd.PersistentFlags().String("foo", utils.EmptyString, "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: diff --git a/cmd/generate.go b/cmd/generate.go index ae19c7e..bc5fd4c 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -17,6 +17,7 @@ package cmd import ( "github.com/ca-gip/dploy/internal/ansible" + "github.com/ca-gip/dploy/internal/utils" "github.com/spf13/cobra" "log" "os" @@ -37,15 +38,15 @@ func init() { rootCmd.AddCommand(generateCmd) // Required flags - generateCmd.Flags().StringSliceP("filter", "", nil, `filters inventory based its on vars ex: "foo==bar,bar!=foo""`) + generateCmd.Flags().StringSliceP("filter", utils.EmptyString, nil, `filters inventory based its on vars ex: "foo==bar,bar!=fooutils.EmptyString`) _ = generateCmd.MarkFlagRequired("filter") - generateCmd.Flags().StringP("playbook", "p", "", "playbook to run") + generateCmd.Flags().StringP("playbook", "p", utils.EmptyString, "playbook to run") _ = generateCmd.MarkFlagRequired("playbook") // Ansible params - generateCmd.Flags().BoolP("ask-vault-password", "", false, "ask for vault password") - generateCmd.Flags().StringP("vault-password-file", "", "", "vault password file") - generateCmd.Flags().StringSliceP("skip-tags", "", nil, "only run plays and tasks whose tags do not match these values") + generateCmd.Flags().BoolP("ask-vault-password", utils.EmptyString, false, "ask for vault password") + generateCmd.Flags().StringP("vault-password-file", utils.EmptyString, utils.EmptyString, "vault password file") + generateCmd.Flags().StringSliceP("skip-tags", utils.EmptyString, nil, "only run plays and tasks whose tags do not match these values") generateCmd.Flags().BoolP("check", "C", false, "don't make any changes; instead, try to predict some of the changes that may occur") generateCmd.Flags().BoolP("diff", "D", false, "when changing (small) files and templates, show the differences in those files; works great with --check") generateCmd.Flags().StringSliceP("limit", "l", nil, "further limit selected hosts to an additional pattern") diff --git a/cmd/play.go b/cmd/play.go index 65f918f..72006aa 100644 --- a/cmd/play.go +++ b/cmd/play.go @@ -18,6 +18,7 @@ package cmd import ( ansibler "github.com/apenella/go-ansible" "github.com/ca-gip/dploy/internal/ansible" + "github.com/ca-gip/dploy/internal/utils" log "github.com/sirupsen/logrus" "os" "strings" @@ -40,13 +41,13 @@ func init() { rootCmd.AddCommand(playCmd) // Required flags - playCmd.Flags().StringSliceP("filter", "", nil, `filters inventory based its on vars ex: "foo==bar,bar!=foo""`) + playCmd.Flags().StringSliceP("filter", utils.EmptyString, nil, `filters inventory based its on vars ex: "foo==bar,bar!=fooutils.EmptyString`) _ = playCmd.MarkFlagRequired("filter") - playCmd.Flags().StringP("playbook", "p", "", "playbook to run") + playCmd.Flags().StringP("playbook", "p", utils.EmptyString, "playbook to run") _ = playCmd.MarkFlagRequired("playbook") // Ansible params - playCmd.Flags().StringP("vault-password-file", "", "", "vault password file") + playCmd.Flags().StringP("vault-password-file", utils.EmptyString, utils.EmptyString, "vault password file") playCmd.Flags().StringSliceP("limit", "l", nil, "further limit selected hosts to an additional pattern") playCmd.Flags().StringSliceP("tags", "t", nil, "only run plays and tasks tagged with these values") } diff --git a/cmd/root.go b/cmd/root.go index 955ecb4..364cca9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( "fmt" + "github.com/ca-gip/dploy/internal/utils" "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -61,7 +62,7 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dploy.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", utils.EmptyString, "config file (default is $HOME/.dploy.yaml)") rootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", logrus.InfoLevel.String(), "Log level (debug, info, warn, error, fatal, panic") // Cobra also supports local flags, which will only run @@ -77,7 +78,7 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { + if cfgFile != utils.EmptyString { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { diff --git a/cmd/shared.go b/cmd/shared.go index f090a66..3af05d2 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "github.com/ca-gip/dploy/internal/ansible" + "github.com/ca-gip/dploy/internal/utils" "github.com/spf13/cobra" "regexp" "strings" @@ -17,12 +18,12 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp availableKeys := k8s.InventoryKeys() - blank := key == "" && op == "" && value == "" + blank := key == utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString if blank { return availableKeys, cobra.ShellCompDirectiveDefault } - writingKey := key != "" && op == "" && value == "" + writingKey := key != utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString if writingKey { var keysCompletion []string for _, availableKey := range availableKeys { @@ -43,7 +44,7 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp return keysCompletion, cobra.ShellCompDirectiveDefault } - writingOp := key != "" && op != "" && value == "" + writingOp := key != utils.EmptyString && op != utils.EmptyString && value == utils.EmptyString if writingOp { var prefixedOperator []string @@ -56,7 +57,7 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp for _, availableValue := range availableValues { - if availableValue != "" { + if availableValue != utils.EmptyString { prefixedValues = append(prefixedValues, fmt.Sprintf("%s%s%s", key, op, availableValue)) } @@ -81,7 +82,7 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp for _, availableValue := range availableValues { - if availableValue != "" { + if availableValue != utils.EmptyString { prefixedValues = append(prefixedValues, fmt.Sprintf("%s%s%s", key, foundOp, availableValue)) } @@ -93,7 +94,7 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp return prefixedOperator, cobra.ShellCompDirectiveDefault } - writingValue := key != "" && op != "" && value != "" + writingValue := key != utils.EmptyString && op != utils.EmptyString && value != utils.EmptyString if writingValue { for _, allowedOperator := range ansible.AllowedOperators { @@ -103,7 +104,7 @@ func filterCompletion(toComplete string, path string) ([]string, cobra.ShellComp var prefixedValues []string for _, availableValue := range availableValues { - if availableValue != "" && strings.HasPrefix(availableValue, value) { + if availableValue != utils.EmptyString && strings.HasPrefix(availableValue, value) { prefixedValues = append(prefixedValues, fmt.Sprintf("%s%s%s", key, op, availableValue)) } diff --git a/internal/ansible/filter_test.go b/internal/ansible/filter_test.go index 90b28e1..9c9d9af 100644 --- a/internal/ansible/filter_test.go +++ b/internal/ansible/filter_test.go @@ -1,6 +1,7 @@ package ansible import ( + "github.com/ca-gip/dploy/internal/utils" "github.com/go-test/deep" "github.com/stretchr/testify/assert" "testing" @@ -58,7 +59,7 @@ func TestEval(t *testing.T) { }{ "foo equal foo should pass": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: Equal, Value: "foo", }, @@ -67,7 +68,7 @@ func TestEval(t *testing.T) { }, "foo equal bar should fail": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: Equal, Value: "foo", }, @@ -76,7 +77,7 @@ func TestEval(t *testing.T) { }, "foo notequal bar should pass": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: NotEqual, Value: "foo", }, @@ -85,7 +86,7 @@ func TestEval(t *testing.T) { }, "foo notequal foo should fail": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: NotEqual, Value: "foo", }, @@ -94,7 +95,7 @@ func TestEval(t *testing.T) { }, "foo endwith oo should pass": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: EndWith, Value: "foo", }, @@ -103,7 +104,7 @@ func TestEval(t *testing.T) { }, "foo endwith fo should fail": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: EndWith, Value: "foo", }, @@ -112,7 +113,7 @@ func TestEval(t *testing.T) { }, "foo contains o should pass": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: Contains, Value: "foo", }, @@ -121,7 +122,7 @@ func TestEval(t *testing.T) { }, "foo contains z should fail": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: Contains, Value: "foo", }, @@ -130,7 +131,7 @@ func TestEval(t *testing.T) { }, "foo StartWith fo should pass": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: StartWith, Value: "foo", }, @@ -139,7 +140,7 @@ func TestEval(t *testing.T) { }, "foo StartWith oo should fail": { given: Filter{ - Key: "", + Key: utils.EmptyString, Op: StartWith, Value: "foo", }, diff --git a/internal/ansible/inventory_test.go b/internal/ansible/inventory_test.go index 6783456..6d48557 100644 --- a/internal/ansible/inventory_test.go +++ b/internal/ansible/inventory_test.go @@ -1,6 +1,7 @@ package ansible import ( + "github.com/ca-gip/dploy/internal/utils" "github.com/relex/aini" "github.com/stretchr/testify/assert" "testing" @@ -23,7 +24,7 @@ func TestProject_InventoryKeys(t *testing.T) { "single inventory with one var should return one key ": { inventories: []*Inventory{{ Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": utils.EmptyString}}}, }, }}, expect: []string{"key1"}, @@ -31,7 +32,7 @@ func TestProject_InventoryKeys(t *testing.T) { "single inventory with two var should return two key ": { inventories: []*Inventory{{ Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": "", "key2": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": utils.EmptyString, "key2": utils.EmptyString}}}, }, }}, expect: []string{"key1", "key2"}, @@ -39,20 +40,20 @@ func TestProject_InventoryKeys(t *testing.T) { "two inventories with same var should return one key ": { inventories: []*Inventory{{ Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": utils.EmptyString}}}, }, }, {Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": utils.EmptyString}}}, }}}, expect: []string{"key1"}, }, "two inventories with different var should return two key ": { inventories: []*Inventory{{ Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": utils.EmptyString}}}, }, }, {Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key2": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key2": utils.EmptyString}}}, }}}, expect: []string{"key1", "key2"}, }, @@ -136,7 +137,7 @@ func TestProject_InventoryValues(t *testing.T) { Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key1": "value1"}}}, }, }, {Data: &aini.InventoryData{ - Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key2": ""}}}, + Groups: map[string]*aini.Group{"all": {Vars: map[string]string{"key2": utils.EmptyString}}}, }}}, given: "key1", expect: []string{"value1"}, diff --git a/internal/ansible/project.go b/internal/ansible/project.go index 8d1e724..3d2925b 100644 --- a/internal/ansible/project.go +++ b/internal/ansible/project.go @@ -64,7 +64,7 @@ func (p Project) InventoryValues(key string) (values []string) { valueSet := utils.NewSet() for _, inventory := range p.Inventories { if inventory.Data != nil { - if value := inventory.Data.Groups["all"].Vars[key]; value != "" { + if value := inventory.Data.Groups["all"].Vars[key]; value != utils.EmptyString { valueSet.Add(value) } } diff --git a/internal/ansible/role.go b/internal/ansible/role.go index 1a22ca7..5e8b05a 100644 --- a/internal/ansible/role.go +++ b/internal/ansible/role.go @@ -75,7 +75,7 @@ func (role *Role) LoadFromPath(rootPath string) (err error) { return nil } - resTask := []Task{} + var resTask []Task for _, task := range roleTasks.Tasks { resTask = append(resTask, task) } diff --git a/internal/utils/logs.go b/internal/utils/logs.go index 77cc842..464147f 100644 --- a/internal/utils/logs.go +++ b/internal/utils/logs.go @@ -25,8 +25,8 @@ func WrapRed(msg ...string) string { func wrapLog(color string, msg ...string) string { if isTerminal() { - return fmt.Sprintf("\033[1;"+color+"m%v\033[0m", strings.Join(msg, "")) + return fmt.Sprintf("\033[1;"+color+"m%v\033[0m", strings.Join(msg, EmptyString)) } else { - return fmt.Sprintf(strings.Join(msg, "")) + return fmt.Sprintf(strings.Join(msg, EmptyString)) } }