forked from fnproject/cli
/
appfile.go
77 lines (67 loc) · 1.57 KB
/
appfile.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v2"
)
var (
validAppfileNames = [...]string{
"app.yaml",
"app.yml",
"app.json",
}
errUnexpectedFileFormat = errors.New("unexpected file format for function file")
)
type appfile struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
// TODO: Config here is not yet used
Config map[string]string `yaml:"config,omitempty" json:"config,omitempty"`
}
func findAppfile(path string) (string, error) {
for _, fn := range validAppfileNames {
fullfn := filepath.Join(path, fn)
if exists(fullfn) {
return fullfn, nil
}
}
return "", newNotFoundError("could not find app file")
}
func loadAppfile() (*appfile, error) {
fn, err := findAppfile(".")
if err != nil {
return nil, err
}
return parseAppfile(fn)
}
func parseAppfile(path string) (*appfile, error) {
ext := filepath.Ext(path)
switch ext {
case ".json":
return decodeAppfileJSON(path)
case ".yaml", ".yml":
return decodeAppfileYAML(path)
}
return nil, errUnexpectedFileFormat
}
func decodeAppfileJSON(path string) (*appfile, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &appfile{}
err = json.NewDecoder(f).Decode(ff)
return ff, err
}
func decodeAppfileYAML(path string) (*appfile, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not open %s for parsing. Error: %v", path, err)
}
ff := &appfile{}
err = yaml.Unmarshal(b, ff)
return ff, err
}