forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.go
More file actions
143 lines (113 loc) · 3.19 KB
/
Copy pathvariables.go
File metadata and controls
143 lines (113 loc) · 3.19 KB
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
package cmd
import (
"context"
"fmt"
"github.com/railwayapp/cli/ui"
"strings"
"github.com/railwayapp/cli/entity"
)
func (h *Handler) Variables(ctx context.Context, _ *entity.CommandRequest) error {
envs, err := h.ctrl.GetEnvs(ctx)
if err != nil {
return err
}
environment, err := h.ctrl.GetEnvironment(ctx)
if err != nil {
return err
}
fmt.Print(ui.Heading(fmt.Sprintf("%s Environment Variables", environment.Name)))
fmt.Print(ui.KeyValues(*envs))
return nil
}
func (h *Handler) VariablesGet(ctx context.Context, req *entity.CommandRequest) error {
envs, err := h.ctrl.GetEnvs(ctx)
if err != nil {
return err
}
for _, key := range req.Args {
fmt.Println(envs.Get(key))
}
return nil
}
func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest) error {
envs, err := h.ctrl.GetEnvsForEnvPlugin(ctx)
if err != nil {
return err
}
updatedEnvs := &entity.Envs{}
updatedEnvNames := make([]string, 0)
for _, kvPair := range req.Args {
parts := strings.SplitN(kvPair, "=", 2)
key := parts[0]
value := parts[1]
envs.Set(key, value)
updatedEnvs.Set(key, value)
updatedEnvNames = append(updatedEnvNames, key)
}
_, err = h.ctrl.UpdateEnvsForEnvPlugin(ctx, envs)
if err != nil {
return err
}
environment, err := h.ctrl.GetEnvironment(ctx)
if err != nil {
return err
}
fmt.Print(ui.Heading(fmt.Sprintf("Updated %s for \"%s\"", strings.Join(updatedEnvNames, ", "), environment.Name)))
fmt.Print(ui.KeyValues(*updatedEnvs))
err = h.redeployAfterVariablesChange(ctx, environment)
if err != nil {
return err
}
return nil
}
func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandRequest) error {
envs, err := h.ctrl.GetEnvsForEnvPlugin(ctx)
if err != nil {
return err
}
updatedEnvNames := make([]string, 0)
for _, key := range req.Args {
updatedEnvNames = append(updatedEnvNames, key)
envs.Delete(key)
}
_, err = h.ctrl.UpdateEnvsForEnvPlugin(ctx, envs)
if err != nil {
return err
}
environment, err := h.ctrl.GetEnvironment(ctx)
if err != nil {
return err
}
fmt.Print(ui.Heading(fmt.Sprintf("Deleted %s for \"%s\"", strings.Join(updatedEnvNames, ", "), environment.Name)))
err = h.redeployAfterVariablesChange(ctx, environment)
if err != nil {
return err
}
return nil
}
func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment *entity.Environment) error {
deployments, err := h.ctrl.GetDeployments(ctx)
if err != nil {
return err
}
// Don't redeploy if we don't yet have any deployments
if len(deployments) == 0 {
return nil
}
// Don't redeploy if the latest deploy for environment came from up
latestDeploy := deployments[0]
if latestDeploy.Meta == nil || latestDeploy.Meta.Repo == "" {
fmt.Printf(ui.AlertInfo("Run %s to redeploy your project"), ui.MagentaText("railway up").Underline())
return nil
}
ui.StartSpinner(&ui.SpinnerCfg{
Message: fmt.Sprintf("Redeploying \"%s\" with new variables", environment.Name),
})
err = h.ctrl.DeployEnvironmentTriggers(ctx)
if err != nil {
return err
}
ui.StopSpinner("Deploy triggered")
fmt.Printf("☁️ Deploy Logs available at %s\n", ui.GrayText(h.ctrl.GetProjectDeploymentsURL(ctx, latestDeploy.ProjectID)))
return nil
}