Repository navigation
Expand file tree
/
Copy pathenvironment.go
More file actions
62 lines (53 loc) · 1.46 KB
/
Copy pathenvironment.go
File metadata and controls
62 lines (53 loc) · 1.46 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
<
78CE
div data-line-number="25" class="react-line-number react-code-text" style="padding-right:16px">2526
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
package cmd
import (
"context"
"fmt"
"github.com/manifoldco/promptui"
"github.com/railwayapp/cli/entity"
"github.com/railwayapp/cli/ui"
)
func (h *Handler) Environment(ctx context.Context, req *entity.CommandRequest) error {
projectID, err := h.cfg.GetProject()
if err != nil {
return err
}
project, err := h.ctrl.GetProject(ctx, projectID)
if err != nil {
return err
}
var environment *entity.Environment
if len(req.Args) > 0 {
var name = req.Args[0]
// Look for existing environment with name
for _, projectEnvironment := range project.Environments {
if name == projectEnvironment.Name {
environment = projectEnvironment
}
}
if (environment != nil) {
fmt.Printf("%s Environment: %s\n", promptui.IconGood, ui.BlueText(environment.Name))
} else {
// Create new environment
environment, err = h.ctrl.CreateEnvironment(ctx, &entity.CreateEnvironmentRequest{
Name: name,
ProjectID: project.Id,
})
if err != nil {
return err
}
fmt.Printf("Created Environment %s\nEnvironment: %s\n", promptui.IconGood, ui.BlueText(ui.Bold(name).String()))
}
} else {
// Existing environment selector
environment, err = ui.PromptEnvironments(project.Environments)
if err != nil {
return err
}
}
err = h.cfg.SetEnvironment(environment.Id)
if err != nil {
return err
}
fmt.Printf("%s ProTip: You can view the active environment by running %s\n", promptui.IconInitial, ui.BlueText("railway status"))
return err
}