diff --git a/README.md b/README.md index 2ee870d..c15c1c3 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Getting the source code Install dependencies - $ export GOPATH=/path/to/your/Go/project/folder + $ cd ${GOPATH}/src/cli-kintone $ go get github.com/mattn/gom $ sudo ln -s $GOPATH/bin/gom /usr/local/bin/gom $ gom -production install @@ -30,6 +30,7 @@ Install dependencies build $ cd ${GOPATH}/src/cli-kintone + $ sudo mv vendor/ src $ gom build ## Downloads diff --git a/export.go b/export.go index 9cfca2f..23e454c 100644 --- a/export.go +++ b/export.go @@ -80,9 +80,12 @@ func writeJSON(app *kintone.App, _writer io.Writer) error { } } + subTableValue.Fields[fieldCodeInSubTable] = transformEncodingJSONValue(fieldValueInSubTable) } } } + record.Fields[fieldCode] = transformEncodingJSONValue(fieldInfo) + } jsonArray, _ := record.MarshalJSON() json := string(jsonArray) @@ -97,6 +100,78 @@ func writeJSON(app *kintone.App, _writer io.Writer) error { return nil } +func transformEncodingJSONValue(fields interface{}) interface{} { + var fieldEncodingValues interface{} + switch fields.(type) { + case kintone.SingleLineTextField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldEncodingValues = kintone.SingleLineTextField(fieldStringValues) + case kintone.MultiLineTextField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldEncodingValues = kintone.MultiLineTextField(fieldStringValues) + case kintone.RichTextField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldEncodingValues = kintone.RichTextField(fieldStringValues) + case kintone.SingleSelectField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + if fieldStringValues == "" { + fieldEncodingValues = kintone.SingleSelectField{Valid: false} + } else { + fieldEncodingValues = kintone.SingleSelectField{fieldStringValues, true} + } + case kintone.CheckBoxField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldStringValuesArray := strings.Split(fieldStringValues, "\n") + fieldEncodingValues = kintone.CheckBoxField(fieldStringValuesArray) + case kintone.RadioButtonField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldEncodingValues = kintone.RadioButtonField(fieldStringValues) + case kintone.MultiSelectField: + fieldStringValues, _ := transformStringFromEncoding(toString(fields, "\n")) + fieldStringValuesArray := strings.Split(fieldStringValues, "\n") + fieldEncodingValues = kintone.MultiSelectField(fieldStringValuesArray) + case kintone.UserField: + var userList []kintone.User + userField := fields.(kintone.UserField) + for _, user := range userField { + user.Code, _ = transformStringFromEncoding(user.Code) + user.Name, _ = transformStringFromEncoding(user.Name) + userList = append(userList, user) + } + fieldEncodingValues = kintone.UserField(userList) + case kintone.OrganizationField: + var organizations []kintone.Organization + organizationField := fields.(kintone.OrganizationField) + for _, organization := range organizationField { + organization.Code, _ = transformStringFromEncoding(organization.Code) + organization.Name, _ = transformStringFromEncoding(organization.Name) + organizations = append(organizations, organization) + } + fieldEncodingValues = kintone.OrganizationField(organizations) + case kintone.GroupField: + var groups []kintone.Group + groupField := fields.(kintone.GroupField) + for _, group := range groupField { + group.Code, _ = transformStringFromEncoding(group.Code) + group.Name, _ = transformStringFromEncoding(group.Name) + groups = append(groups, group) + } + fieldEncodingValues = kintone.GroupField(groupField) + case kintone.CreatorField: + creatorField := fields.(kintone.CreatorField) + creatorField.Code, _ = transformStringFromEncoding(creatorField.Code) + creatorField.Name, _ = transformStringFromEncoding(creatorField.Name) + fieldEncodingValues = kintone.CreatorField(creatorField) + case kintone.ModifierField: + modifierField := fields.(kintone.ModifierField) + modifierField.Code, _ = transformStringFromEncoding(modifierField.Code) + modifierField.Name, _ = transformStringFromEncoding(modifierField.Name) + fieldEncodingValues = kintone.ModifierField(modifierField) + default: + fieldEncodingValues = fields + } + return fieldEncodingValues +} func makeColumns(fields map[string]*kintone.FieldInfo) Columns { columns := make([]*Column, 0) @@ -268,7 +343,10 @@ func writeCsv(app *kintone.App, _writer io.Writer) error { return err } } - fmt.Fprint(writer, "\""+escapeCol(toString(subField, "\n"))+"\"") + subFieldEncoding, _ := transformStringFromEncoding(toString(subField, "\n")) + fmt.Fprint(writer, "\"") + fmt.Fprint(writer, escapeCol(subFieldEncoding)) + fmt.Fprint(writer, "\"") } } else { field := record.Fields[f.Code] @@ -280,7 +358,10 @@ func writeCsv(app *kintone.App, _writer io.Writer) error { return err } } - fmt.Fprint(writer, "\""+escapeCol(toString(field, "\n"))+"\"") + fieldEncoding, _ := transformStringFromEncoding(toString(field, "\n")) + fmt.Fprint(writer, "\"") + fmt.Fprint(writer, escapeCol(fieldEncoding)) + fmt.Fprint(writer, "\"") } } k++ @@ -512,6 +593,8 @@ func toString(f interface{}, delimiter string) string { userField := f.(kintone.UserField) users := make([]string, 0, len(userField)) for _, user := range userField { + user.Code, _ = transformStringFromEncoding(user.Code) + user.Name, _ = transformStringFromEncoding(user.Name) users = append(users, user.Code) } return strings.Join(users, delimiter) @@ -519,6 +602,8 @@ func toString(f interface{}, delimiter string) string { organizationField := f.(kintone.OrganizationField) organizations := make([]string, 0, len(organizationField)) for _, organization := range organizationField { + organization.Code, _ = transformStringFromEncoding(organization.Code) + organization.Name, _ = transformStringFromEncoding(organization.Name) organizations = append(organizations, organization.Code) } return strings.Join(organizations, delimiter) @@ -526,6 +611,8 @@ func toString(f interface{}, delimiter string) string { groupField := f.(kintone.GroupField) groups := make([]string, 0, len(groupField)) for _, group := range groupField { + group.Code, _ = transformStringFromEncoding(group.Code) + group.Name, _ = transformStringFromEncoding(group.Name) groups = append(groups, group.Code) } return strings.Join(groups, delimiter) diff --git a/main.go b/main.go index 4d7a844..533a7d9 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "io" + "io/ioutil" "log" "os" "strings" @@ -11,6 +13,7 @@ import ( "golang.org/x/text/encoding" "golang.org/x/text/encoding/japanese" "golang.org/x/text/encoding/unicode" + "golang.org/x/text/transform" flags "github.com/jessevdk/go-flags" ) @@ -269,3 +272,20 @@ func importDataFromFile(app *kintone.App) error { } return err } + +func transformStringFromEncoding(str string) (string, error) { + transformString := str + encoding := getEncoding() + if encoding != nil { + return transformEncoding(strings.NewReader(str), encoding.NewEncoder()) + } + return transformString, nil +} + +func transformEncoding(rawReader io.Reader, trans transform.Transformer) (string, error) { + ret, err := ioutil.ReadAll(transform.NewReader(rawReader, trans)) + if err == nil { + return string(ret), nil + } + return string(ret), err +}