-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathactions.go
More file actions
170 lines (164 loc) · 4.65 KB
/
Copy pathactions.go
File metadata and controls
170 lines (164 loc) · 4.65 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
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
package tui
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/leg100/pug/internal/module"
"github.com/leg100/pug/internal/plan"
"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/task"
"github.com/leg100/pug/internal/tui/keys"
)
// ActionHandler handles actions common to more than one model.
type ActionHandler struct {
*Helpers
IDRetriever
}
type IDRetriever interface {
GetModuleIDs() ([]resource.ID, error)
GetWorkspaceIDs() ([]resource.ID, error)
}
func (m *ActionHandler) Update(msg tea.Msg) tea.Cmd {
var (
createPlanOptions plan.CreateOptions
// TODO: if only one worskpace is being applied, then customise message
// to mention name of workspace being applied.
applyPrompt = "Auto-apply %d workspaces?"
)
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Common.Init),
key.Matches(msg, keys.Common.InitUpgrade),
key.Matches(msg, keys.Common.InitReconfigure):
initOpts := module.InitOptions{
Upgrade: key.Matches(msg, keys.Common.InitUpgrade),
Reconfigure: key.Matches(msg, keys.Common.InitReconfigure),
}
ids, err := m.GetModuleIDs()
if err != nil {
return ReportError(err)
}
fn := func(moduleID resource.ID) (task.Spec, error) {
return m.Modules.Init(moduleID, initOpts)
}
return m.CreateTasks(fn, ids...)
case key.Matches(msg, keys.Common.Execute):
ids, err := m.GetModuleIDs()
if err != nil {
return ReportError(err)
}
return CmdHandler(PromptMsg{
Prompt: fmt.Sprintf("Execute program in %d module directories: ", len(ids)),
Placeholder: "terraform version",
Action: func(v string) tea.Cmd {
if v == "" {
return nil
}
// split value into program and any args
parts := strings.Split(v, " ")
prog := parts[0]
args := parts[1:]
fn := func(moduleID resource.ID) (task.Spec, error) {
return m.Modules.Execute(moduleID, prog, args...)
}
return m.CreateTasks(fn, ids...)
},
Key: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "confirm")),
Cancel: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "cancel")),
})
case key.Matches(msg, keys.Common.Validate):
ids, err := m.GetModuleIDs()
if err != nil {
return ReportError(err)
}
cmd := m.CreateTasks(m.Modules.Validate, ids...)
return cmd
case key.Matches(msg, keys.Common.Format):
ids, err := m.GetModuleIDs()
if err != nil {
return ReportError(err)
}
cmd := m.CreateTasks(m.Modules.Format, ids...)
return cmd
case key.Matches(msg, keys.Common.PlanDestroy):
createPlanOptions.Destroy = true
fallthrough
case key.Matches(msg, keys.Common.Plan):
ids, err := m.GetWorkspaceIDs()
if err != nil {
return ReportError(err)
}
fn := func(workspaceID resource.ID) (task.Spec, error) {
return m.Plans.Plan(workspaceID, createPlanOptions)
}
return m.CreateTasks(fn, ids...)
case key.Matches(msg, keys.Common.Destroy):
createPlanOptions.Destroy = true
applyPrompt = "Destroy resources of %d workspaces?"
fallthrough
case key.Matches(msg, keys.Common.AutoApply):
ids, err := m.GetWorkspaceIDs()
if err != nil {
return ReportError(err)
}
fn := func(workspaceID resource.ID) (task.Spec, error) {
return m.Plans.Apply(workspaceID, createPlanOptions)
}
return YesNoPrompt(
fmt.Sprintf(applyPrompt, len(ids)),
m.CreateTasks(fn, ids...),
)
case key.Matches(msg, keys.Common.Cost):
ids, err := m.GetWorkspaceIDs()
if err != nil {
return ReportError(err)
}
spec, err := m.Workspaces.Cost(ids...)
if err != nil {
return ReportError(fmt.Errorf("creating task: %w", err))
}
return m.CreateTasksWithSpecs(spec)
case key.Matches(msg, keys.Common.State):
ids, err := m.GetWorkspaceIDs()
if err != nil {
return ReportError(err)
}
if len(ids) == 0 {
return nil
}
return NavigateTo(ResourceListKind, WithParent(ids[0]))
case key.Matches(msg, keys.Common.Edit):
ids, err := m.GetModuleIDs()
if err != nil {
return ReportError(err)
}
if len(ids) == 0 {
return nil
}
mod, err := m.Modules.Get(ids[0])
if err != nil {
return ReportError(err)
}
return OpenEditor(m.Workdir.Join(mod.Path))
}
}
return nil
}
func (m *ActionHandler) HelpBindings() []key.Binding {
return []key.Binding{
keys.Common.Init,
keys.Common.InitUpgrade,
keys.Common.InitReconfigure,
keys.Common.Format,
keys.Common.Validate,
keys.Common.Plan,
keys.Common.PlanDestroy,
keys.Common.AutoApply,
keys.Common.Destroy,
keys.Common.Execute,
keys.Common.State,
keys.Common.Cost,
}
}