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
133 lines (103 loc) · 2.99 KB
/
Copy pathvariables.go
File metadata and controls
133 lines (103 loc) · 2.99 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
package cmd
import (
"context"
"errors"
"fmt"
"strings"
"github.com/railwayapp/cli/ui"
"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.GetCurrentEnvironment(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 {
variables := &entity.Envs{}
updatedEnvNames := make([]string, 0)
for _, kvPair := range req.Args {
parts := strings.SplitN(kvPair, "=", 2)
if len(parts) != 2 {
return errors.New("invalid variables invocation. See --help")
}
key := parts[0]
value := parts[1]
variables.Set(key, value)
updatedEnvNames = append(updatedEnvNames, key)
}
err := h.ctrl.UpsertEnvsForEnvPlugin(ctx, variables)
if err != nil {
return err
}
environment, err := h.ctrl.GetCurrentEnvironment(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(*variables))
err = h.redeployAfterVariablesChange(ctx, environment)
if err != nil {
return err
}
return nil
}
func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandRequest) error {
err := h.ctrl.DeleteEnvsForEnvPlugin(ctx, req.Args)
if err != nil {
return err
}
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
fmt.Print(ui.Heading(fmt.Sprintf("Deleted %s for \"%s\"", strings.Join(req.Args, ", "), 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
}