-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathspec.go
83 lines (67 loc) · 1.92 KB
/
spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package spec
import (
"bytes"
"os"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)
const APIGroup = "tanka.dev"
// list of deprecated config keys and their alternatives
// however, they still work and are aliased internally
var deprecated = []depreciation{
{old: "namespace", new: "spec.namespace"},
{old: "server", new: "spec.apiServer"},
{old: "team", new: "metadata.labels.team"},
}
// Parse parses the json `data` into a `v1alpha1.Config` object.
// `name` is the name of the environment
func Parse(data []byte, name string) (*v1alpha1.Config, error) {
v := viper.New()
v.SetConfigType("json")
if err := v.ReadConfig(bytes.NewReader(data)); err != nil {
return nil, err
}
return parse(v, name)
}
// ParseDir parses the given environments `spec.json` into a `v1alpha1.Config`
// object with the name set to the directories name
func ParseDir(baseDir, name string) (*v1alpha1.Config, error) {
fi, err := os.Stat(baseDir)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, errors.New("baseDir is not an directory")
}
v := viper.New()
v.SetConfigName("spec")
v.AddConfigPath(baseDir)
if err := v.ReadInConfig(); err != nil {
return nil, err
}
return parse(v, name)
}
// parse accepts a viper.Viper already loaded with the actual config and
// unmarshals it onto a v1alpha1.Config
func parse(v *viper.Viper, name string) (*v1alpha1.Config, error) {
var errDepr ErrDeprecated
// handle deprecated ksonnet spec
for _, d := range deprecated {
if v.IsSet(d.old) && !v.IsSet(d.new) {
errDepr = append(errDepr, d)
v.Set(d.new, v.Get(d.old))
}
}
config := v1alpha1.New()
if err := v.Unmarshal(config); err != nil {
return nil, errors.Wrap(err, "parsing spec.json")
}
// set the name field
config.Metadata.Name = name
if len(errDepr) == 0 {
return config, nil
}
// return depreciation notes in case any exist as well
return config, errDepr
}