This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathupload_cleanup.go
84 lines (66 loc) · 1.82 KB
/
upload_cleanup.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
package main
import (
"fmt"
"sort"
"strings"
"github.com/phrase/phraseapp-client/internal/prompt"
"github.com/phrase/phraseapp-go/phraseapp"
)
type UploadCleanupCommand struct {
phraseapp.Config
ID string `cli:"arg required"`
Confirm bool `cli:"opt --confirm desc='Don’t ask for confirmation'"`
}
func (cmd *UploadCleanupCommand) Run() error {
client, err := newClient(cmd.Config.Credentials, cmd.Config.Debug)
if err != nil {
return err
}
return UploadCleanup(client, cmd)
}
func UploadCleanup(client *phraseapp.Client, cmd *UploadCleanupCommand) error {
q := "unmentioned_in_upload:" + cmd.ID
params := &phraseapp.KeysListParams{Q: &q}
page := 1
keys, err := client.KeysList(cmd.Config.DefaultProjectID, page, 100, params)
if err != nil {
return err
}
if len(keys) == 0 {
fmt.Println("There were no keys unmentioned in that upload.")
return nil
}
for len(keys) != 0 {
ids := make([]string, len(keys))
names := make([]string, len(keys))
for i, key := range keys {
ids[i] = key.ID
names[i] = key.Name
}
if !cmd.Confirm {
fmt.Println("You are about to delete the following key(s) from your project:")
sort.Strings(names)
fmt.Println(strings.Join(names, "\n"))
confirmation := ""
err := prompt.WithDefault("Are you sure you want to continue? (y/n)", &confirmation, "n")
if err != nil {
return err
}
if strings.ToLower(confirmation) != "y" {
fmt.Println("Clean up aborted")
return nil
}
}
q := "ids:" + strings.Join(ids, ",")
affected, err := client.KeysDelete(cmd.Config.DefaultProjectID, &phraseapp.KeysDeleteParams{
Q: &q,
})
if err != nil {
return err
}
fmt.Printf("%d key(s) successfully deleted.\n", affected.RecordsAffected)
page++
keys, _ = client.KeysList(cmd.Config.DefaultProjectID, page, 100, params)
}
return nil
}