-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
71 lines (60 loc) · 1.21 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
package main
import (
"errors"
"fmt"
"gopkg.in/yaml.v1"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Config struct {
Cmd string
Dockerfile string
Image string
Services map[string]Service
}
type Service struct {
Env []string
Format string
Image string
Port string
Hooks map[string][]string
}
func ConfigFromFile() (*Config, error) {
pwd, _ := os.Getwd()
_, temakiYml, err := GetTemakiYml(pwd)
if err != nil {
return nil, err
}
config, err := ioutil.ReadAll(temakiYml)
if err != nil {
return nil, err
}
conf := Config{}
if err := yaml.Unmarshal(config, &conf); err != nil {
return nil, err
}
if len(os.Args) > 1 {
conf.Cmd = strings.Join(os.Args[1:], " ")
}
return &conf, nil
}
func GetTemakiYml(basePath string) (string, *os.File, error) {
fullPath := filepath.Join(basePath, "temaki.yml")
stat, err := os.Stat(fullPath)
if err != nil {
if os.IsNotExist(err) && basePath != "/" {
return GetTemakiYml(filepath.Dir(basePath))
}
return "", nil, err
}
if stat.IsDir() {
return basePath, nil, errors.New(fmt.Sprintf("%s is a directory.", fullPath))
}
file, err := os.Open(fullPath)
if err != nil {
return "", nil, err
}
return basePath, file, nil
}