diff --git a/.gitignore b/.gitignore index 82f23b9..d7c0b43 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,8 @@ dist/ .idea/ .DS_Store +# config file +config.* + # temporary disabled go.sum \ No newline at end of file diff --git a/cmd/cfg.go b/cmd/cfg.go new file mode 100644 index 0000000..72da6f1 --- /dev/null +++ b/cmd/cfg.go @@ -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) + } + } +} diff --git a/cmd/file.go b/cmd/file.go index beefbb8..e380e98 100644 --- a/cmd/file.go +++ b/cmd/file.go @@ -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)") diff --git a/cmd/key.go b/cmd/key.go index 5b891ac..9b6df67 100644 --- a/cmd/key.go +++ b/cmd/key.go @@ -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 } diff --git a/cmd/project.go b/cmd/project.go index 4297d55..3c13346 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -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 ( @@ -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, @@ -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") } diff --git a/cmd/root.go b/cmd/root.go index 792ec32..39f907a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 ( @@ -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 }, } @@ -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")) } // ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ diff --git a/cmd/screenshot.go b/cmd/screenshot.go index 61c4042..14b6c83 100644 --- a/cmd/screenshot.go +++ b/cmd/screenshot.go @@ -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).") diff --git a/cmd/task.go b/cmd/task.go index 2fdabc4..1a952d6 100644 --- a/cmd/task.go +++ b/cmd/task.go @@ -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 } diff --git a/cmd/translation.go b/cmd/translation.go index 9823969..83aba83 100644 --- a/cmd/translation.go +++ b/cmd/translation.go @@ -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 } diff --git a/go.mod b/go.mod index 38a6bbd..16911e5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/main.go b/main.go index ca042bb..0014726 100644 --- a/main.go +++ b/main.go @@ -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()