-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
215 lines (185 loc) · 4.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package captain // import "github.com/harbur/captain"
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
// Config represents the information stored at captain.yml. It keeps information about images and unit tests.
type Config interface {
FilterConfig(filter string) bool
GetApp(app string) App
GetApps() []App
}
type configV1 struct {
Build build
Test map[string][]string
Images []string
Root []string
}
type build struct {
Images map[string]string
}
type config map[string]App
var configOrder *yaml.MapSlice
// App struct
type App struct {
Build string
Image string
Pre []string
Post []string
Test []string
Build_arg map[string]string
}
// configFile returns the file to read the config from.
// If the --config option was given,
// it will only use the given file.
func configFile(path string) string {
if len(path) > 0 {
return path
}
return "captain.yml"
}
// readConfig will read the config file
// and return the created config.
func readConfig(filename string) *config {
data, err := ioutil.ReadFile(filename)
os.Chdir(filepath.Dir(filename))
if err != nil {
panic(StatusError{err, 74})
}
return unmarshal(data)
}
// displaySyntaxError will display more information
// such as line and error type given an error and
// the data that was unmarshalled.
// Thanks to https://github.com/markpeek/packer/commit/5bf33a0e91b2318a40c42e9bf855dcc8dd4cdec5
func displaySyntaxError(data []byte, syntaxError error) (err error) {
syntax, ok := syntaxError.(*json.SyntaxError)
if !ok {
err = syntaxError
return
}
newline := []byte{'\x0a'}
space := []byte{' '}
start, end := bytes.LastIndex(data[:syntax.Offset], newline)+1, len(data)
if idx := bytes.Index(data[start:], newline); idx >= 0 {
end = start + idx
}
line, pos := bytes.Count(data[:start], newline)+1, int(syntax.Offset)-start-1
err = fmt.Errorf("\nError in line %d: %s \n%s\n%s^", line, syntaxError, data[start:end], bytes.Repeat(space, pos))
return
}
// unmarshal converts either JSON
// or YAML into a config object.
func unmarshal(data []byte) *config {
var configV1 *configV1
res := yaml.Unmarshal(data, &configV1)
if len(configV1.Build.Images) > 0 {
err("Old %s format detected! Please check the https://github.com/harbur/captain how to upgrade", "captain.yml")
os.Exit(-1)
}
var config *config
res = yaml.Unmarshal(data, &config)
if res != nil {
res = displaySyntaxError(data, res)
err("%s", res)
os.Exit(InvalidCaptainYML)
}
// We re-import it as MapSlice to keep order of apps
res = yaml.Unmarshal(data, &configOrder)
if res != nil {
res = displaySyntaxError(data, res)
err("%s", res)
os.Exit(InvalidCaptainYML)
}
return config
}
// NewConfig returns a new Config instance based on the reading the captain.yml
// file at path.
// Containers will be ordered so that they can be
// brought up and down with Docker.
func NewConfig(namespace, path string, forceOrder bool) Config {
var conf *config
f := configFile(path)
if _, err := os.Stat(f); err == nil {
conf = readConfig(f)
}
if conf == nil {
info("No configuration found %v - inferring values", configFile(path))
autoconf := make(config)
conf = &autoconf
dockerfiles := getDockerfiles(namespace)
for build, image := range dockerfiles {
autoconf[image] = App{Build: build, Image: image}
}
}
var err error
if err != nil {
panic(StatusError{err, 78})
}
return conf
}
// GetApps returns a list of Apps
func (c *config) GetApps() []App {
var cc = *c
var apps []App
if configOrder != nil {
for _, v := range *configOrder {
if val, ok := cc[v.Key.(string)]; ok {
apps = append(apps, val)
}
}
} else {
for _, v := range *c {
apps = append(apps, v)
}
}
return apps
}
func (c *config) FilterConfig(filter string) bool {
if filter != "" {
res := false
for key := range *c {
if key == filter {
res = true
} else {
delete(*c, key)
}
}
return res
}
return true
}
// GetApp returns App configuration
func (c *config) GetApp(app string) App {
for key, k := range *c {
if key == app {
return k
}
}
return App{}
}
// Global list, how can I pass it to the visitor pattern?
var imagesMap = make(map[string]string)
func getDockerfiles(namespace string) map[string]string {
filepath.Walk(".", visit(namespace))
return imagesMap
}
func visit(namespace string) filepath.WalkFunc {
return func(path string, f os.FileInfo, err error) error {
// Filename is "Dockerfile" or has "Dockerfile." prefix and is not a directory
if (f.Name() == "Dockerfile" || strings.HasPrefix(f.Name(), "Dockerfile.")) && !f.IsDir() {
// Get Parent Dirname
absolutePath, _ := filepath.Abs(path)
var image = strings.ToLower(filepath.Base(filepath.Dir(absolutePath)))
imagesMap[path] = namespace + "/" + image + strings.ToLower(filepath.Ext(path))
debug("Located %s will be used to create %s", path, imagesMap[path])
}
return nil
}
}