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 #73 from kintone/SSR-744
Browse files Browse the repository at this point in the history
fix loop seek method when input -c option
  • Loading branch information
josh-vo authored May 7, 2020
2 parents ba42b01 + 4e68f80 commit 04a56f3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
40 changes: 22 additions & 18 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ func escapeCol(s string) string {
return strings.Replace(s, "\"", "\"\"", -1)
}

func exportRecordsBySeekMethod(app *kintone.App, writer io.Writer, fields []string) error {
func exportRecordsBySeekMethod(app *kintone.App, writer io.Writer, fields []string, isAppendIdCustome bool) error {
row, err := getRow(app)
hasTable := hasSubTable(row)
if err != nil {
return err
}

if config.Format == "json" {
err := writeRecordsBySeekMethodForJson(app, 0, writer, 0, fields, true)
err := writeRecordsBySeekMethodForJson(app, 0, writer, 0, fields, true, isAppendIdCustome)
return err
}
return writeRecordsBySeekMethodForCsv(app, 0, writer, row, hasTable, 0, fields, true)
return writeRecordsBySeekMethodForCsv(app, 0, writer, row, hasTable, 0, fields, true, isAppendIdCustome)
}

func exportRecordsWithQuery(app *kintone.App, fields []string, writer io.Writer) error {
Expand All @@ -154,7 +154,7 @@ func exportRecords(app *kintone.App, fields []string, writer io.Writer) error {
checkNoRecord(records)
if config.Format == "json" {
fmt.Fprint(writer, "{\"records\": [\n")
_, err = writeRecordsJSON(app, writer, records, 0)
_, err = writeRecordsJSON(app, writer, records, 0, false)
if err != nil {
return err
}
Expand All @@ -166,7 +166,7 @@ func exportRecords(app *kintone.App, fields []string, writer io.Writer) error {
if err != nil {
return err
}
_, err = writeRecordsCsv(app, writer, records, row, hasTable, 0)
_, err = writeRecordsCsv(app, writer, records, row, hasTable, 0, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func exportRecordsByCursorForJSON(app *kintone.App, fields []string, writer io.W
if index == 0 {
fmt.Fprint(writer, "{\"records\": [\n")
}
index, err = writeRecordsJSON(app, writer, recordsCursor.Records, index)
index, err = writeRecordsJSON(app, writer, recordsCursor.Records, index, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func exportRecordsByCursorForCsv(app *kintone.App, fields []string, writer io.Wr
if err != nil {
return err
}
index, err = writeRecordsCsv(app, writer, recordsCursor.Records, row, hasTable, index)
index, err = writeRecordsCsv(app, writer, recordsCursor.Records, row, hasTable, index, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -444,16 +444,20 @@ func writeHeaderCsv(writer io.Writer, hasTable bool, row Row) {
fmt.Fprint(writer, "\r\n")
}

func writeRecordsJSON(app *kintone.App, writer io.Writer, records []*kintone.Record, i uint64) (uint64, error) {
func writeRecordsJSON(app *kintone.App, writer io.Writer, records []*kintone.Record, i uint64, isAppendIdCustome bool) (uint64, error) {
for _, record := range records {
if i > 0 {
fmt.Fprint(writer, ",\n")
}
rowID := record.Id()
if rowID == 0 || isAppendIdCustome {
rowID = i
}
// Download file to local folder that is the value of param -b
for fieldCode, fieldInfo := range record.Fields {
fieldType := reflect.TypeOf(fieldInfo).String()
if fieldType == "kintone.FileField" {
dir := fmt.Sprintf("%s-%d", fieldCode, record.Id())
dir := fmt.Sprintf("%s-%d", fieldCode, rowID)
err := downloadFile(app, fieldInfo, dir)
if err != nil {
return 0, err
Expand All @@ -464,7 +468,7 @@ func writeRecordsJSON(app *kintone.App, writer io.Writer, records []*kintone.Rec
for subTableIndex, subTableValue := range subTable {
for fieldCodeInSubTable, fieldValueInSubTable := range subTableValue.Fields {
if reflect.TypeOf(fieldValueInSubTable).String() == "kintone.FileField" {
dir := fmt.Sprintf("%s-%d-%d", fieldCodeInSubTable, record.Id(), subTableIndex)
dir := fmt.Sprintf("%s-%d-%d", fieldCodeInSubTable, rowID, subTableIndex)
err := downloadFile(app, fieldValueInSubTable, dir)
if err != nil {
return 0, err
Expand All @@ -486,13 +490,13 @@ func writeRecordsJSON(app *kintone.App, writer io.Writer, records []*kintone.Rec
return i, nil
}

func writeRecordsCsv(app *kintone.App, writer io.Writer, records []*kintone.Record, row Row, hasTable bool, i uint64) (uint64, error) {
func writeRecordsCsv(app *kintone.App, writer io.Writer, records []*kintone.Record, row Row, hasTable bool, i uint64, isAppendIdCustome bool) (uint64, error) {
if i == 0 {
writeHeaderCsv(writer, hasTable, row)
}
for _, record := range records {
rowID := record.Id()
if rowID == 0 {
if rowID == 0 || isAppendIdCustome {
rowID = i
}

Expand Down Expand Up @@ -568,37 +572,37 @@ func writeRecordsCsv(app *kintone.App, writer io.Writer, records []*kintone.Reco
return i, nil
}

func writeRecordsBySeekMethodForCsv(app *kintone.App, id uint64, writer io.Writer, row Row, hasTable bool, index uint64, fields []string, isRecordFound bool) error {
func writeRecordsBySeekMethodForCsv(app *kintone.App, id uint64, writer io.Writer, row Row, hasTable bool, index uint64, fields []string, isRecordFound bool, isAppendIdCustome bool) error {
records, err := getRecordsForSeekMethod(app, id, fields, isRecordFound)
if err != nil {
return err
}
index, err = writeRecordsCsv(app, writer, records, row, hasTable, index)
index, err = writeRecordsCsv(app, writer, records, row, hasTable, index, isAppendIdCustome)
if err != nil {
return err
}
if len(records) == EXPORT_ROW_LIMIT {
isRecordFound = false
return writeRecordsBySeekMethodForCsv(app, records[len(records)-1].Id(), writer, row, hasTable, index, fields, isRecordFound)
return writeRecordsBySeekMethodForCsv(app, records[len(records)-1].Id(), writer, row, hasTable, index, fields, isRecordFound, isAppendIdCustome)
}
return nil
}

func writeRecordsBySeekMethodForJson(app *kintone.App, id uint64, writer io.Writer, index uint64, fields []string, isRecordsNotFound bool) error {
func writeRecordsBySeekMethodForJson(app *kintone.App, id uint64, writer io.Writer, index uint64, fields []string, isRecordsNotFound bool, isAppendIdCustome bool) error {
records, err := getRecordsForSeekMethod(app, id, fields, isRecordsNotFound)
if err != nil {
return err
}
if index == 0 {
fmt.Fprint(writer, "{\"records\": [\n")
}
index, err = writeRecordsJSON(app, writer, records, index)
index, err = writeRecordsJSON(app, writer, records, index, isAppendIdCustome)
if err != nil {
return err
}
if len(records) == EXPORT_ROW_LIMIT {
isRecordsNotFound = false
return writeRecordsBySeekMethodForJson(app, records[len(records)-1].Id(), writer, index, fields, isRecordsNotFound)
return writeRecordsBySeekMethodForJson(app, records[len(records)-1].Id(), writer, index, fields, isRecordsNotFound, isAppendIdCustome)
}
fmt.Fprint(writer, "\n]}")
return nil
Expand Down
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"fmt"
"log"
"os"
"strings"
"runtime"
"strings"

"github.com/howeyc/gopass"
"github.com/kintone/go-kintone"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"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"
)
Expand Down Expand Up @@ -124,6 +124,15 @@ func getColumn(code string, fields map[string]*kintone.FieldInfo) *Column {
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 {
Expand Down Expand Up @@ -268,7 +277,14 @@ func main() {
if config.Query != "" {
err = exportRecordsWithQuery(app, config.Fields, writer)
} else {
err = exportRecordsBySeekMethod(app, writer, config.Fields)
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)
Expand All @@ -295,7 +311,13 @@ func main() {
if config.Query != "" {
err = exportRecordsWithQuery(app, config.Fields, writer)
} else {
err = exportRecordsBySeekMethod(app, writer, config.Fields)
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 {
Expand Down

0 comments on commit 04a56f3

Please sign in to comment.