-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.go
269 lines (229 loc) · 6.34 KB
/
api.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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
"time"
"github.com/99designs/api-sdk-go"
"github.com/urfave/cli"
)
func PrintList(uriMask string, olderThan time.Duration) {
req := smartling.FilesListRequest{
URIMask: uriMask,
}
if olderThan > 0 {
req.LastUploadedBefore = smartling.UTC{Time: time.Now().Add(-olderThan)}
}
files, err := client.List(req)
logAndQuitIfError(err)
for _, f := range files.Items {
fmt.Println(f.FileURI)
}
}
var LsCommand = cli.Command{
Name: "ls",
Usage: "list remote files",
Description: "ls [<uriMask>]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "older-than",
},
},
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) > 1 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: ls [<uriMask>]")
}
uriMask := c.Args().Get(0)
var d time.Duration
if len(c.String("older-than")) > 0 {
var err error
d, err = time.ParseDuration(c.String("older-than"))
logAndQuitIfError(err)
}
PrintList(uriMask, d)
},
}
func PrintFileStatus(remotepath, locale string) {
f, err := client.Status(remotepath)
logAndQuitIfError(err)
fst, err := f.GetFileStatusTranslation(locale)
logAndQuitIfError(err)
fmt.Println("File ", f.FileURI)
fmt.Println("String Count ", f.TotalStringCount)
fmt.Println("Word Count ", fst.AuthorizedWordCount+fst.CompletedWordCount)
fmt.Println("Authorized String Count ", fst.AuthorizedStringCount)
fmt.Println("Completed String Count ", fst.CompletedStringCount)
fmt.Println("Excluded String Count ", fst.ExcludedStringCount)
fmt.Println("Last Uploaded ", f.LastUploaded)
fmt.Println("File Type ", f.FileType)
}
var StatusCommand = cli.Command{
Name: "stat",
Usage: "display the translation status of a remote file",
Description: "stat <remote file> <locale>",
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: stat <remote file> <locale>")
}
remotepath := c.Args().Get(0)
locale := c.Args().Get(1)
PrintFileStatus(remotepath, locale)
},
}
var GetCommand = cli.Command{
Name: "get",
Usage: "downloads a remote file",
Description: "get <remote file>",
Before: cmdBefore,
Flags: []cli.Flag{
cli.StringFlag{
Name: "locale",
},
},
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: get <remote file>")
}
remotepath := c.Args().Get(0)
locale := c.String("locale")
var (
b []byte
err error
)
if locale == "" {
b, err = client.Download(remotepath)
} else {
b, err = client.DownloadTranslation(locale, smartling.FileDownloadRequest{
FileURIRequest: smartling.FileURIRequest{FileURI: remotepath},
})
}
logAndQuitIfError(err)
fmt.Println(string(b))
},
}
var PutCommand = cli.Command{
Name: "put",
Usage: "uploads a local file",
Description: "put <local file> <remote file>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "filetype",
}, cli.StringFlag{
Name: "parserconfig",
}, cli.BoolFlag{
Name: "approve",
},
},
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: put <local file> <remote file>")
}
localpath := c.Args().Get(0)
remotepath := c.Args().Get(1)
ft := smartling.FileType(c.String("filetype"))
if ft == "" {
ft = smartling.GetFileTypeByExtension(filepath.Ext(localpath))
}
parserconfig := map[string]string{}
if c.String("parserconfig") != "" {
parts := strings.Split(c.String("parserconfig"), ",")
if len(parts)%2 == 1 {
log.Fatalln("parserconfig must be in the format --parserconfig=key1,value1,key2,value2")
}
for i := 0; i < len(parts); i += 2 {
parserconfig[parts[i]] = parts[i+1]
}
}
f, err := ioutil.ReadFile(localpath)
logAndQuitIfError(err)
r, err := client.Upload(&smartling.FileUploadRequest{
File: f,
FileType: ft,
Authorize: c.Bool("approve"),
FileURIRequest: smartling.FileURIRequest{FileURI: remotepath},
})
logAndQuitIfError(err)
fmt.Println("Overwritten: ", r.Overwritten)
fmt.Println("String Count:", r.StringCount)
fmt.Println("Word Count: ", r.WordCount)
},
}
var RenameCommand = cli.Command{
Name: "rename",
Usage: "renames a remote file",
Description: "rename <remote file name> <new smartling file name>",
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) != 2 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: rename <remote file> <new smartling file name>")
}
remotepath := c.Args().Get(0)
newremotepath := c.Args().Get(1)
err := client.Rename(remotepath, newremotepath)
logAndQuitIfError(err)
},
}
var RmCommand = cli.Command{
Name: "rm",
Usage: "removes a remote file",
Description: "rm <remote file>...",
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) < 1 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: rm <remote file>...")
}
for _, remotepath := range c.Args() {
logAndQuitIfError(client.Delete(remotepath))
}
},
}
var LastmodifiedCommand = cli.Command{
Name: "lastmodified",
Usage: "shows when a remote file was modified last",
Description: "lastmodified <remote file>",
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: lastmodified <remote file>")
}
remotepath := c.Args().Get(0)
locales, err := client.LastModified(smartling.FileLastModifiedRequest{
FileURIRequest: smartling.FileURIRequest{FileURI: remotepath},
})
logAndQuitIfError(err)
for _, i := range locales.Items {
t := time.Time(i.LastModified.Time).Format("2 Jan 3:04")
fmt.Printf("%s %s\n", i.LocaleID, t)
}
},
}
var LocalesCommand = cli.Command{
Name: "locales",
Usage: "list the locales for the project",
Before: cmdBefore,
Action: func(c *cli.Context) {
if len(c.Args()) != 0 {
log.Println("Wrong number of arguments")
log.Fatalln("Usage: locales")
}
tl, err := client.Locales()
logAndQuitIfError(err)
for _, l := range tl {
if l.Enabled {
fmt.Printf("%-5s %s\n", l.LocaleID, l.Description)
}
}
},
}