forked from kintone-labs/cli-kintone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (199 loc) · 5.15 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
package main
import (
"flag"
"fmt"
"log"
"strings"
"os"
"github.com/kintone/go-kintone"
"github.com/howeyc/gopass"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/unicode"
)
type Configure struct {
login string
password string
basicAuthUser string
basicAuthPassword string
apiToken string
domain string
basic string
format string
query string
appId uint64
fields []string
filePath string
deleteAll bool
encoding string
guestSpaceId uint64
fileDir string
}
var config Configure
const IMPORT_ROW_LIMIT = 100
const EXPORT_ROW_LIMIT = 500
type Column struct {
Code string
Type string
IsSubField bool
Table string
}
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 colNames string
flag.StringVar(&config.login, "u", "", "Login name")
flag.StringVar(&config.password, "p", "", "Password")
flag.StringVar(&config.basicAuthUser, "U", "", "Basic authentication user name")
flag.StringVar(&config.basicAuthPassword, "P", "", "Basic authentication password")
flag.StringVar(&config.domain, "d", "", "Domain name")
flag.StringVar(&config.apiToken, "t", "", "API token")
flag.Uint64Var(&config.appId, "a", 0, "App ID")
flag.Uint64Var(&config.guestSpaceId, "g", 0, "Guest Space ID")
flag.StringVar(&config.format, "o", "csv", "Output format: 'json' or 'csv'(default)")
flag.StringVar(&config.query, "q", "", "Query string")
flag.StringVar(&colNames, "c", "", "Field names (comma separated)")
flag.StringVar(&config.filePath, "f", "", "Input file path")
flag.BoolVar(&config.deleteAll, "D", false, "Delete all records before insert")
flag.StringVar(&config.encoding, "e", "utf-8", "Character encoding: 'utf-8'(default), 'utf-16', 'utf-16be-with-signature', 'utf-16le-with-signature', 'sjis' or 'euc-jp'")
flag.StringVar(&config.fileDir, "b", "", "Attachment file directory")
flag.Parse()
if config.appId == 0 || (config.apiToken == "" && (config.domain == "" || config.login == "")) {
flag.PrintDefaults()
return
}
if !strings.Contains(config.domain, ".") {
config.domain += ".cybozu.com"
}
if colNames != "" {
config.fields = strings.Split(colNames, ",")
for i, field := range config.fields {
config.fields[i] = strings.TrimSpace(field)
}
}
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)
}
var err error
if config.filePath == "" {
if config.format == "json" {
err = writeJson(app, os.Stdout)
} else {
err = writeCsv(app, os.Stdout)
}
} else {
var file *os.File
file, err = os.Open(config.filePath)
if err == nil {
defer file.Close()
err = readCsv(app, file)
}
}
if err != nil {
log.Fatal(err)
}
}