/
meta.go
172 lines (150 loc) · 4.94 KB
/
meta.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
package command
import (
"encoding/json"
"fmt"
"os"
"regexp"
"github.com/TimeIncOSS/ape-dev-rt/clippy"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate/backends"
"github.com/TimeIncOSS/ape-dev-rt/deploymentstate/schema"
"github.com/TimeIncOSS/ape-dev-rt/rt"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/ttacon/chalk"
)
var dateLayout = "Mon Jan 2 15:04:05 -0700 2006"
type chalkFunc func(string) string
type colours struct {
boldGreen chalkFunc
boldWhite chalkFunc
boldYellow chalkFunc
boldRed chalkFunc
boldBlue chalkFunc
red chalkFunc
green chalkFunc
}
var colour = &colours{
boldGreen: chalk.Green.NewStyle().WithTextStyle(chalk.Bold).Style,
boldWhite: chalk.White.NewStyle().WithTextStyle(chalk.Bold).Style,
boldYellow: chalk.Yellow.NewStyle().WithTextStyle(chalk.Bold).Style,
boldRed: chalk.Red.NewStyle().WithTextStyle(chalk.Bold).Style,
boldBlue: chalk.Blue.NewStyle().WithTextStyle(chalk.Bold).Style,
red: chalk.Red.NewStyle().Style,
green: chalk.Green.NewStyle().Style,
}
type ApplicationTemplateVars struct {
AwsAccountId, Environment, AppName string
}
type VersionTemplateVars struct {
AwsAccountId, Environment, AppName string
}
func turnOffColours(c *colours) {
c.boldGreen = chalk.Reset.Style
c.boldWhite = chalk.Reset.Style
c.boldYellow = chalk.Reset.Style
c.boldRed = chalk.Reset.Style
c.boldBlue = chalk.Reset.Style
c.red = chalk.Reset.Style
c.green = chalk.Reset.Style
}
func BeginApplicationOperation(env, appName string, ds *deploymentstate.DeploymentState, yesOverride ...bool) (*schema.ApplicationData, bool, error) {
yes := false // use this varaible to control output of creation prompt
if len(yesOverride) > 0 {
yes = yesOverride[0]
}
app, err := ds.GetApplication(appName)
if err != nil {
_, ok := err.(*backends.AppNotFound)
if ok {
note := fmt.Sprintf("Application %q doesn't exist in %q, do you want to create it?", appName, env)
isSensitive := isEnvironmentSensitive(env)
out, confirmed, _ := clippy.BoolPrompt(note, yes, isSensitive, func() (interface{}, error) {
return &schema.ApplicationData{
UseCentralGitRepo: false,
LastRtVersion: rt.Version,
LastTerraformVersion: rt.TerraformVersion,
IsActive: true,
}, nil
}, nil)
if !confirmed {
return nil, false, nil
}
app = out.(*schema.ApplicationData)
} else {
return nil, false, err
}
}
lastRtVersion, err := version.NewVersion(app.LastRtVersion)
if err != nil {
return nil, false, err
}
currentRtVersion, err := version.NewVersion(rt.Version)
if err != nil {
return nil, false, err
}
if currentRtVersion.LessThan(lastRtVersion) {
return nil, false, fmt.Errorf("Last used RT version for %q: %s. You have %q, please upgrade.",
appName, app.LastRtVersion, rt.Version)
}
return app, true, nil
}
func FinishApplicationOperation(appName string, appData *schema.ApplicationData, isActive bool, outputs map[string]string,
ds *deploymentstate.DeploymentState) error {
appData.IsActive = isActive
appData.LastRtVersion = rt.Version
appData.LastTerraformVersion = rt.TerraformVersion
if outputs != nil && len(outputs) > 0 {
appData.InfraOutputs = outputs
}
err := ds.SaveApplication(appName, appData)
return err
}
func isEnvironmentSensitive(environment string) bool {
re := regexp.MustCompile("(prod|production|live)")
return re.Match([]byte(environment))
}
func cleanupFilePaths(paths []string) error {
var errors error
for _, p := range paths {
if err := os.RemoveAll(p); err != nil {
errors = multierror.Append(err)
}
}
return errors
}
func generateOutputMessage(app, env, slotId, name string, outputs map[string]string) (string, error) {
slotIdMessage := ""
if slotId != "" {
slotIdMessage = fmt.Sprintf(" with slot-id %s", slotId)
}
if name == "" {
b, err := json.MarshalIndent(outputs, "", " ")
if err != nil {
return "", err
}
return fmt.Sprintf("The app %s%s in env %s contains the outputs:\n%s\n", app, slotIdMessage, env, b), nil
}
value, ok := outputs[name]
if ok {
return fmt.Sprintf("The app %s%s in env %s contains the output:\n%s: %s\n", app, slotIdMessage, env, name, value), nil
}
return "", fmt.Errorf("The app %s%s in env %s does not contain the output:\n%s\n", app, slotIdMessage, env, name)
}
func deprecatedGitError() error {
return fmt.Errorf("%s", `
____________________________________
___ ___ / \
/ \____/ \ | Please migrate your app out of the |
/ / \/ \ \ | central Git repository as it is |
/ | .. | \ <--| deprecated. |
\___/| |\___/\ \____________________________________/
| |_| |_| \
| |/|__|\| \
| |__| |\
| |__| |_/ / \
| @ | | @ || @ | '
| |~~| || |
'ooo' 'ooo''ooo'
`)
}