Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from ryokdy/v0.5
Browse files Browse the repository at this point in the history
V0.5
  • Loading branch information
ushirosako committed Mar 11, 2016
2 parents c7f02ef + ce13939 commit e3c16b1
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 72 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cli-kintone is a command line utility for kintone.

## Version

0.4
0.5

## How to Build

Expand Down Expand Up @@ -46,10 +46,12 @@ https://github.com/kintone/cli-kintone/releases
-P="": Basic authentication password
-U="": Basic authentication user name
-a=0: App ID
-b="": Attachment file directory
-c="": Field names (comma separated)
-d="": Domain name
-e="utf-8": Character encoding: 'utf-8'(default), 'utf-16', 'utf-16be-with-signature', 'utf-16le-with-signature', 'sjis' or 'euc-jp'
-f="": Input file path
-g=0: Guest Space ID
-o="csv": Output format: 'json' or 'csv'(default)
-p="": Password
-q="": Query string
Expand All @@ -70,6 +72,14 @@ If the file has $id column, the original data will be updated. If not, new row w

$ cli-kintone -a <APP_ID> -d <DOMAIN_NAME> -e sjis -t <API_TOKEN> -f <INPUT_FILE>

Export and download attachment files to ./download directory.

$ cli-kintone -a <APP_ID> -d <DOMAIN_NAME> -t <API_TOKEN> -b download

Import and upload attachment files from ./upload directory.

$ cli-kintone -a <APP_ID> -d <DOMAIN_NAME> -t <API_TOKEN> -b upload -f <INPUT_FILE>

## Licence

GPL v2
Expand Down
77 changes: 75 additions & 2 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func makePartialColumns(fields map[string]*kintone.FieldInfo, partialFields []st
columns = append(columns, column)
}
}

return columns
}

Expand Down Expand Up @@ -138,7 +137,7 @@ func hasSubTable(columns []*Column) bool {
}

func writeCsv(app *kintone.App) error {
i := 0
i := uint64(0)
offset := int64(0)
writer := getWriter()
var columns Columns
Expand Down Expand Up @@ -180,6 +179,10 @@ func writeCsv(app *kintone.App) error {
}
fmt.Fprint(writer, "\r\n");
}
rowId := record.Id()
if rowId == 0 {
rowId = i
}

// determine subtable's row count
rowNum := getSubTableRowCount(record, columns)
Expand All @@ -206,11 +209,25 @@ func writeCsv(app *kintone.App) error {
table := record.Fields[f.Table].(kintone.SubTableField)
if j < len(table) {
subField := table[j].Fields[f.Code]
if f.Type == kintone.FT_FILE {
dir := fmt.Sprintf("%s-%d-%d", f.Code, rowId, j)
err := downloadFile(app, subField, dir)
if err != nil {
return err
}
}
fmt.Fprint(writer, "\"" + escapeCol(toString(subField, "\n")) + "\"")
}
} else {
field := record.Fields[f.Code]
if field != nil {
if j == 0 && f.Type == kintone.FT_FILE {
dir := fmt.Sprintf("%s-%d", f.Code, rowId)
err := downloadFile(app, field, dir)
if err != nil {
return err
}
}
fmt.Fprint(writer, "\"" + escapeCol(toString(field, "\n")) + "\"")
}
}
Expand All @@ -228,6 +245,62 @@ func writeCsv(app *kintone.App) error {
return nil
}

func downloadFile(app *kintone.App, field interface{}, dir string) error {
if config.fileDir == "" {
return nil
}

v, ok := field.(kintone.FileField)
if !ok {
return nil
}

if len(v) == 0 {
return nil
}

fileDir := fmt.Sprintf("%s%c%s", config.fileDir, os.PathSeparator, dir)
if err := os.MkdirAll(fileDir, 0777); err != nil {
return err
}

for idx, file := range v {
path := fmt.Sprintf("%s%c%s", fileDir, os.PathSeparator, file.Name)
data, err := app.Download(file.FileKey)
if err != nil {
return err
}

fo, err := os.Create(path)
if err != nil {
return err
}
defer fo.Close()

// make a buffer to keep chunks that are read
buf := make([]byte, 256 * 1024)
for {
// read a chunk
n, err := data.Reader.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}

// write a chunk
if _, err := fo.Write(buf[:n]); err != nil {
return err
}
}

v[idx].Name = fmt.Sprintf("%s%c%s", dir, os.PathSeparator, file.Name)
}

return nil
}

func escapeCol(s string) string {
return strings.Replace(s, "\"", "\"\"", -1)
}
Expand Down
Loading

0 comments on commit e3c16b1

Please sign in to comment.