-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathkey.go
More file actions
314 lines (274 loc) · 11.6 KB
/
key.go
File metadata and controls
314 lines (274 loc) · 11.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package cmd
import (
"encoding/json"
"github.com/lokalise/go-lokalise-api/v4"
"github.com/spf13/cobra"
)
var (
keyId int64
keyListOpts lokalise.KeyListOptions
filterUntranslated bool
newKey lokalise.NewKey
newKeyName string
newKeyFilenames string
newKeyComments []string
newKeyTranslations string
useAutomations bool
bulkDeleteKeyIds []int
bulkUpdateKeys string
bulkCreateKeys string
)
// keyCmd represents the key command
var keyCmd = &cobra.Command{
Use: "key",
Short: "Manage keys",
Long: `Keys are core item elements of the project.
Each phrase that is used in your app or website must be identified by a key and have values that represent translations to various languages. For example key index.welcome would have values of Welcome in English and Benvenuto in Italian. Keys can be assigned to one or multiple platforms. Once a key is assigned to a platform, it would be included in the export for file formats related to this platform.
`,
}
var keyListCmd = &cobra.Command{
Use: "list",
Short: "List all keys",
Long: "Lists all keys in the project.",
RunE: func(*cobra.Command, []string) error {
k := Api.Keys()
// preparing filters
keyListOpts.Limit = k.ListOpts().Limit
if filterUntranslated {
keyListOpts.FilterUntranslated = "1"
}
return repeatableCursorList(
func(cursor string) {
keyListOpts.Pagination = lokalise.PaginationCursor
keyListOpts.Cursor = cursor
k.SetListOptions(keyListOpts)
},
func() (lokalise.CursorPager, error) {
return k.List(projectId)
},
)
},
}
var keyCreateCmd = &cobra.Command{
Use: "create",
Short: "Create keys",
Long: "Creates one or more keys in the project.",
RunE: func(*cobra.Command, []string) error {
// preparing options
err := newKeyFillFields()
if err != nil {
return err
}
k := Api.Keys()
resp, err := k.Create(
projectId,
[]lokalise.NewKey{newKey},
lokalise.WithAutomations(useAutomations),
)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyRetrieveCmd = &cobra.Command{
Use: "retrieve",
Short: "Retrieve a key",
Long: "Retrieves a key.",
RunE: func(*cobra.Command, []string) error {
resp, err := Api.Keys().Retrieve(projectId, keyId)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update a key",
Long: "Updates the properties of a key and it’s associated objects. Requires Manage keys admin right.",
RunE: func(*cobra.Command, []string) error {
// preparing opts
err := newKeyFillFields()
if err != nil {
return err
}
resp, err := Api.Keys().Update(projectId, keyId, newKey)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a key",
Long: "Deletes a key from the project. Requires Manage keys admin right.",
RunE: func(*cobra.Command, []string) error {
resp, err := Api.Keys().Delete(projectId, keyId)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyBulkDeleteCmd = &cobra.Command{
Use: "bulk-delete",
Short: "Delete multiple keys",
Long: "Deletes multiple keys from the project. Requires Manage keys admin right.",
RunE: func(*cobra.Command, []string) error {
var keyIDs []int64
for _, id := range bulkDeleteKeyIds {
keyIDs = append(keyIDs, int64(id))
}
resp, err := Api.Keys().BulkDelete(projectId, keyIDs)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyBulkUpdateCmd = &cobra.Command{
Use: "bulk-update",
Short: "Update multiple keys",
Long: "Updates multiple keys in the project. Requires Manage keys admin right.",
RunE: func(*cobra.Command, []string) error {
var keys []lokalise.BulkUpdateKey
if err := json.Unmarshal([]byte(bulkUpdateKeys), &keys); err != nil {
return err
}
resp, err := Api.Keys().BulkUpdate(
projectId,
keys,
lokalise.WithAutomations(useAutomations),
)
if err != nil {
return err
}
return printJson(resp)
},
}
var keyBulkCreateCmd = &cobra.Command{
Use: "bulk-create",
Short: "Create multiple keys",
Long: "Creates multiple keys in the project from a JSON array.",
RunE: func(*cobra.Command, []string) error {
var keys []lokalise.NewKey
if err := json.Unmarshal([]byte(bulkCreateKeys), &keys); err != nil {
return err
}
resp, err := Api.Keys().Create(
projectId,
keys,
lokalise.WithAutomations(useAutomations),
)
if err != nil {
return err
}
return printJson(resp)
},
}
func init() {
keyCmd.AddCommand(keyListCmd, keyCreateCmd, keyRetrieveCmd, keyUpdateCmd, keyDeleteCmd,
keyBulkDeleteCmd, keyBulkUpdateCmd, keyBulkCreateCmd)
rootCmd.AddCommand(keyCmd)
// common for all Comment cmd`s
flagProjectId(keyCmd, true)
// List
fs := keyListCmd.Flags()
fs.Uint8Var(&keyListOpts.DisableReferences, "disable-references", 0, "Whether to disable key references.")
fs.Uint8Var(&keyListOpts.IncludeComments, "include-comments", 0, "Whether to include comments.")
fs.Uint8Var(&keyListOpts.IncludeScreenshots, "include-screenshots", 0, "Whether to include URL to screenshots.")
fs.Uint8Var(&keyListOpts.IncludeTranslations, "include-translations", 0, "Whether to include translations.")
fs.StringVar(&keyListOpts.FilterTranslationLangIDs, "filter-translation-lang-ids", "",
"One or more language ID to filter by. Will include translations only for listed IDs.")
fs.StringVar(&keyListOpts.FilterTags, "filter-tags", "", "One or more tags to filter by.")
fs.StringVar(&keyListOpts.FilterFilenames, "filter-filenames", "", "One or more filenames to filter by.")
fs.StringVar(&keyListOpts.FilterKeys, "filter-keys", "", "One or more key name to filter by. In case \"Per-platform keys\" is enabled in project settings, the filter will be applied to all platform names.")
fs.StringVar(&keyListOpts.FilterKeyIDs, "filter-key-ids", "", "One or more key identifiers to filter by.")
fs.StringVar(&keyListOpts.FilterPlatforms, "filter-platforms", "", "One or more platforms to filter by. Possible values are ios, android, web and other")
fs.BoolVar(&filterUntranslated, "filter-untranslated", false, "Filter by untranslated keys.")
fs.StringVar(&keyListOpts.FilterQAIssues, "filter-qa-issues", "", "One or more QA issues to filter by. Possible values are spelling_and_grammar, placeholders, html, url_count, url, email_count, email, brackets, numbers, leading_whitespace, trailing_whitespace, double_space and special_placeholder.")
// Create
fs = keyCreateCmd.Flags()
fs.StringVar(&newKeyName, "key-name", "", "Key name. For projects with enabled Per-platform key names, pass JSON object with included ios, android, web and other string attributes. (JSON, required).")
_ = keyCreateCmd.MarkFlagRequired("key-name")
fs.StringVar(&newKey.Description, "description", "", "Description of the key.")
fs.StringSliceVar(&newKey.Platforms, "platforms", []string{}, "List of platforms, enabled for this key (required).")
_ = keyCreateCmd.MarkFlagRequired("platforms")
fs.StringVar(&newKeyFilenames, "filenames", "", "An object containing key filename attribute for each platform. (JSON, see https://lokalise.com/api2docs/curl/#transition-create-keys-post).")
fs.StringSliceVar(&newKey.Tags, "tags", []string{}, "List of tags for this keys.")
fs.StringSliceVar(&newKeyComments, "comments", []string{}, "List of comments for this key.")
// screenshots skipped
fs.StringVar(&newKeyTranslations, "translations", "", "Translations for all languages. (JSON, see https://lokalise.com/api2docs/curl/#transition-create-keys-post).")
fs.BoolVar(&newKey.IsPlural, "is-plural", false, "Whether this key is plural.")
fs.StringVar(&newKey.PluralName, "plural-name", "", "Optional custom plural name (used in some formats).")
fs.BoolVar(&newKey.IsHidden, "is-hidden", false, "Whether this key is hidden from non-admins (translators).")
fs.BoolVar(&newKey.IsArchived, "is-archived", false, "Whether this key is archived.")
fs.StringVar(&newKey.Context, "context", "", "Optional context of the key (used with some file formats).")
fs.IntVar(&newKey.CharLimit, "char-limit", 0, "Maximum allowed number of characters in translations for this key.")
fs.StringVar(&newKey.CustomAttributes, "custom-attributes", "", "JSON containing custom attributes (if any).")
fs.BoolVar(&useAutomations, "use-automations", true, "Whether to run automations on the new key translations.")
// Update
flagKeyId(keyUpdateCmd)
fs = keyUpdateCmd.Flags()
fs.StringVar(&newKeyName, "key-name", "", "Key identifier. For projects with enabled Per-platform key names, pass `object` with included ios, android, web and other string attributes.")
fs.StringVar(&newKey.Description, "description", "", "Description of the key.")
fs.StringSliceVar(&newKey.Platforms, "platforms", []string{}, "List of platforms, enabled for this key. Possible values are ios, android, web and other.")
fs.StringVar(&newKeyFilenames, "filenames", "", "An object containing key filename attribute for each platform. (JSON, see https://lokalise.com/api2docs/curl/#transition-update-a-key-put).")
fs.StringSliceVar(&newKey.Tags, "tags", []string{}, "List of tags for this keys.")
fs.BoolVar(&newKey.MergeTags, "merge-tags", false, "Enable to merge specified tags with the current tags attached to the key.")
fs.BoolVar(&newKey.IsPlural, "is-plural", false, "Whether this key is plural.")
fs.StringVar(&newKey.PluralName, "plural-name", "", "Optional custom plural name (used in some formats).")
fs.BoolVar(&newKey.IsHidden, "is-hidden", false, "Whether this key is hidden from non-admins (translators).")
fs.BoolVar(&newKey.IsArchived, "is-archived", false, "Whether this key is archived.")
fs.StringVar(&newKey.Context, "context", "", "Optional context of the key (used with some file formats).")
fs.IntVar(&newKey.CharLimit, "char-limit", 0, "Maximum allowed number of characters in translations for this key.")
fs.StringVar(&newKey.CustomAttributes, "custom-attributes", "", "JSON containing custom attributes (if any).")
// retrieve, delete
flagKeyId(keyRetrieveCmd)
keyRetrieveCmd.Flags().Uint8Var(&keyListOpts.DisableReferences, "disable-references", 0, "Whether to disable key references.")
flagKeyId(keyDeleteCmd)
// Bulk delete
keyBulkDeleteCmd.Flags().IntSliceVar(&bulkDeleteKeyIds, "key-ids", []int{},
"Comma-separated list of key IDs to delete (required).")
_ = keyBulkDeleteCmd.MarkFlagRequired("key-ids")
// Bulk update
fs = keyBulkUpdateCmd.Flags()
fs.StringVar(&bulkUpdateKeys, "keys", "",
"JSON array of key objects to update. Each object must contain key_id and fields to update (required).")
_ = keyBulkUpdateCmd.MarkFlagRequired("keys")
fs.BoolVar(&useAutomations, "use-automations", true,
"Whether to run automations on the updated key translations.")
// Bulk create
fs = keyBulkCreateCmd.Flags()
fs.StringVar(&bulkCreateKeys, "keys", "",
"JSON array of key objects to create. Each object should contain key_name, platforms, and other fields (required).")
_ = keyBulkCreateCmd.MarkFlagRequired("keys")
fs.BoolVar(&useAutomations, "use-automations", true,
"Whether to run automations on the new key translations.")
}
func flagKeyId(cmd *cobra.Command) {
cmd.Flags().Int64Var(&keyId, "key-id", 0, "A unique identifier of the key (required).")
_ = cmd.MarkFlagRequired("key-id")
}
func newKeyFillFields() error {
if newKeyName != "" {
newKey.KeyName = newKeyName
}
if newKeyFilenames != "" {
ps := lokalise.PlatformStrings{}
err := json.Unmarshal([]byte(newKeyFilenames), &ps)
if err != nil {
return err
}
newKey.Filenames = &ps
}
if newKeyTranslations != "" {
err := json.Unmarshal([]byte(newKeyTranslations), &newKey.Translations)
if err != nil {
return err
}
}
return nil
}