-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathstages.go
173 lines (143 loc) · 3.03 KB
/
stages.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
package config
import (
"sort"
"github.com/apex/up/internal/validate"
"github.com/pkg/errors"
)
// defaultStages is a list of default stage names.
var defaultStages = []string{
"development",
"staging",
"production",
}
// Stage config.
type Stage struct {
Domain string `json:"domain"`
Zone interface{} `json:"zone"`
Path string `json:"path"`
Cert string `json:"cert"`
Name string `json:"-"`
StageOverrides
}
// IsLocal returns true if the stage represents a local environment.
func (s *Stage) IsLocal() bool {
return s.Name == "development"
}
// IsRemote returns true if the stage represents a remote environment.
func (s *Stage) IsRemote() bool {
return !s.IsLocal()
}
// Validate implementation.
func (s *Stage) Validate() error {
if err := validate.Stage(s.Name); err != nil {
return errors.Wrap(err, ".name")
}
switch s.Zone.(type) {
case bool, string:
return nil
default:
return errors.Errorf(".zone is an invalid type, must be string or boolean")
}
}
// Default implementation.
func (s *Stage) Default() error {
if s.Zone == nil {
s.Zone = true
}
return nil
}
// StageOverrides config.
type StageOverrides struct {
Hooks Hooks `json:"hooks"`
Lambda Lambda `json:"lambda"`
Proxy Relay `json:"proxy"`
}
// Override config.
func (s *StageOverrides) Override(c *Config) {
s.Hooks.Override(c)
s.Lambda.Override(c)
s.Proxy.Override(c)
}
// Stages config.
type Stages map[string]*Stage
// Default implementation.
func (s Stages) Default() error {
// defaults
for _, name := range defaultStages {
if _, ok := s[name]; !ok {
s[name] = &Stage{}
}
}
// assign names
for name, s := range s {
s.Name = name
}
// defaults
for _, s := range s {
if err := s.Default(); err != nil {
return errors.Wrapf(err, "stage %q", s.Name)
}
}
return nil
}
// Validate implementation.
func (s Stages) Validate() error {
for _, s := range s {
if err := s.Validate(); err != nil {
return errors.Wrapf(err, "stage %q", s.Name)
}
}
return nil
}
// List returns configured stages.
func (s Stages) List() (v []*Stage) {
for _, s := range s {
v = append(v, s)
}
return
}
// Domains returns configured domains.
func (s Stages) Domains() (v []string) {
for _, s := range s.List() {
if s.Domain != "" {
v = append(v, s.Domain)
}
}
return
}
// Names returns configured stage names.
func (s Stages) Names() (v []string) {
for _, s := range s.List() {
v = append(v, s.Name)
}
sort.Strings(v)
return
}
// RemoteNames returns configured remote stage names.
func (s Stages) RemoteNames() (v []string) {
for _, s := range s.List() {
if s.IsRemote() {
v = append(v, s.Name)
}
}
sort.Strings(v)
return
}
// GetByDomain returns the stage by domain or nil.
func (s Stages) GetByDomain(domain string) *Stage {
for _, s := range s.List() {
if s.Domain == domain {
return s
}
}
return nil
}
// GetByName returns the stage by name or nil.
func (s Stages) GetByName(name string) *Stage {
for _, s := range s.List() {
if s.Name == name {
return s
}
}
return nil
}