/
prompt.go
204 lines (157 loc) · 3.91 KB
/
prompt.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cmd
import (
"fmt"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/strolt/strolt/apps/strolt/internal/config"
"github.com/strolt/strolt/apps/strolt/internal/sctxt"
"github.com/strolt/strolt/apps/strolt/internal/task"
)
type Prompt struct {
ServiceName string
TaskName string
DestinationName string
SnapshotName string
cmd *cobra.Command
}
func NewPrompt(cmd *cobra.Command) *Prompt {
return &Prompt{
cmd: cmd,
}
}
func (p *Prompt) ScanServiceName() error {
c := config.Get()
serviceName, err := p.cmd.Flags().GetString("service")
if serviceName == "" || err != nil {
prompt := promptui.Select{
HideSelected: true,
Label: "select service",
Items: config.GetServiceNameList(),
}
_, result, err := prompt.Run()
if err != nil {
return err
}
serviceName = result
}
p.ServiceName = serviceName
if _, ok := c.Services[serviceName]; !ok {
return fmt.Errorf("does not exists service - '%s'", serviceName)
}
return nil
}
func (p *Prompt) ScanTaskName() error {
if p.ServiceName == "" {
return fmt.Errorf("serviceName is empty")
}
c := config.Get()
taskName, _ := p.cmd.Flags().GetString("task")
if taskName == "" {
prompt := promptui.Select{
HideSelected: true,
Label: "select task",
Items: config.GetTaskNameList(p.ServiceName),
}
_, result, err := prompt.Run()
if err != nil {
return err
}
taskName = result
}
p.TaskName = taskName
if _, ok := c.Services[p.ServiceName][p.TaskName]; !ok {
return fmt.Errorf("does not exists task - '%s'", p.TaskName)
}
return nil
}
func (p *Prompt) ScanDestinationName() error {
if p.ServiceName == "" {
return fmt.Errorf("serviceName is empty")
}
if p.TaskName == "" {
return fmt.Errorf("taskName is empty")
}
destinationName, _ := p.cmd.Flags().GetString("destination")
if destinationName == "" {
prompt := promptui.Select{
HideSelected: true,
Label: "Select destination",
Items: config.GetDestinationNameList(p.ServiceName, p.TaskName),
}
_, result, err := prompt.Run()
if err != nil {
return err
}
destinationName = result
}
p.DestinationName = destinationName
t, err := task.New(p.ServiceName, p.TaskName, sctxt.TManual, sctxt.OpTypeSnapshots)
if err != nil {
return err
}
defer t.Close()
if !t.IsAvailableDestinationName(p.DestinationName) {
return fmt.Errorf("does not exists destination - %s", p.DestinationName)
}
return nil
}
func (p *Prompt) ScanSnapshotName() error {
if p.ServiceName == "" {
return fmt.Errorf("serviceName is empty")
}
if p.TaskName == "" {
return fmt.Errorf("taskName is empty")
}
if p.DestinationName == "" {
return fmt.Errorf("destinationName is empty")
}
t, err := task.New(p.ServiceName, p.TaskName, sctxt.TManual, sctxt.OpTypeRestore)
if err != nil {
return err
}
defer t.Close()
snapshotList, err := t.GetSnapshotList(p.DestinationName)
if err != nil {
return err
}
snapshotName, err := p.cmd.Flags().GetString("snapshot")
if err != nil {
return err
}
items := make([]string, len(snapshotList))
for i, snapshot := range snapshotList {
snapshotID := snapshot.ShortID
if snapshotID == "" {
snapshotID = snapshot.ID
}
items[i] = fmt.Sprintf("%s (%s) [%s]", snapshotID, snapshot.Time.Format(time.RFC3339), strings.Join(snapshot.Tags, ", "))
}
if snapshotName == "" {
prompt := promptui.Select{
HideSelected: true,
Label: "select snapshot",
Items: items,
}
i, _, err := prompt.Run()
if err != nil {
return err
}
snapshotName = snapshotList[i].ID
}
if !snapshotList.IsAvailable(snapshotName) {
return fmt.Errorf("snapshot not exists")
}
p.SnapshotName = snapshotName
return nil
}
func (p *Prompt) AskIsConfirm() bool {
prompt := promptui.Prompt{
HideEntered: true,
Label: "Are you shure",
IsConfirm: true,
}
_, err := prompt.Run()
return err == nil
}