forked from frozzare/posync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (189 loc) · 4.42 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
var (
configFlag = flag.String("config", "", "Path to config file")
versionFlag = flag.Bool("version", false, "Print posync version")
version = "master"
)
// File represents a config file struct.
type File struct {
Lang string
Path string
}
// Config represents a config struct.
type Config struct {
Download bool
ID string
Files []File
Path string
Token string
Type string
Upload bool
}
// body represents the response from POEditor api.
type body struct {
Response struct {
Code string
Message string
Status string
}
Result struct {
URL string
}
}
// getConfig will return config instance.
func getConfig() Config {
path := "config.json"
if len(*configFlag) > 0 {
path = *configFlag
}
file, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("File error: %v\n", err)
return Config{}
}
var config Config
json.Unmarshal(file, &config)
return config
}
// uploadRequest will do a request to POEditor with pot file.
func uploadRequest(token, id, path string) {
file, err := os.Open(path)
if err != nil {
fmt.Println("Error while reading file", err)
return
}
defer file.Close()
form := &bytes.Buffer{}
writer := multipart.NewWriter(form)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
fmt.Println("Error while doing writer", err)
return
}
_, err = io.Copy(part, file)
if err != nil {
fmt.Println("Error while doing copy", err)
return
}
writer.WriteField("api_token", token)
writer.WriteField("id", id)
writer.WriteField("updating", "terms")
err = writer.Close()
if err != nil {
fmt.Println("Error while doing writer", err)
return
}
req, err := http.NewRequest("POST", "https://api.poeditor.com/v2/projects/upload", form)
req.Header.Add("Content-Type", writer.FormDataContentType())
if err != nil {
fmt.Println("Error while creating request", err)
return
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println("Error while doing request", err)
return
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var b *body
err = decoder.Decode(&b)
if err != nil {
fmt.Println("Error while decoding", err)
return
}
fmt.Println("Upload response:", b.Response.Status, "-", b.Response.Message)
}
// downloadRequest will do a request to POEditor and get
// the download file url.
func downloadRequest(token, id, lang, typ string) *body {
form := url.Values{}
if typ == "" {
typ = "mo"
}
form.Add("api_token", token)
form.Add("id", id)
form.Add("type", typ)
form.Add("language", lang)
req, err := http.NewRequest("POST", "https://api.poeditor.com/v2/projects/export", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
fmt.Println("Error while creating request", err)
return nil
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println("Error while doing request", err)
return nil
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var b *body
err = decoder.Decode(&b)
if err != nil {
fmt.Println("Error while decoding", err)
return nil
}
return b
}
// downloadFromURL will download a file from url to a file.
func downloadFromURL(path, url string) {
output, err := os.Create(path)
if err != nil {
fmt.Println("Error while creating", path, "-", err)
return
}
defer output.Close()
res, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
defer res.Body.Close()
_, err = io.Copy(output, res.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("posync version %s\n", version)
os.Exit(0)
}
config := getConfig()
if config.Upload {
uploadRequest(config.Token, config.ID, config.Path)
}
if config.Download {
for _, file := range config.Files {
body := downloadRequest(config.Token, config.ID, file.Lang, config.Type)
if body == nil {
continue
}
if body.Response.Code == "200" {
downloadFromURL(file.Path, body.Result.URL)
fmt.Println("Download response:", file.Path, "downloaded")
} else {
fmt.Println("Failed response from POEditor:", body.Response.Message, "-", file.Lang)
}
}
}
}