/
config.go
189 lines (164 loc) · 4.31 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
package pkg
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
"gopkg.in/yaml.v2"
)
// The name of the config file that envsnap reads from.
const configFile = ".envsnap"
var (
// Version 1 of the envsnap configuration file scheme.
configV1 = 1
)
// RenderConfig defines an interface for configuration sections for envsnap
// which can be rendered into results.
type RenderConfig interface {
Render() (Result, error)
}
// VersionedConfig is an intermediary struct which is used to load the
// version information from configuration YAML. This allows envsnap to
// determine the version of the configuration, and thus the correct struct
// to load the configuration into.
type VersionedConfig struct {
Version *int `yaml:"version"`
}
// EnvsnapConfig defines an interface which all versions of the envsnap
// configuration should implement.
type EnvsnapConfig interface {
All() []RenderConfig
Render() (EnvsnapResult, error)
}
// LoadConfig loads the configuration for envsnap to render.
//
// If no path is specified, it assumes the configuration is in the current
// working directory. The path may also be a reference to a GitHub repository
// containing the configuration, in the form of "github.com/<owner>/<repo>[@<ref>]"
// where the <ref> may be a branch name, tag, or commit.
func LoadConfig(path string) (EnvsnapConfig, error) {
var (
err error
data []byte
)
// If no path is specified, assume the current working directory.
if path == "" {
path, err = filepath.Abs(filepath.Join(".", configFile))
if err != nil {
return nil, err
}
}
// If the path starts with "github.com/", assume that it is referencing
// a remote GitHub repo. This will cause the config to be loaded from
// .envsnap file in that repo, instead of locally.
if strings.HasPrefix(path, "github.com/") {
parts := strings.Split(path, "@")
var ref string
url := parts[0]
if len(parts) > 1 {
ref = parts[1]
}
parts = strings.Split(url, "/")
if len(parts) != 3 {
return nil, ErrInvalidGithubURL
}
user := parts[1]
repo := parts[2]
ghc := github.NewClient(nil)
content, _, _, err := ghc.Repositories.GetContents(
context.Background(),
user,
repo,
configFile,
&github.RepositoryContentGetOptions{
Ref: ref,
},
)
if err != nil {
return nil, err
}
ctnt, err := content.GetContent()
if err != nil {
return nil, err
}
data = []byte(ctnt)
} else {
// Load from file
// Attempt to load the config from file. First, check that
// the specified path even exists.
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, ErrNoConfig
}
// The file exists. Read from it and figure out which version
// of the configuration it is.
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
data = contents
}
// ----
v := &VersionedConfig{}
if err := yaml.Unmarshal(data, v); err != nil {
return nil, err
}
if v.Version == nil {
return nil, ErrNoConfigVersion
}
switch *v.Version {
case configV1:
cfg := &V1EnvsnapConfig{}
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, err
}
return cfg, nil
default:
return nil, ErrInvalidConfigVersion
}
}
// V1EnvsnapConfig contains all the data for the environment snapshot.
type V1EnvsnapConfig struct {
Environment EnvConfig `yaml:"environment,omitempty"`
Exec ExecConfig `yaml:"exec,omitempty"`
Golang GolangConfig `yaml:"go,omitempty"`
Python PythonConfig `yaml:"python,omitempty"`
System SystemConfig `yaml:"system,omitempty"`
}
// All returns all of the configuration components for the v1 envsnap config.
func (c V1EnvsnapConfig) All() []RenderConfig {
return []RenderConfig{
c.System,
c.Environment,
c.Exec,
c.Python,
c.Golang,
}
}
// Render each configured source into its corresponding v1 result.
func (c V1EnvsnapConfig) Render() (EnvsnapResult, error) {
var err error
v1 := NewV1EnvsnapResult()
v1.System, err = c.System.Render()
if err != nil {
return nil, err
}
v1.Environment, err = c.Environment.Render()
if err != nil {
return nil, err
}
v1.Exec, err = c.Exec.Render()
if err != nil {
return nil, err
}
v1.Python, err = c.Python.Render()
if err != nil {
return nil, err
}
v1.Golang, err = c.Golang.Render()
if err != nil {
return nil, err
}
return &v1, nil
}