Skip to content

Commit

Permalink
endpoint improvements & bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Telgerov committed Oct 11, 2019
1 parent 4ffee15 commit 1c08597
Show file tree
Hide file tree
Showing 12 changed files with 629 additions and 27 deletions.
47 changes: 36 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ lokalise

create
--project-id string*
--title string*
--description string
--due-date string
--keys array
Expand All @@ -361,11 +362,11 @@ lokalise

retrieve
--project-id string*
--task-id int
--task-id int*

update
--project-id string*
--task-id int
--task-id int*
--title string
--description string
--due-date string
Expand All @@ -378,7 +379,7 @@ lokalise

delete
--project-id string*
--task-id int
--task-id int*

team
list
Expand Down Expand Up @@ -428,22 +429,22 @@ lokalise
add-projects
--team-id int*
--group-id int*
--projects array
--projects array*

remove-projects
--team-id int*
--group-id int*
--projects array
--projects array*

add-members
--team-id int*
--group-id int*
--users array
--users array*

remove-members
--team-id int*
--group-id int*
--users array
--users array*

delete
--team-id int*
Expand All @@ -468,7 +469,8 @@ lokalise
--translation-id int*
--translation string*
--is-fuzzy bool
--is-reviewed bool
--is-reviewed bool
--custom-translation-status-ids array

translation-provider
list
Expand All @@ -484,8 +486,8 @@ lokalise

create
--project-id string*
--title string
--color string
--title string*
--color string*

retrieve
--project-id string*
Expand All @@ -503,5 +505,28 @@ lokalise

retrieve-colors


webhook
list
--project-id string*
retrieve
--project-id string*
--webhook-id string*
create
--project-id string*
--url string*
--events array*
--event-lang-map json
update
--project-id string*
--webhook-id string*
--url string
--events array
--event-lang-map json
delete
--project-id string*
--webhook-id string*

132 changes: 125 additions & 7 deletions cmd/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package cmd

import (
"archive/zip"
"encoding/base64"
"fmt"
"github.com/lokalise/go-lokalise-api"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
)

var (
Expand Down Expand Up @@ -49,18 +58,37 @@ var fileUploadCmd = &cobra.Command{
Short: "Upload a file",
Long: "Imports a localization file to the project. Requires Upload files admin right. List of supported file formats is available here https://docs.lokalise.com/en/collections/652248-supported-file-formats",
RunE: func(*cobra.Command, []string) error {
f := Api.Files()

// preparing opts
uploadOpts.ConvertPlaceholders = &uploadOptsConvertPlaceholders
uploadOpts.TagInsertedKeys = &uploadOptsTagInsertedKeys
uploadOpts.TagUpdatedKeys = &uploadOptsTagUpdatedKeys

f := Api.Files()
f.SetDebug(true)
resp, err := f.Upload(projectId, uploadOpts)
files, err := filepath.Glob(uploadFile)
if err != nil {
return err
}
return printJson(resp)

for _, file := range files {
fmt.Println("file", file, "is in progress...")
buf, err := ioutil.ReadFile(file)
if err != nil {
return err
}

uploadOpts.Data = base64.StdEncoding.EncodeToString(buf)
uploadOpts.Filename = path.Base(file)

resp, err := f.Upload(projectId, uploadOpts)
if err != nil {
return err
}

_ = printJson(resp)
}

return nil
},
}

Expand All @@ -74,7 +102,17 @@ var fileDownloadCmd = &cobra.Command{
if err != nil {
return err
}
return printJson(resp)

if downloadJsonOnly {
return printJson(resp)
}

err = downloadAndUnzip(resp.BundleURL, downloadDestination, downloadUnzipTo)
if err != nil {
return err
}

return nil
},
}

Expand All @@ -92,8 +130,8 @@ func init() {

fs.BoolVar(&downloadJsonOnly, "json-only", false, "Should only the API JSON response be returned.")
fs.BoolVar(&downloadKeepZip, "keep-zip", false, "Keep or delete ZIP file after being unpacked.")
fs.StringVar(&downloadDestination, "dest", "/tmp", "Destination folder for ZIP file.")
fs.StringVar(&downloadUnzipTo, "unzip-to", "", "Unzip to this folder.")
fs.StringVar(&downloadDestination, "dest", "./", "Destination folder for ZIP file.")
fs.StringVar(&downloadUnzipTo, "unzip-to", "./", "Unzip to this folder.")

fs.BoolVar(&downloadOpts.OriginalFilenames, "original-filenames", true, "Enable to use original filenames/formats. If set to false all keys will be export to a single file per language.")
fs.StringVar(&downloadOpts.BundleStructure, "bundle-structure", "", "Bundle structure, used when original-filenames set to false. Allowed placeholders are %LANG_ISO%, %LANG_NAME%, %FORMAT% and %PROJECT_NAME%).")
Expand Down Expand Up @@ -148,3 +186,83 @@ func init() {
fs.BoolVar(&uploadOpts.ApplyTM, "apply-tm", false, "Enable to automatically apply 100% translation memory matches.")
fs.BoolVar(&uploadOpts.CleanupMode, "cleanup-mode", false, "Enable to delete all keys with all language translations that are not present in the uploaded file. You may want to make a snapshot of the project before importing new file, just in case.")
}

//noinspection GoUnhandledErrorResult
func downloadAndUnzip(srcUrl, destPath, unzipPath string) error {
fileName := path.Base(srcUrl)
zip, err := os.Create(path.Join(destPath, fileName))
if err != nil {
return err
}
defer zip.Close()

resp, err := http.Get(srcUrl)
if err != nil {
return err
}
defer resp.Body.Close()

_, err = io.Copy(zip, resp.Body)
if err != nil {
return err
}

err = unzip(zip.Name(), unzipPath)
if err != nil {
return err
}

if !downloadKeepZip {
_ = os.Remove(zip.Name())
}

return nil
}

//noinspection GoUnhandledErrorResult
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()

os.MkdirAll(dest, 0755)

// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()

path := filepath.Join(dest, f.Name)

if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()

_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}

for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}

return nil
}
1 change: 0 additions & 1 deletion cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var keyCreateCmd = &cobra.Command{
}

k := Api.Keys()
// k.SetDebug(false)
resp, err := k.Create(projectId, []lokalise.NewKey{newKey})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func init() {
flagTeamId(orderCmd)

// Create
flagProjectId(orderCreateCmd, false)
flagCardId(orderCreateCmd)
fs := orderCreateCmd.Flags()
fs.StringVar(&newOrder.Briefing, "briefing", "", "Order briefing (required).")
Expand Down
2 changes: 0 additions & 2 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var projectCreateCmd = &cobra.Command{
}

p := Api.Projects()
p.SetDebug(true)
resp, err := p.Create(newProject)
if err != nil {
return err
Expand All @@ -56,7 +55,6 @@ var projectListCmd = &cobra.Command{
RunE: func(*cobra.Command, []string) error {

p := Api.Projects()
p.SetDebug(true)
opts := lokalise.ProjectListOptions{
IncludeSettings: fmt.Sprintf("%d", includeSettings),
IncludeStat: fmt.Sprintf("%d", includeStatistics),
Expand Down
Loading

0 comments on commit 1c08597

Please sign in to comment.