Skip to content

Commit

Permalink
Merge branch global options into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Telgerov committed Oct 13, 2019
1 parent 1c08597 commit 8c440f7
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ dist/
.idea/
.DS_Store

# config file
config.*

# temporary disabled
go.sum
20 changes: 20 additions & 0 deletions cmd/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import "github.com/spf13/viper"

func init() {
parseConfig()
}

func parseConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".") // optionally look for config in the working directory
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignoring error
} else {
// incorrect config, etc
panic(err)
}
}
}
3 changes: 3 additions & 0 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func init() {
// general flags
flagProjectId(fileCmd, true)

// List
// todo list options

// Download
fs := fileDownloadCmd.Flags()
fs.StringVar(&downloadOpts.Format, "format", "", "File format (e.g. json, strings, xml). Must be file extension of any of the file formats we support. May also be ios_sdk or android_sdk for respective OTA SDK bundles. (required)")
Expand Down
4 changes: 3 additions & 1 deletion cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ var keyListCmd = &cobra.Command{
Short: "List all keys",
Long: "Lists all keys in the project.",
RunE: func(*cobra.Command, []string) error {
k := Api.Keys()
// preparing filters
keyListOpts.Limit = k.ListOpts().Limit
if filterUntranslated {
keyListOpts.FilterUntranslated = "1"
}

resp, err := Api.Keys().WithListOptions(keyListOpts).List(projectId)
resp, err := k.WithListOptions(keyListOpts).List(projectId)
if err != nil {
return err
}
Expand Down
18 changes: 13 additions & 5 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"encoding/json"
"fmt"

"github.com/lokalise/go-lokalise-api"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

var (
Expand Down Expand Up @@ -56,6 +57,7 @@ var projectListCmd = &cobra.Command{

p := Api.Projects()
opts := lokalise.ProjectListOptions{
Limit: p.ListOpts().Limit,
IncludeSettings: fmt.Sprintf("%d", includeSettings),
IncludeStat: fmt.Sprintf("%d", includeStatistics),
FilterTeamID: filterTeamID,
Expand Down Expand Up @@ -163,11 +165,17 @@ func init() {
}

func flagProjectId(cmd *cobra.Command, isPersistent bool) {
var fs *pflag.FlagSet
var defaultPID string

if isPersistent {
cmd.PersistentFlags().StringVar(&projectId, "project-id", "", "A unique project identifier (required).")
_ = cmd.MarkPersistentFlagRequired("project-id")
fs = cmd.PersistentFlags()
defaultPID = viper.GetString("project-id")
} else {
cmd.Flags().StringVar(&projectId, "project-id", "", "A unique project identifier (required).")
_ = cmd.MarkFlagRequired("project-id")
fs = cmd.Flags()
defaultPID = ""
}

fs.StringVar(&projectId, "project-id", defaultPID, "Unique project identifier (required).")
_ = cmd.MarkFlagRequired("project-id")
}
35 changes: 29 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/lokalise/go-lokalise-api"
"os"

"github.com/lokalise/go-lokalise-api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
Version = "2.0"
Version = "2.0"
DefaultPageLimit = 5000
)

var (
Expand All @@ -25,7 +27,20 @@ var rootCmd = &cobra.Command{
Version: Version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
// init Api, runs like a middleware
Api, err = lokalise.New(Token)
perPage := viper.GetUint("page-limit")
if perPage == 0 {
perPage = DefaultPageLimit
}

Api, err = lokalise.New(
viper.GetString("token"),

lokalise.WithDebug(viper.GetBool("debug")),
lokalise.WithRetryCount(viper.GetInt("retry-count")),
lokalise.WithRetryTimeout(viper.GetDuration("retry-timeout")),
lokalise.WithConnectionTimeout(viper.GetDuration("connection-timeout")),
lokalise.WithPageLimit(perPage),
)
return err
},
}
Expand All @@ -40,9 +55,17 @@ func Execute() {
}

func init() {
// API Token, used for all commands
rootCmd.PersistentFlags().StringVarP(&Token, "token", "t", "", "API token (required). You can create API tokens at https://lokalise.com/profile.")
_ = rootCmd.MarkPersistentFlagRequired("token")
// init API Token, used for all commands
if viper.GetString("token") == "" {
// if not found in config
rootCmd.PersistentFlags().StringVarP(&Token, "token", "t", "", "API token (required). You can create API tokens at https://lokalise.com/profile.")
_ = rootCmd.MarkPersistentFlagRequired("token")

} else {
rootCmd.PersistentFlags().StringVarP(&Token, "token", "t", "", "API token (override value from config if desired).")
}
// binding
_ = viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
}

// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand Down
3 changes: 3 additions & 0 deletions cmd/screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func init() {
// general flags
flagProjectId(screenshotCmd, true)

// List
// todo ScreenshotListOptions ?

// Create
fs := screenshotCreateCmd.Flags()
fs.StringVar(&newScreenshotFile, "file", "", "Path to a local image file (required).")
Expand Down
4 changes: 3 additions & 1 deletion cmd/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ var taskListCmd = &cobra.Command{
Short: "List all tasks",
Long: "Lists all tasks in the project.",
RunE: func(*cobra.Command, []string) error {
t := Api.Tasks()
t.SetListOptions(lokalise.TaskListOptions{Title: filterTitle, Limit: t.ListOpts().Limit})

resp, err := Api.Tasks().WithListOptions(lokalise.TaskListOptions{Title: filterTitle}).List(projectId)
resp, err := t.List(projectId)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ var translationListCmd = &cobra.Command{
Short: "List all translations",
Long: "Retrieves a list of project translation items, ungrouped. You may want to request Keys resource in order to get the structured key/translation pairs for all languages.",
RunE: func(*cobra.Command, []string) error {
t := Api.Translations()
translationListOpts.Limit = t.ListOpts().Limit

resp, err := Api.Translations().WithListOptions(translationListOpts).List(projectId)
resp, err := t.WithListOptions(translationListOpts).List(projectId)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ module github.com/lokalise/lokalise-cli-2-go
require (
github.com/lokalise/go-lokalise-api v0.0.0-20191011031534-989e1415dc95
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
)

replace github.com/lokalise/go-lokalise-api => ../go-lokalise-api
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/lokalise/lokalise-cli-2-go/cmd"
import (
"github.com/lokalise/lokalise-cli-2-go/cmd"
)

func main() {
cmd.Execute()
Expand Down

0 comments on commit 8c440f7

Please sign in to comment.