This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.go
More file actions
354 lines (314 loc) · 9.54 KB
/
main.go
File metadata and controls
354 lines (314 loc) · 9.54 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"fmt"
"log"
"os"
"runtime"
"strings"
"github.com/howeyc/gopass"
"github.com/kintone-labs/go-kintone"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/unicode"
flags "github.com/jessevdk/go-flags"
)
// NAME of this package
const NAME = "cli-kintone"
// VERSION of this package
const VERSION = "0.14.1"
// IMPORT_ROW_LIMIT The maximum row will be import
const IMPORT_ROW_LIMIT = 100
// EXPORT_ROW_LIMIT The maximum row will be export
const EXPORT_ROW_LIMIT = 500
// Configure of this package
type Configure struct {
IsImport bool `long:"import" description:"Import data from stdin. If \"-f\" is also specified, data is imported from the file instead"`
IsExport bool `long:"export" description:"Export kintone data to stdout"`
Domain string `short:"d" default:"" description:"Domain name (specify the FQDN)"`
AppID uint64 `short:"a" default:"0" description:"App ID"`
Login string `short:"u" default:"" description:"User's log in name"`
Password string `short:"p" default:"" description:"User's password"`
APIToken string `short:"t" default:"" description:"API token"`
GuestSpaceID uint64 `short:"g" default:"0" description:"Guest Space ID"`
Format string `short:"o" default:"csv" description:"Output format. Specify either 'json' or 'csv'"`
Encoding string `short:"e" default:"utf-8" description:"Character encoding (default: utf-8).\n Only support the encoding below both field code and data itself: \n 'utf-8', 'utf-16', 'utf-16be-with-signature', 'utf-16le-with-signature', 'sjis' or 'euc-jp', 'gbk' or 'big5'"`
BasicAuthUser string `short:"U" default:"" description:"Basic authentication user name"`
BasicAuthPassword string `short:"P" default:"" description:"Basic authentication password"`
Query string `short:"q" default:"" description:"Query string"`
Fields []string `short:"c" description:"Fields to export (comma separated). Specify the field code name"`
FilePath string `short:"f" default:"" description:"Input file path"`
FileDir string `short:"b" default:"" description:"Attachment file directory"`
DeleteAll bool `short:"D" description:"Delete records before insert. You can specify the deleting record condition by option \"-q\""`
Line uint64 `short:"l" default:"1" description:"Position index of data in the input file"`
Version bool `short:"v" long:"version" description:"Version of cli-kintone"`
}
var config Configure
// Column config
// Column config is deprecated, replace using Cell config
type Column struct {
Code string
Type string
IsSubField bool
Table string
}
// Columns config
// Columns config is deprecated, replace using Row config
type Columns []*Column
// Cell config
type Cell struct {
Code string
Type string
IsSubField bool
Table string
Index int
}
// Row config
type Row []*Cell
func getFields(app *kintone.App) (map[string]*kintone.FieldInfo, error) {
fields, err := app.Fields()
if err != nil {
return nil, err
}
return fields, nil
}
func getSupportedFields(app *kintone.App) (map[string]*kintone.FieldInfo, error) {
fields, err := getFields(app)
if err != nil {
return nil, err
}
for key, field := range fields {
switch field.Type {
case "STATUS_ASSIGNEE", "CATEGORY", "STATUS":
delete(fields, key)
default:
continue
}
}
return fields, nil
}
// set column information from fieldinfo
// This function is deprecated, replace using function getCell
func getColumn(code string, fields map[string]*kintone.FieldInfo) *Column {
// initialize values
column := Column{Code: code, IsSubField: false, Table: ""}
if code == "$id" {
column.Type = kintone.FT_ID
return &column
} else if code == "$revision" {
column.Type = kintone.FT_REVISION
return &column
} else {
// is this code the one of sub field?
for _, val := range fields {
if val.Code == code {
column.Type = val.Type
return &column
}
if val.Type == kintone.FT_SUBTABLE {
for _, subField := range val.Fields {
if subField.Code == code {
column.IsSubField = true
column.Type = subField.Type
column.Table = val.Code
return &column
}
}
}
}
}
// the code is not found
column.Type = "UNKNOWN"
return &column
}
func containtString(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
// set Cell information from fieldinfo
// function replace getColumn so getColumn is invalid name
func getCell(code string, fields map[string]*kintone.FieldInfo) *Cell {
// initialize values
cell := Cell{Code: code, IsSubField: false, Table: ""}
if code == "$id" {
cell.Type = kintone.FT_ID
return &cell
} else if code == "$revision" {
cell.Type = kintone.FT_REVISION
return &cell
} else {
// is this code the one of sub field?
for _, val := range fields {
if val.Code == code {
cell.Type = val.Type
return &cell
}
if val.Type == kintone.FT_SUBTABLE {
for _, subField := range val.Fields {
if subField.Code == code {
cell.IsSubField = true
cell.Type = subField.Type
cell.Table = val.Code
return &cell
}
}
}
}
}
// the code is not found
cell.Type = "UNKNOWN"
return &cell
}
func getEncoding() encoding.Encoding {
switch config.Encoding {
case "utf-16":
return unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
case "utf-16be-with-signature":
return unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM)
case "utf-16le-with-signature":
return unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM)
case "euc-jp":
return japanese.EUCJP
case "sjis":
return japanese.ShiftJIS
case "gbk":
return simplifiedchinese.GBK
case "big5":
return traditionalchinese.Big5
default:
return nil
}
}
func main() {
var err error
_, err = flags.ParseArgs(&config, os.Args[1:])
if err != nil {
if os.Args[1] != "-h" && os.Args[1] != "--help" {
fileExecute := os.Args[0]
fmt.Printf("\nTry '%s --help' for more information.\n", fileExecute)
}
os.Exit(1)
}
if len(os.Args) > 0 && config.Version {
fmt.Println(VERSION)
os.Exit(0)
}
if len(os.Args) == 0 || config.AppID == 0 || (config.APIToken == "" && (config.Domain == "" || config.Login == "")) {
helpArg := []string{"-h"}
flags.ParseArgs(&config, helpArg)
os.Exit(1)
}
if !strings.Contains(config.Domain, ".") {
config.Domain += ".cybozu.com"
}
// Support set columm with comma separated (",") in arg
var cols []string
if len(config.Fields) > 0 {
for _, field := range config.Fields {
curField := strings.Split(field, ",")
cols = append(cols, curField...)
}
config.Fields = nil
for _, col := range cols {
curFieldString := strings.TrimSpace(col)
if curFieldString != "" {
config.Fields = append(config.Fields, curFieldString)
}
}
}
var app *kintone.App
if config.BasicAuthUser != "" && config.BasicAuthPassword == "" {
fmt.Printf("Basic authentication password: ")
pass, _ := gopass.GetPasswd()
config.BasicAuthPassword = string(pass)
}
if config.APIToken == "" {
if config.Password == "" {
fmt.Printf("Password: ")
pass, _ := gopass.GetPasswd()
config.Password = string(pass)
}
app = &kintone.App{
Domain: config.Domain,
User: config.Login,
Password: config.Password,
AppId: config.AppID,
GuestSpaceId: config.GuestSpaceID,
}
} else {
app = &kintone.App{
Domain: config.Domain,
ApiToken: config.APIToken,
AppId: config.AppID,
GuestSpaceId: config.GuestSpaceID,
}
}
if config.BasicAuthUser != "" {
app.SetBasicAuth(config.BasicAuthUser, config.BasicAuthPassword)
}
app.SetUserAgentHeader(NAME + "/" + VERSION + " (" + runtime.GOOS + " " + runtime.GOARCH + ")")
// Old logic without force import/export
if config.IsImport == false && config.IsExport == false {
if config.FilePath == "" {
writer := getWriter(os.Stdout)
if config.Query != "" {
err = exportRecordsWithQuery(app, config.Fields, writer)
} else {
fields := config.Fields
isAppendIdCustome := false
if len(config.Fields) > 0 && !containtString(config.Fields, "$id") {
fields = append(fields, "$id")
isAppendIdCustome = true
}
err = exportRecordsBySeekMethod(app, writer, fields, isAppendIdCustome)
}
} else {
err = importDataFromFile(app)
}
}
if config.IsImport && config.IsExport {
log.Fatal("The options --import and --export cannot be specified together!")
}
if config.IsImport {
if config.FilePath == "" {
err = importFromCSV(app, os.Stdin)
} else {
err = importDataFromFile(app)
}
}
if config.IsExport {
if config.FilePath != "" {
log.Fatal("The -f option is not supported with the --export option.")
}
writer := getWriter(os.Stdout)
if config.Query != "" {
err = exportRecordsWithQuery(app, config.Fields, writer)
} else {
fields := config.Fields
isAppendIdCustome := false
if len(config.Fields) > 0 && !containtString(config.Fields, "$id") {
fields = append(fields, "$id")
isAppendIdCustome = true
}
err = exportRecordsBySeekMethod(app, writer, fields, isAppendIdCustome)
}
}
if err != nil {
log.Fatal(err)
}
}
func importDataFromFile(app *kintone.App) error {
var file *os.File
var err error
file, err = os.Open(config.FilePath)
if err == nil {
defer file.Close()
err = importFromCSV(app, file)
}
return err
}