Skip to content

Commit

Permalink
v0.6.0 - Added env flag to pull in all environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-szabo committed Jul 19, 2019
1 parent f9df041 commit b227bfe
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
35 changes: 28 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

type FlagsType struct {
Env bool
File string
String string
List string
Expand All @@ -34,8 +35,8 @@ func CheckArgs(cmd *cobra.Command, args []string) (err error) {
return
}

if inputFlags.File == "" && inputFlags.String == "" && inputFlags.List == "" && inputFlags.Map == "" {
return errors.New("at least one of --file, --string, --list or --map is required")
if inputFlags.File == "" && inputFlags.String == "" && inputFlags.List == "" && inputFlags.Map == "" && ! inputFlags.Env {
return errors.New("at least one of --file, --string, --list --env or --map is required")
}

for _, item := range strings.Split(args[0], ",") {
Expand Down Expand Up @@ -150,11 +151,26 @@ func sub(a interface{}, b interface{}) (result uint64, err error) {
}

func RunRoot(cmd *cobra.Command, args []string) (output string, err error) {
// Priorities least to most: env, file, string, list, map

// Read file
if inputFlags.File == "" {
dictionary = make(map[string]interface{})
} else {
dictionary = make(map[string]interface{})

// Read --env
if inputFlags.Env {
for _, envVar := range os.Environ() {
equals := strings.Index(envVar,"=")
if equals < 1 || equals==len(envVar) {
// Invalid string
continue
}
name := envVar[0:equals]
value := envVar[equals+1:]
dictionary[name] = value
}
}

// Read --file
if inputFlags.File != "" {
viper.SetConfigFile(inputFlags.File)
err = viper.ReadInConfig()
if err != nil {
Expand All @@ -168,7 +184,11 @@ func RunRoot(cmd *cobra.Command, args []string) (output string, err error) {
return
}
}
dictionary = viper.AllSettings()
for k, v := range viper.AllSettings() {
if dictionary[k] == nil {
dictionary[k] = v
}
}
}

// Read --string
Expand Down Expand Up @@ -352,6 +372,7 @@ Source and documentation is available at https://github.com/freshautomations/ste
pflag.StringVarP(&inputFlags.Map, "map", "m", "", "Comma-separated list of environment variable names that contain comma-separated strings of key=value pairs")
pflag.StringVarP(&inputFlags.Extension, "extension", "t", ".template", "Extension for template files when template input or output is a directory. Default: .template")
pflag.BoolVarP(&inputFlags.All, "all", "a", false, "Consider all files in a directory templates, regardless of extension.")
pflag.BoolVarP(&inputFlags.Env, "env", "e", false, "Import all environment variables for templates as strings.")
_ = rootCmd.MarkFlagFilename("file")

return rootCmd.Execute()
Expand Down
81 changes: 78 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Welcome to custom functions demonstration.
* mid "abcdefg" 3 2: de
`

var testenvresult = `--env test
* From the env.var hellotest: helloresult
* From the env.var ANOTHER_TEST: ANOTHER_RESULT
`

// rootDir has to be ".." for CircleCI to work correctly.
var rootDir = ".."

Expand All @@ -52,17 +58,15 @@ func TestCheckArgs(t *testing.T) {
assert.Nil(t, CheckArgs(cmd, []string{filepath.Join(rootDir, "test_templates", "test.template")}), "parameter check")
}

func TestRunRoot(t *testing.T) {
func TestRunRootSimpleFiles(t *testing.T) {
cmd := &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Version: defaults.Version,
}

var resultfile []byte
var err error

inputFlags.Extension = ".template"

// JSON test
Expand Down Expand Up @@ -114,6 +118,19 @@ func TestRunRoot(t *testing.T) {
inputFlags.List = ""
inputFlags.Map = ""

}

func TestRunRootOutputDirectory(t *testing.T) {
cmd := &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Version: defaults.Version,
}
var resultfile []byte
var err error
inputFlags.Extension = ".template"

// Output is a directory test
inputFlags.Output = filepath.Join(rootDir, "outputdir1")
inputFlags.File = filepath.Join(rootDir, "test_dictionaries","test.json")
Expand All @@ -127,6 +144,19 @@ func TestRunRoot(t *testing.T) {
_ = os.Remove(filepath.Join(inputFlags.Output, "test"))
_ = os.Remove(inputFlags.Output)

}

func TestRunRootInputDirectory(t *testing.T) {
cmd := &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Version: defaults.Version,
}
var resultfile []byte
var err error
inputFlags.Extension = ".template"

// Input template is a folder, output is a directory test
inputFlags.Output = filepath.Join(rootDir, "outputdir2")
inputFlags.File = filepath.Join(rootDir, "test_dictionaries","test.json")
Expand Down Expand Up @@ -186,3 +216,48 @@ func TestCustomFunctions(t *testing.T) {
_ = os.Remove(inputFlags.Output)

}

func TestEnvParam(t *testing.T) {
cmd := &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Version: defaults.Version,
}

var resultfile []byte
var err error

inputFlags.Extension = ".template"
inputFlags.Env = true
os.Setenv("hellotest","helloresult")
os.Setenv("ANOTHER_TEST", "ANOTHER_RESULT")

// JSON test
inputFlags.Output = filepath.Join(rootDir, "env_jsonresult.tmp")
_, err = RunRoot(cmd, []string{filepath.Join(rootDir, "test_templates2","env.template")})
assert.Nil(t, err, "unexpected error")
resultfile, err = ioutil.ReadFile(inputFlags.Output)
assert.Nil(t, err, "unexpected error")
assert.Equal(t, testenvresult, string(resultfile), "unexpected result")
_ = os.Remove(inputFlags.Output)

// TOML test
inputFlags.Output = filepath.Join(rootDir, "env_tomlresult.tmp")
_, err = RunRoot(cmd, []string{filepath.Join(rootDir, "test_templates2","env.template")})
assert.Nil(t, err, "unexpected error")
resultfile, err = ioutil.ReadFile(inputFlags.Output)
assert.Nil(t, err, "unexpected error")
assert.Equal(t, testenvresult, string(resultfile), "unexpected result")
_ = os.Remove(inputFlags.Output)

// YAML test
inputFlags.Output = filepath.Join(rootDir, "env_yamlresult.tmp")
_, err = RunRoot(cmd, []string{filepath.Join(rootDir, "test_templates2","env.template")})
assert.Nil(t, err, "unexpected error")
resultfile, err = ioutil.ReadFile(inputFlags.Output)
assert.Nil(t, err, "unexpected error")
assert.Equal(t, testenvresult, string(resultfile), "unexpected result")
_ = os.Remove(inputFlags.Output)

}
2 changes: 1 addition & 1 deletion defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package defaults

// Application version
const Version = "0.5.0"
const Version = "0.6.0"
4 changes: 4 additions & 0 deletions test_templates2/env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--env test

* From the env.var hellotest: {{ .hellotest }}
* From the env.var ANOTHER_TEST: {{ .ANOTHER_TEST }}

0 comments on commit b227bfe

Please sign in to comment.