Skip to content

Commit

Permalink
Refacto, utils.EmptyString (more error proof: space char...)
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 27, 2021
1 parent 7169d02 commit 10a78af
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand Down
11 changes: 6 additions & 5 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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))
}

Expand All @@ -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))
}

Expand All @@ -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 {

Expand All @@ -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))
}

Expand Down
21 changes: 11 additions & 10 deletions internal/ansible/filter_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestEval(t *testing.T) {
}{
"foo equal foo should pass": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: Equal,
Value: "foo",
},
Expand All @@ -67,7 +68,7 @@ func TestEval(t *testing.T) {
},
"foo equal bar should fail": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: Equal,
Value: "foo",
},
Expand All @@ -76,7 +77,7 @@ func TestEval(t *testing.T) {
},
"foo notequal bar should pass": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: NotEqual,
Value: "foo",
},
Expand All @@ -85,7 +86,7 @@ func TestEval(t *testing.T) {
},
"foo notequal foo should fail": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: NotEqual,
Value: "foo",
},
Expand All @@ -94,7 +95,7 @@ func TestEval(t *testing.T) {
},
"foo endwith oo should pass": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: EndWith,
Value: "foo",
},
Expand All @@ -103,7 +104,7 @@ func TestEval(t *testing.T) {
},
"foo endwith fo should fail": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: EndWith,
Value: "foo",
},
Expand All @@ -112,7 +113,7 @@ func TestEval(t *testing.T) {
},
"foo contains o should pass": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: Contains,
Value: "foo",
},
Expand All @@ -121,7 +122,7 @@ func TestEval(t *testing.T) {
},
"foo contains z should fail": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: Contains,
Value: "foo",
},
Expand All @@ -130,7 +131,7 @@ func TestEval(t *testing.T) {
},
"foo StartWith fo should pass": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: StartWith,
Value: "foo",
},
Expand All @@ -139,7 +140,7 @@ func TestEval(t *testing.T) {
},
"foo StartWith oo should fail": {
given: Filter{
Key: "",
Key: utils.EmptyString,
Op: StartWith,
Value: "foo",
},
Expand Down
15 changes: 8 additions & 7 deletions internal/ansible/inventory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ansible

import (
"github.com/ca-gip/dploy/internal/utils"
"github.com/relex/aini"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -23,36 +24,36 @@ 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"},
},
"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"},
},
"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"},
},
Expand Down Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion internal/ansible/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ansible/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/utils/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 10a78af

Please sign in to comment.