forked from kintone-labs/cli-kintone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
271 lines (239 loc) · 7.17 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
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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/howeyc/gopass"
"github.com/kintone/go-kintone"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"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.9.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 {
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. Specify one of the following -> 'utf-8'(default), 'utf-16', 'utf-16be-with-signature', 'utf-16le-with-signature', 'sjis' or 'euc-jp'"`
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"`
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"`
}
var config Configure
// Column config
type Column struct {
Code string
Type string
IsSubField bool
Table string
}
// Columns config
type Columns []*Column
func (p Columns) Len() int {
return len(p)
}
func (p Columns) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Columns) Less(i, j int) bool {
p1 := p[i]
code1 := p1.Code
if p1.IsSubField {
code1 = p1.Table
}
p2 := p[j]
code2 := p2.Code
if p2.IsSubField {
code2 = p2.Table
}
if code1 == code2 {
return p[i].Code < p[j].Code
}
return code1 < code2
}
func getFields(app *kintone.App) (map[string]*kintone.FieldInfo, error) {
fields, err := app.Fields()
if err != nil {
return nil, err
}
return fields, nil
}
// set column information from fieldinfo
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 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
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.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)
}
// Old logic without force import/export
if config.IsImport == false && config.IsExport == false {
if config.FilePath == "" {
if config.Format == "json" {
err = writeJSON(app, os.Stdout)
} else {
err = writeCsv(app, os.Stdout)
}
} 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.")
}
if config.Format == "json" {
err = writeJSON(app, os.Stdout)
} else {
err = writeCsv(app, os.Stdout)
}
}
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
}