-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add -spec flag to support specification file consisting of multiple d…
…esired state files state files, add priority override
- Loading branch information
1 parent
321afde
commit dd807d6
Showing
7 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
stateFiles: | ||
- path: examples/example.yaml | ||
priority: -10 | ||
- path: examples/minimal-example.yaml | ||
- path: examples/minimal-example.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type SpecConfig struct { | ||
Path string `yaml:"path"` | ||
Priority int `yaml:"priority"` | ||
} | ||
|
||
type StateFiles struct { | ||
StateFiles []SpecConfig `yaml:"stateFiles"` | ||
} | ||
|
||
// fromYAML reads a yaml file and decodes it to a state type. | ||
// parser which throws an error if the YAML file is not valid. | ||
func (pc *StateFiles) specFromYAML(file string) error { | ||
rawYamlFile, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
log.Errorf("specFromYaml %v %v", file, err) | ||
return err | ||
} | ||
|
||
yamlFile := string(rawYamlFile) | ||
|
||
if err = yaml.UnmarshalStrict([]byte(yamlFile), pc); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkSpecValid(f string) error { | ||
if err := isValidFile(f, []string{"yaml", "yml"}); err != nil { | ||
// exit | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *state) patchPriority(priority int) error { | ||
for app, _ := range s.Apps { | ||
if s.Apps[app].Priority > priority { | ||
s.Apps[app].Priority = priority | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package app | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_specFromYAML(t *testing.T) { | ||
type args struct { | ||
file string | ||
s *StateFiles | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "test case 1 -- Valid YAML", | ||
args: args{ | ||
file: "../../examples/example-spec.yaml", | ||
s: new(StateFiles), | ||
}, | ||
want: true, | ||
}, { | ||
name: "test case 2 -- Invalid Yaml", | ||
args: args{ | ||
file: "../../tests/Invalid_example_spec.yaml", | ||
s: new(StateFiles), | ||
}, | ||
want: false, | ||
}, | ||
} | ||
|
||
teardownTestCase := setupStateFileTestCase(t) | ||
defer teardownTestCase(t) | ||
for _, tt := range tests { | ||
// os.Args = append(os.Args, "-f ../../examples/example.yaml") | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.args.s.specFromYAML(tt.args.file) | ||
if err != nil { | ||
t.Log(err) | ||
} | ||
|
||
got := err == nil | ||
if got != tt.want { | ||
t.Errorf("specFromYaml() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
stateFiles: | ||
name1: invalid/example.yaml | ||
name2: invalid/minimal-example.yaml |