-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
210 lines (182 loc) · 3.53 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
package glx
import (
"bytes"
"io"
"os"
"path/filepath"
"github.com/spf13/afero"
"github.com/spf13/viper"
api_pb "github.com/srvc/glx/api"
)
type Config struct {
Root string
Projects []*Project
}
type Project struct {
Name string
Apps []*App
}
type App struct {
Name string
Hostname string
Local *LocalApp
Docker *DockerApp
Kubernetes *KubernetesApp
}
func (a *App) Pb() *api_pb.App {
pb := &api_pb.App{
Name: a.Name,
Hostname: a.Hostname,
}
switch {
case a.Local != nil:
pb.Type = api_pb.App_TYPE_LOCAL
for name, port := range a.Local.PortEnv {
pb.Ports = append(pb.Ports, &api_pb.App_Port{
Network: api_pb.App_Port_TCP, // TODO
ExposedPort: uint32(port),
Env: name,
})
}
case a.Docker != nil:
pb.Type = api_pb.App_TYPE_DOCKER
for name, port := range a.Docker.Run.PortEnv {
pb.Ports = append(pb.Ports, &api_pb.App_Port{
Network: api_pb.App_Port_TCP, // TODO
ExposedPort: uint32(port),
Env: name,
})
}
case a.Kubernetes != nil:
pb.Type = api_pb.App_TYPE_KUBERNETES
// TODO: not yet supported
}
return pb
}
type LocalApp struct {
PortEnv map[string]Port `mapstructure:"port_env"`
Cmd []string
Path string
}
type DockerApp struct {
Path string
Build struct {
Dockerfile string
Target string
}
Run struct {
Cmd [][]string
Volumes []string
PortEnv map[string]Port `mapstructure:"port_env"`
}
}
type KubernetesApp struct {
Context string
Namespace string
Labels map[string]string
Ports map[Port]Port
}
func (c *Config) FindProject(name string) *Project {
for _, p := range c.Projects {
if p.Name == name {
return p
}
}
return nil
}
var configDir = filepath.Join(os.Getenv("HOME"), ".config", "glx")
func NewViper(fs afero.Fs) *viper.Viper {
v := viper.New()
v.SetFs(fs)
v.AddConfigPath(configDir)
v.SetConfigName("glx")
return v
}
func NewFs() afero.Fs {
return afero.NewOsFs()
}
type UnionFS struct {
afero.Fs
}
func (fs *UnionFS) MergeConfigFiles() error {
var matches []string
for _, ext := range []string{"yaml", "yml"} {
resp, err := afero.Glob(fs, filepath.Join(configDir, "glx.*."+ext))
if err != nil {
return err
}
matches = append(matches, resp...)
}
if len(matches) == 0 {
return nil
}
path := filepath.Join(configDir, "glx.yml")
if ok, _ := afero.Exists(fs, path); !ok {
path = filepath.Join(configDir, "glx.yaml")
}
out, err := fs.OpenFile(path, os.O_RDWR, 0644)
buf := new(bytes.Buffer)
switch {
case err == nil:
_, err = io.Copy(buf, out)
if err != nil {
return err
}
err = out.Truncate(0)
if err != nil {
return err
}
_, err = out.Seek(0, 0)
if err != nil {
return err
}
case os.IsNotExist(err):
out, err = fs.Create(path)
if err != nil {
return err
}
default:
return err
}
defer out.Close()
for _, match := range matches {
in, err := fs.Open(match)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
}
_, err = io.Copy(out, buf)
if err != nil {
return err
}
return nil
}
func NewUnionFs(baseFs afero.Fs) (*UnionFS, error) {
fs := afero.NewCopyOnWriteFs(
afero.NewReadOnlyFs(baseFs),
afero.NewMemMapFs(),
)
ufs := &UnionFS{Fs: fs}
err := ufs.MergeConfigFiles()
if err != nil {
return nil, err
}
return ufs, nil
}
func NewConfig(v *viper.Viper) (*Config, error) {
err := v.ReadInConfig()
if err != nil {
return nil, err
}
var cfg Config
err = v.Unmarshal(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}