Skip to content

Commit

Permalink
Adds back --config-file and adds tests around initConfigFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowiem committed Jun 20, 2020
1 parent 35adc82 commit cf91dc6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ Easily run one-off tasks against an ECS Task Definition.

- [x] Support basic CLI usage
- [x] Support local config file
- [ ] Refactor root.go to be cleaner.
- [ ] Add more tests
- [] Support `--dryrun` Flag
- [] Support selection of resources similar to gossm (cluster, task def, task def version, etc etc)
- [x] Support `--dryrun` Flag
- [x] Add more tests
- [] Support log group / stream tailing of initiated task
- [] Support selection of resources similar to gossm (cluster, task def, task def version, etc etc)
- [] Support validation of given params: cluster, definition name, revision, subnet ID, SG ID, ect.
- [] Support EC2 usage.
35 changes: 29 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func (v VersionInfo) String() string {
v.BuiltBy)
}

var cfgFile string

var log = logrus.New()

var fs = afero.NewOsFs()
Expand Down Expand Up @@ -116,8 +114,7 @@ func init() {
rootCmd.PersistentFlags().Bool("version", false, "version output")

// Config File Flags
// TODO: Add this back at another time
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config-file", "", "config file (default is $PWD/escrun.yml)")
rootCmd.PersistentFlags().String("config-file", "", "config file to read config entries from (default is $PWD/escrun.yml)")
rootCmd.PersistentFlags().String("config", "default", "config entry to read in the config file (default is 'default')")
rootCmd.PersistentFlags().Bool("dry-run", false, "dry-run your ecsrun execution to check config (default is false)")

Expand Down Expand Up @@ -232,7 +229,17 @@ func initAwsSession(profile string) (*session.Session, error) {
}

func initConfigFile() error {
filename, err := findConfigFile()
var filename string
var err error

cfgFile := viper.GetString("config-file")

if cfgFile == "" {
filename, err = findConfigFile()
} else {
filename, err = findCustomConfigFile(cfgFile)
}

if err != nil {
return err
}
Expand All @@ -252,21 +259,37 @@ func initConfigFile() error {
log.Debug("Full config file contents: ", config)

configEntry := viper.GetString("config")

log.Debug("Config entry: ", configEntry, " result: ", config[configEntry])
if err = viper.MergeConfigMap(config[configEntry]); err != nil {
return err
}

return nil
}

func findCustomConfigFile(filename string) (string, error) {
log.Info("filename: ", filename)
exists, err := afero.Exists(fs, filename)
if err != nil {
return "", err
}

if exists {
return filename, nil
}

return "", errors.New("custom config file not found")
}

func findConfigFile() (string, error) {
supportedExts := []string{"yaml", "yml"}

for _, extension := range supportedExts {
filename := filepath.Join(".", "ecsrun"+"."+extension)
exists, err := afero.Exists(fs, filename)
if err != nil {
log.Fatal("Failed to check if file exists: ", err)
return "", err
}

if exists {
Expand Down
13 changes: 11 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,17 @@ func TestConfigFile(t *testing.T) {
assert := assert.New(t)
setup()

t.Skip("TODO")
assert.Equal(true, true)
setRequired()
viper.Set("config", "default")
viper.Set("config-file", "../example/configs/ecsrun.yaml")

err := initConfigFile()

assert.Nil(err)
viper.Debug()
assert.Equal("test-cluster", viper.Get("cluster"))
assert.Equal("test-task", viper.Get("task"))
assert.Equal("sg1", viper.Get("security-group"))

teardown()
}
Expand Down
4 changes: 2 additions & 2 deletions ecsrun.yaml → example/configs/ecsrun.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default: &default
cluster: "testing"
task: "testing2"
cluster: "test-cluster"
task: "test-task"
security-group: "sg1"

custom:
Expand Down

0 comments on commit cf91dc6

Please sign in to comment.