aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.go37
-rw-r--r--config_test.go19
-rw-r--r--handler.go6
-rw-r--r--handler_test.go4
4 files changed, 57 insertions, 9 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
}
diff --git a/config_test.go b/config_test.go
index 10422e9..3bbe7e9 100644
--- a/config_test.go
+++ b/config_test.go
@@ -27,3 +27,22 @@ packages:
assert.Equal(t, pkg, Package{Repo: "github.com/grpc/grpc-go"})
}
+
+func TestNotAlphabetical(t *testing.T) {
+ path, clean := TempFile(t, `
+
+url: google.golang.org
+packages:
+ grpc:
+ repo: github.com/grpc/grpc-go
+ atomic:
+ repo: github.com/uber-go/atomic
+
+`)
+ defer clean()
+
+ _, err := Parse(path)
+ if assert.Error(t, err, "YAML configuration is not listed alphabetically") {
+ assert.Contains(t, err.Error(), "must be alphabetically ordered")
+ }
+}
diff --git a/handler.go b/handler.go
index 09f5999..0a1c807 100644
--- a/handler.go
+++ b/handler.go
@@ -9,7 +9,7 @@ import (
)
// CreateHandler creates a Sally http.Handler
-func CreateHandler(config Config) http.Handler {
+func CreateHandler(config *Config) http.Handler {
router := httprouter.New()
router.RedirectTrailingSlash = false
@@ -29,7 +29,7 @@ func CreateHandler(config Config) http.Handler {
}
type indexHandler struct {
- config Config
+ config *Config
}
func (h indexHandler) Handle(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -54,7 +54,7 @@ var indexTemplate = template.Must(template.New("index").Parse(`
type packageHandler struct {
pkgName string
pkg Package
- config Config
+ config *Config
}
func (h packageHandler) Handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
diff --git a/handler_test.go b/handler_test.go
index 9f2eca8..ce5e290 100644
--- a/handler_test.go
+++ b/handler_test.go
@@ -6,10 +6,10 @@ var config = `
url: go.uber.org
packages:
- yarpc:
- repo: github.com/yarpc/yarpc-go
thriftrw:
repo: github.com/thriftrw/thriftrw-go
+ yarpc:
+ repo: github.com/yarpc/yarpc-go
`