-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdelete.go
More file actions
199 lines (174 loc) · 4.39 KB
/
delete.go
File metadata and controls
199 lines (174 loc) · 4.39 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 txlib
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/gosimple/slug"
"github.com/transifex/cli/internal/txlib/config"
"github.com/transifex/cli/pkg/jsonapi"
"github.com/transifex/cli/pkg/txapi"
)
type DeleteCommandArguments struct {
ResourceIds []string
Force bool
Skip bool
Branch string
}
func DeleteCommand(
cfg *config.Config,
api jsonapi.Connection,
arguments *DeleteCommandArguments,
) error {
var cfgResources []*config.Resource
if arguments.Branch == "-1" {
arguments.Branch = ""
} else if arguments.Branch == "" {
arguments.Branch = getGitBranch()
if arguments.Branch == "" {
fmt.Println("Couldn't find branch information")
}
}
fmt.Printf("# Initiating Delete\n\n")
for _, resourceId := range arguments.ResourceIds {
// Split resource id to check if it is a bulk delete
parts := strings.Split(resourceId, ".")
if len(parts) != 2 {
if !arguments.Skip {
return fmt.Errorf(
"wrong resource id for %s. Aborting",
resourceId,
)
} else {
fmt.Printf(
"Wrong resource id for %s. Aborting",
resourceId,
)
}
}
projectSlug := parts[0]
resourceSlug := parts[1]
// Find Resources for delete in config
if resourceSlug != "*" {
cfgResource := cfg.FindResource(resourceId)
if cfgResource == nil {
if !arguments.Skip {
return fmt.Errorf(
"could not find resource '%s' in local configuration. Aborting",
resourceId,
)
} else {
fmt.Printf(
"could not find resource '%s' in local configuration.",
resourceId,
)
break
}
}
cfgResources = append(cfgResources, cfgResource)
} else {
batchedResources := cfg.FindResourcesByProject(projectSlug)
cfgResources = append(cfgResources, batchedResources...)
}
}
// If there are no resources found stop
if len(cfgResources) == 0 {
color.Red("Given resources not found in config file.")
return nil
}
// Delete each resource
for _, item := range cfgResources {
// Delete Resource from Server
cfgResource := *item
if arguments.Branch != "" {
cfgResource.ResourceSlug = fmt.Sprintf("%s--%s",
slug.Make(arguments.Branch),
cfgResource.ResourceSlug)
}
err := deleteResource(&api, cfg, cfgResource, *arguments)
if err != nil {
if !arguments.Skip {
return err
} else {
color.Red("Given resources not found in config file.")
}
} else {
// Remove successful deletes from config
cfg.RemoveResource(cfgResource)
}
}
err := cfg.Save()
if err != nil {
return err
}
return nil
}
func deleteResource(
api *jsonapi.Connection, cfg *config.Config, cfgResource config.Resource,
args DeleteCommandArguments,
) error {
// Get Organization from Server
organization, err := txapi.GetOrganization(api,
cfgResource.OrganizationSlug)
if err != nil {
return err
}
if organization == nil {
return fmt.Errorf("organization '%s' not found",
cfgResource.OrganizationSlug)
}
// Get Project from Server
project, err := txapi.GetProject(api, organization,
cfgResource.ProjectSlug)
if err != nil {
return err
}
if project == nil {
return fmt.Errorf("project '%s - %s' not found",
cfgResource.OrganizationSlug,
cfgResource.ProjectSlug)
}
// Get Resource from Server
resource, err := txapi.GetResource(api, project, cfgResource.ResourceSlug)
if err != nil {
return err
}
if resource == nil {
return fmt.Errorf("resource '%s - %s - %s' not found",
cfgResource.OrganizationSlug,
cfgResource.ProjectSlug,
cfgResource.ResourceSlug)
}
msg := fmt.Sprintf("Deleting resource '%s'",
cfgResource.ResourceSlug)
fmt.Println(msg)
if err != nil {
return err
}
if !args.Force {
remoteStats, _ := txapi.GetResourceStats(api, resource, nil)
for languageId := range remoteStats {
if languageId == project.
Relationships["source_language"].DataSingular.Id {
continue
}
var remoteStatAttributes txapi.ResourceLanguageStatsAttributes
err := remoteStats[languageId].MapAttributes(&remoteStatAttributes)
if err != nil {
return err
}
if remoteStatAttributes.TranslatedStrings > 0 {
return fmt.Errorf("aborting due to translations in %s",
cfgResource.ResourceSlug)
}
}
}
err = txapi.DeleteResource(api, resource)
if err != nil {
color.Red("Resource deletion for '%s' failed",
cfgResource.ResourceSlug)
return err
} else {
color.Green("Resource '%s' deleted", cfgResource.ResourceSlug)
}
return nil
}