-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathupdate.go
More file actions
199 lines (171 loc) · 5.21 KB
/
Copy pathupdate.go
File metadata and controls
199 lines (171 loc) · 5.21 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
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
package update
import (
"context"
"fmt"
"strings"
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"github.com/zeabur/cli/internal/cmdutil"
"github.com/zeabur/cli/internal/util"
"github.com/zeabur/cli/pkg/fill"
)
type Options struct {
id string
name string
environmentID string
keys map[string]string
rawKeys []string
skipConfirm bool
inputDone bool
}
func NewCmdUpdateVariable(f *cmdutil.Factory) *cobra.Command {
opts := &Options{}
cmd := &cobra.Command{
Use: "update",
Short: "update variable(s)",
Long: `update variable(s) for a service`,
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdateVariable(f, opts)
},
}
util.AddServiceParam(cmd, &opts.id, &opts.name)
util.AddEnvOfServiceParam(cmd, &opts.environmentID)
cmd.Flags().BoolVarP(&opts.skipConfirm, "yes", "y", false, "Skip confirmation")
cmd.Flags().StringArrayVarP(&opts.rawKeys, "key", "k", nil, "Key value pair of the variable (KEY=VALUE)")
return cmd
}
func runUpdateVariable(f *cmdutil.Factory, opts *Options) error {
if f.Interactive {
opts.keys = make(map[string]string)
return runUpdateVariableInteractive(f, opts)
}
return runUpdateVariableNonInteractive(f, opts)
}
func runUpdateVariableInteractive(f *cmdutil.Factory, opts *Options) error {
zctx := f.Config.GetContext()
if _, err := f.ParamFiller.ServiceByNameWithEnvironment(fill.ServiceByNameWithEnvironmentOptions{
ProjectCtx: zctx,
ServiceID: &opts.id,
ServiceName: &opts.name,
EnvironmentID: &opts.environmentID,
CreateNew: false,
}); err != nil {
return err
}
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(fmt.Sprintf(" Querying variables of service: %s...", opts.name)),
)
s.Start()
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
if err != nil {
return err
}
varMap := varList.ToMap()
keyTable := make([]string, 0, len(varMap))
selectTable := make([]string, 0, len(varMap))
for k, v := range varMap {
keyTable = append(keyTable, k)
selectTable = append(selectTable, fmt.Sprintf("%s = %s", k, v))
opts.keys[k] = v
}
s.Stop()
for !opts.inputDone {
updateVarSelect, err := f.Prompter.Select("Select variable to update", "", selectTable)
if err != nil {
return err
}
varInput, err := f.Prompter.Input("Enter selected variable value modified: ", "")
if err != nil {
return err
}
opts.keys[keyTable[updateVarSelect]] = varInput
doneConfirm, err := f.Prompter.Confirm("Are you done entering value of variable?", false)
if err != nil {
return err
}
opts.inputDone = doneConfirm
}
return runUpdateVariableNonInteractive(f, opts)
}
func parseRawKeys(rawKeys []string) (map[string]string, error) {
result := make(map[string]string, len(rawKeys))
for _, raw := range rawKeys {
parts := strings.SplitN(raw, "=", 2)
if len(parts) != 2 || parts[0] == "" {
return nil, fmt.Errorf("invalid --key format %q: expected KEY=VALUE", raw)
}
result[parts[0]] = parts[1]
}
return result, nil
}
func runUpdateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
// Parse rawKeys from StringArrayVar into the keys map (non-interactive mode)
if opts.keys == nil && len(opts.rawKeys) > 0 {
parsed, err := parseRawKeys(opts.rawKeys)
if err != nil {
return err
}
opts.keys = parsed
}
if opts.id == "" && opts.name != "" {
service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.name)
if err != nil {
return err
}
opts.id = service.ID
}
if opts.id == "" {
return fmt.Errorf("--id or --name is required")
}
if opts.environmentID == "" {
envID, err := util.ResolveEnvironmentIDByServiceID(f.ApiClient, opts.id)
if err != nil {
return err
}
opts.environmentID = envID
}
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(fmt.Sprintf(" Updating variables of service: %s...", opts.name)),
)
s.Start()
// Fetch existing variables and merge with user-provided keys,
// so that unmentioned variables are preserved (not deleted).
// In interactive mode, opts.keys already contains the full
// variable map from the interactive flow.
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
if err != nil {
s.Stop()
return err
}
mergedVars := varList.ToMap()
for k, v := range opts.keys {
mergedVars[k] = v
}
opts.keys = mergedVars
createVarResult, err := f.ApiClient.UpdateVariables(context.Background(), opts.id, opts.environmentID, opts.keys)
if err != nil {
s.Stop()
return err
}
if !createVarResult {
s.Stop()
return fmt.Errorf("failed to update variables of service: %s", opts.name)
}
s.Stop()
if f.JSON {
out := make([]map[string]string, 0, len(opts.keys))
for k, v := range opts.keys {
out = append(out, map[string]string{"Key": k, "Value": v})
}
return f.Printer.JSON(out)
}
f.Log.Infof("Successfully updated variables of service: %s\n", opts.name)
table := make([][]string, 0, len(opts.keys))
for k, v := range opts.keys {
table = append(table, []string{k, v})
}
f.Printer.Table([]string{"Key", "Value"}, table)
return nil
}