aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorGravatar Garrett Squire <garrettsquire@gmail.com> 2016-10-27 16:39:01 -0700
committerGravatar Grayson Koonce <grayson.koonce@gmail.com> 2016-10-27 16:39:01 -0700
commit81051d9e2d5fa34552eccf25801d137b5486c804 (patch)
tree58a529445f3eaa5d80cf2d6f0cc4428e57df0eb4 /config.go
parentac8374fd17e30fca9a7773a2a6f690a7ea4d2ec9 (diff)
downloadsally-81051d9e2d5fa34552eccf25801d137b5486c804.tar.gz
sally-81051d9e2d5fa34552eccf25801d137b5486c804.tar.zst
sally-81051d9e2d5fa34552eccf25801d137b5486c804.zip
Enforce alphabetical ordering of packages in YAML config (#22)
Diffstat (limited to 'config.go')
-rw-r--r--config.go37
1 files changed, 33 insertions, 4 deletions
diff --git a/config.go b/config.go
index 5071f37..735b147 100644
--- a/config.go
+++ b/config.go
@@ -1,7 +1,9 @@
package main
import (
+ "fmt"
"io/ioutil"
+ "sort"
"gopkg.in/yaml.v2"
)
@@ -17,18 +19,45 @@ type Package struct {
Repo string `yaml:"repo"`
}
+// ensureAlphabetical checks that the packages are listed alphabetically in the configuration.
+func ensureAlphabetical(data []byte) bool {
+ // A yaml.MapSlice perservers ordering of keys: https://godoc.org/gopkg.in/yaml.v2#MapSlice
+ var c struct {
+ Packages yaml.MapSlice `yaml:"packages"`
+ }
+
+ if err := yaml.Unmarshal(data, &c); err != nil {
+ return false
+ }
+
+ packageNames := make([]string, 0, len(c.Packages))
+ for _, v := range c.Packages {
+ name, ok := v.Key.(string)
+ if !ok {
+ return false
+ }
+ packageNames = append(packageNames, name)
+ }
+
+ return sort.StringsAreSorted(packageNames)
+}
+
// Parse takes a path to a yaml file and produces a parsed Config
-func Parse(path string) (Config, error) {
+func Parse(path string) (*Config, error) {
var c Config
data, err := ioutil.ReadFile(path)
if err != nil {
- return c, err
+ return nil, err
}
if err := yaml.Unmarshal(data, &c); err != nil {
- return c, err
+ return nil, err
+ }
+
+ if !ensureAlphabetical(data) {
+ return nil, fmt.Errorf("packages in %s must be alphabetically ordered", path)
}
- return c, err
+ return &c, err
}