forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
79 lines (71 loc) · 2.04 KB
/
config.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
package faas
import (
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// ConfigFile is the name of the config's serialized form.
const ConfigFile = ".faas.yaml"
// Config represents the serialized state of a Function's metadata.
// See the Function struct for attribute documentation.
type config struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Runtime string `yaml:"runtime"`
Image string `yaml:"image"`
Trigger string `yaml:"trigger"`
// Add new values to the toConfig/fromConfig functions.
}
// newConfig returns a Config populated from data serialized to disk if it is
// available. Errors are returned if the path is not valid, if there are
// errors accessing an extant config file, or the contents of the file do not
// unmarshall. A missing file at a valid path does not error but returns the
// empty value of Config.
func newConfig(root string) (c config, err error) {
filename := filepath.Join(root, ConfigFile)
if _, err = os.Stat(filename); err != nil {
// do not consider a missing config file an error. Just return.
if os.IsNotExist(err) {
err = nil
}
return
}
bb, err := ioutil.ReadFile(filename)
if err != nil {
return
}
err = yaml.Unmarshal(bb, &c)
return
}
// fromConfig returns a Function populated from config.
// Note that config does not include ancillary fields not serialized, such as Root.
func fromConfig(c config) (f Function) {
return Function{
Name: c.Name,
Namespace: c.Namespace,
Runtime: c.Runtime,
Image: c.Image,
Trigger: c.Trigger,
}
}
// toConfig serializes a Function to a config object.
func toConfig(f Function) config {
return config{
Name: f.Name,
Namespace: f.Namespace,
Runtime: f.Runtime,
Image: f.Image,
Trigger: f.Trigger,
}
}
// writeConfig for the given Function out to disk at root.
func writeConfig(f Function) (err error) {
path := filepath.Join(f.Root, ConfigFile)
c := toConfig(f)
var bb []byte
if bb, err = yaml.Marshal(&c); err != nil {
return
}
return ioutil.WriteFile(path, bb, 0644)
}