Skip to content

Commit

Permalink
- Added basic schema validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Ahl committed Jun 3, 2019
1 parent a516741 commit 000938a
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 8 deletions.
5 changes: 4 additions & 1 deletion cmd/centry/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
func TestMain(t *testing.T) {
g := Goblin(t)

// Esuring the workdir is the root of the repo
os.Chdir("../../")

g.Describe("runtime", func() {
g.It("returns error when manifest fails to load", func() {
context := NewContext(CLI, io.Headless())
Expand Down Expand Up @@ -264,7 +267,7 @@ func execCentry(source string, quiet bool) *execResult {
source = fmt.Sprintf("--quiet %s", source)
}
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime(strings.Split(fmt.Sprintf("../../test/data/main_test.yaml %s", source), " "), context)
runtime, err := NewRuntime(strings.Split(fmt.Sprintf("test/data/main_test.yaml %s", source), " "), context)
if err != nil {
exitCode = 1
runtimeErr = err
Expand Down
4 changes: 4 additions & 0 deletions examples/centry/centry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ commands:
- name: get
path: commands/get.sh
description: Gets resources

- name: up
path: commands/updown.sh
description: Upserts resources
annotations:
centry.api/serve: "true"

- name: down
path: commands/updown.sh
description: Destroys resources
annotations:
centry.api/serve: "false"

- name: rotate
path: commands/rotate.sh
description: Rotating secrets, hosts etc.

- name: interactive
path: commands/interactive.sh
description: Reading from stdin
Expand Down
23 changes: 18 additions & 5 deletions internal/pkg/config/manifest.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package config

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/kristofferahl/go-centry/internal/pkg/cmd"
yaml "gopkg.in/yaml.v2"
yaml2 "gopkg.in/yaml.v2"
)

// Manifest defines the structure of a manifest
Expand Down Expand Up @@ -54,11 +56,11 @@ type LogConfig struct {
}

// LoadManifest reads, parses and returns a manifest root object
func LoadManifest(path string) (*Manifest, error) {
mp, _ := filepath.Abs(path)
func LoadManifest(manifest string) (*Manifest, error) {
mp, _ := filepath.Abs(manifest)

if _, err := os.Stat(mp); os.IsNotExist(err) {
return nil, fmt.Errorf("The first argument must be a path to a valid manfest file (%s)", path)
return nil, fmt.Errorf("The first argument must be a path to a valid manfest file (%s)", manifest)
}

bs, err := readManifestFile(mp)
Expand All @@ -71,6 +73,17 @@ func LoadManifest(path string) (*Manifest, error) {
return nil, err
}

jbs, err := yaml.YAMLToJSON(bs)
if err != nil {
return nil, err
}

r := bytes.NewReader(jbs)
err = validateManifestYaml("bindata://schemas/manifest.json", r)
if err != nil {
return nil, err
}

m.Path = mp
m.BasePath = filepath.Dir(mp)

Expand All @@ -87,7 +100,7 @@ func readManifestFile(filename string) ([]byte, error) {

func parseManifestYaml(bs []byte) (*Manifest, error) {
m := Manifest{}
err := yaml.Unmarshal(bs, &m)
err := yaml2.Unmarshal(bs, &m)
if err != nil {
return nil, fmt.Errorf("Failed to parse manifest yaml. %v", err)
}
Expand Down
10 changes: 9 additions & 1 deletion internal/pkg/config/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ import (
func TestMain(t *testing.T) {
g := Goblin(t)

// Esuring the workdir is the root of the repo
os.Chdir("../../../")

g.Describe("LoadManifest", func() {
g.It("returns error for invalid manifest file", func() {
_, err := LoadManifest("test/data/invalid.yaml")
g.Assert(err != nil).IsTrue("expected validation error")
})

g.It("returns manifest when file is found", func() {
path := "../../../test/data/main_test.yaml"
path := "test/data/main_test.yaml"
absPath, _ := filepath.Abs(path)
basePath := filepath.Dir(absPath)

Expand Down
273 changes: 273 additions & 0 deletions internal/pkg/config/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 000938a

Please sign in to comment.