Skip to content

Commit 87c549f

Browse files
refactor: decouple flag state, use int64 for key IDs, add body assertions
- Decouple use-automations flag by introducing per-command backing variables (bulkUpdateUseAutomations, bulkCreateUseAutomations) to prevent flag state leaking across commands - Change bulkDeleteKeyIds from []int to []int64 with Int64SliceVar, removing the manual conversion loop for consistency with single-key operations - Add testBody assertions to TestKeyBulkUpdate and TestKeyBulkCreate to validate outbound request payloads
1 parent 84d7289 commit 87c549f

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

cmd/key.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ var (
2121

2222
useAutomations bool
2323

24-
bulkDeleteKeyIds []int
25-
bulkUpdateKeys string
26-
bulkCreateKeys string
24+
// bulk-delete
25+
bulkDeleteKeyIds []int64
26+
27+
// bulk-update
28+
bulkUpdateKeys string
29+
bulkUpdateUseAutomations bool
30+
31+
// bulk-create
32+
bulkCreateKeys string
33+
bulkCreateUseAutomations bool
2734
)
2835

2936
// keyCmd represents the key command
@@ -137,12 +144,7 @@ var keyBulkDeleteCmd = &cobra.Command{
137144
Short: "Delete multiple keys",
138145
Long: "Deletes multiple keys from the project. Requires Manage keys admin right.",
139146
RunE: func(*cobra.Command, []string) error {
140-
var keyIDs []int64
141-
for _, id := range bulkDeleteKeyIds {
142-
keyIDs = append(keyIDs, int64(id))
143-
}
144-
145-
resp, err := Api.Keys().BulkDelete(projectId, keyIDs)
147+
resp, err := Api.Keys().BulkDelete(projectId, bulkDeleteKeyIds)
146148
if err != nil {
147149
return err
148150
}
@@ -163,7 +165,7 @@ var keyBulkUpdateCmd = &cobra.Command{
163165
resp, err := Api.Keys().BulkUpdate(
164166
projectId,
165167
keys,
166-
lokalise.WithAutomations(useAutomations),
168+
lokalise.WithAutomations(bulkUpdateUseAutomations),
167169
)
168170
if err != nil {
169171
return err
@@ -185,7 +187,7 @@ var keyBulkCreateCmd = &cobra.Command{
185187
resp, err := Api.Keys().Create(
186188
projectId,
187189
keys,
188-
lokalise.WithAutomations(useAutomations),
190+
lokalise.WithAutomations(bulkCreateUseAutomations),
189191
)
190192
if err != nil {
191193
return err
@@ -263,7 +265,7 @@ func init() {
263265
flagKeyId(keyDeleteCmd)
264266

265267
// Bulk delete
266-
keyBulkDeleteCmd.Flags().IntSliceVar(&bulkDeleteKeyIds, "key-ids", []int{},
268+
keyBulkDeleteCmd.Flags().Int64SliceVar(&bulkDeleteKeyIds, "key-ids", []int64{},
267269
"Comma-separated list of key IDs to delete (required).")
268270
_ = keyBulkDeleteCmd.MarkFlagRequired("key-ids")
269271

@@ -272,15 +274,15 @@ func init() {
272274
fs.StringVar(&bulkUpdateKeys, "keys", "",
273275
"JSON array of key objects to update. Each object must contain key_id and fields to update (required).")
274276
_ = keyBulkUpdateCmd.MarkFlagRequired("keys")
275-
fs.BoolVar(&useAutomations, "use-automations", true,
277+
fs.BoolVar(&bulkUpdateUseAutomations, "use-automations", true,
276278
"Whether to run automations on the updated key translations.")
277279

278280
// Bulk create
279281
fs = keyBulkCreateCmd.Flags()
280282
fs.StringVar(&bulkCreateKeys, "keys", "",
281283
"JSON array of key objects to create. Each object should contain key_name, platforms, and other fields (required).")
282284
_ = keyBulkCreateCmd.MarkFlagRequired("keys")
283-
fs.BoolVar(&useAutomations, "use-automations", true,
285+
fs.BoolVar(&bulkCreateUseAutomations, "use-automations", true,
284286
"Whether to run automations on the new key translations.")
285287
}
286288

cmd/key_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func TestKeyBulkUpdate(t *testing.T) {
5555
testMethod(t, r, "PUT")
5656
testHeader(t, r, "X-Api-Token", testApiToken)
5757

58+
data := `{"keys":[{"key_id":123,"description":"Updated description","tags":["updated"]}],"use_automations":true}`
59+
req := new(bytes.Buffer)
60+
_ = json.Compact(req, []byte(data))
61+
testBody(t, r, req.String())
62+
5863
_, _ = fmt.Fprint(w, `{
5964
"project_id": "`+testProjectID+`",
6065
"keys": [
@@ -101,6 +106,11 @@ func TestKeyBulkCreate(t *testing.T) {
101106
testMethod(t, r, "POST")
102107
testHeader(t, r, "X-Api-Token", testApiToken)
103108

109+
data := `{"keys":[{"key_name":"welcome","platforms":["web"],"tags":[]},{"key_name":"goodbye","platforms":["ios","android"],"tags":["v2"]}],"use_automations":true}`
110+
req := new(bytes.Buffer)
111+
_ = json.Compact(req, []byte(data))
112+
testBody(t, r, req.String())
113+
104114
_, _ = fmt.Fprint(w, `{
105115
"project_id": "`+testProjectID+`",
106116
"keys": [

0 commit comments

Comments
 (0)