未验证 提交 16081d8b 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Add i18n support for command description (#245)

* Add more i18n files

* Test pass

* I18n support test pass

* Load language from system env

* Use RunE instead of run

* Fix the package import error

* Delete the changelog

* Add more i18n items

* Fix missing tools

* Add test cases for i18n function
上级 6782fc6c
...@@ -20,6 +20,12 @@ mock ...@@ -20,6 +20,12 @@ mock
# goland # goland
.idea .idea
*/**/.DS_Store
# test files # test files
app/cmd/test.yaml app/cmd/test.yaml
# language
app/i18n/bindata.go
app/i18n/jcli/zh_CN/LC_MESSAGES/jcli.mo
app/i18n/jcli.pot
...@@ -3,6 +3,7 @@ CGO_ENABLED = 0 ...@@ -3,6 +3,7 @@ CGO_ENABLED = 0
GO := go GO := go
BUILD_TARGET = build BUILD_TARGET = build
COMMIT := $(shell git rev-parse --short HEAD) COMMIT := $(shell git rev-parse --short HEAD)
# CHANGE_LOG := $(shell echo -n "$(shell hub release show $(shell hub release --include-drafts -L 1))" | base64)
VERSION := dev-$(shell git describe --tags $(shell git rev-list --tags --max-count=1)) VERSION := dev-$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
BUILDFLAGS = -ldflags "-X github.com/jenkins-zh/jenkins-cli/app.version=$(VERSION) -X github.com/jenkins-zh/jenkins-cli/app.commit=$(COMMIT)" BUILDFLAGS = -ldflags "-X github.com/jenkins-zh/jenkins-cli/app.version=$(VERSION) -X github.com/jenkins-zh/jenkins-cli/app.commit=$(COMMIT)"
COVERED_MAIN_SRC_FILE=./main COVERED_MAIN_SRC_FILE=./main
...@@ -14,15 +15,15 @@ gen-mock: ...@@ -14,15 +15,15 @@ gen-mock:
init: gen-mock init: gen-mock
darwin: ## Build for OSX darwin: gen-data
GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/darwin/$(NAME) $(MAIN_SRC_FILE) GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/darwin/$(NAME) $(MAIN_SRC_FILE)
chmod +x bin/darwin/$(NAME) chmod +x bin/darwin/$(NAME)
linux: ## Build for linux linux: gen-data
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/linux/$(NAME) $(MAIN_SRC_FILE) CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/linux/$(NAME) $(MAIN_SRC_FILE)
chmod +x bin/linux/$(NAME) chmod +x bin/linux/$(NAME)
win: ## Build for windows win: gen-data
go get github.com/inconshreveable/mousetrap go get github.com/inconshreveable/mousetrap
go get github.com/mattn/go-isatty go get github.com/mattn/go-isatty
CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=386 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/windows/$(NAME).exe $(MAIN_SRC_FILE) CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=386 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o bin/windows/$(NAME).exe $(MAIN_SRC_FILE)
...@@ -45,14 +46,28 @@ clean: ## Clean the generated artifacts ...@@ -45,14 +46,28 @@ clean: ## Clean the generated artifacts
copy: darwin copy: darwin
sudo cp bin/darwin/$(NAME) $(shell which jcli) sudo cp bin/darwin/$(NAME) $(shell which jcli)
tools: tools: i18n-tools
go get -u golang.org/x/lint/golint go get -u golang.org/x/lint/golint
i18n-tools:
go get github.com/gosexy/gettext/go-xgettext
go get github.com/go-bindata/go-bindata/...
verify: verify:
go vet ./... go vet ./...
golint -set_exit_status ./... golint -set_exit_status app/cmd/...
golint -set_exit_status app/helper/...
golint -set_exit_status app/i18n/i18n.go
golint -set_exit_status app/.
golint -set_exit_status client/...
golint -set_exit_status util/...
fmt:
go fmt ./util/...
go fmt ./client/...
go fmt ./app/...
test: verify fmt test: gen-data verify fmt
mkdir -p bin mkdir -p bin
go vet ./... go vet ./...
go test ./... -v -coverprofile coverage.out go test ./... -v -coverprofile coverage.out
...@@ -65,7 +80,9 @@ dep: ...@@ -65,7 +80,9 @@ dep:
go get gopkg.in/yaml.v2 go get gopkg.in/yaml.v2
go get github.com/Pallinder/go-randomdata go get github.com/Pallinder/go-randomdata
fmt: JCLI_FILES="app/cmd/*.go"
go fmt ./util/... gettext:
go fmt ./client/... go-xgettext -k=i18n.T "${JCLI_FILES}" > app/i18n/jcli.pot
go fmt ./app/...
gen-data:
cd app/i18n && go-bindata -o bindata.go -pkg i18n jcli/zh_CN/LC_MESSAGES/
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
...@@ -26,13 +28,14 @@ func init() { ...@@ -26,13 +28,14 @@ func init() {
var centerCmd = &cobra.Command{ var centerCmd = &cobra.Command{
Use: "center", Use: "center",
Short: "Manage your update center", Short: i18n.T("Manage your update center"),
Long: `Manage your update center`, Long: i18n.T("Manage your update center"),
Run: func(cmd *cobra.Command, _ []string) { RunE: func(cmd *cobra.Command, _ []string) (err error) {
jenkins := getCurrentJenkinsFromOptionsOrDie() jenkins := getCurrentJenkinsFromOptionsOrDie()
printJenkinsStatus(jenkins, cmd, centerOption.RoundTripper) printJenkinsStatus(jenkins, cmd, centerOption.RoundTripper)
printUpdateCenter(jenkins, cmd, centerOption.RoundTripper) _, err = printUpdateCenter(jenkins, cmd, centerOption.RoundTripper)
return
}, },
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -25,18 +26,22 @@ var centerDownloadOption CenterDownloadOption ...@@ -25,18 +26,22 @@ var centerDownloadOption CenterDownloadOption
func init() { func init() {
centerCmd.AddCommand(centerDownloadCmd) centerCmd.AddCommand(centerDownloadCmd)
centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.LTS, "lts", "", true, "If you want to download Jenkins as LTS") centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.LTS, "lts", "", true,
i18n.T("If you want to download Jenkins as LTS"))
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Version, "war-version", "", "", centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Version, "war-version", "", "",
"Version of the Jenkins which you want to download") i18n.T("Version of the Jenkins which you want to download"))
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Mirror, "mirror", "m", "default", "The mirror site of Jenkins") centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Mirror, "mirror", "m", "default",
centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.ShowProgress, "progress", "p", true, "If you want to show the download progress") i18n.T("The mirror site of Jenkins"))
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Output, "output", "o", "jenkins.war", "The file of output") centerDownloadCmd.Flags().BoolVarP(&centerDownloadOption.ShowProgress, "progress", "p", true,
i18n.T("If you want to show the download progress"))
centerDownloadCmd.Flags().StringVarP(&centerDownloadOption.Output, "output", "o", "jenkins.war",
i18n.T("The file of output"))
} }
var centerDownloadCmd = &cobra.Command{ var centerDownloadCmd = &cobra.Command{
Use: "download", Use: "download",
Short: "Download Jenkins", Short: i18n.T("Download Jenkins"),
Long: `Download Jenkins from a mirror site. You can get more mirror sites from https://jenkins-zh.cn/tutorial/management/mirror/`, Long: i18n.T(`Download Jenkins from a mirror site. You can get more mirror sites from https://jenkins-zh.cn/tutorial/management/mirror/`),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
mirrorSite := getMirror(centerDownloadOption.Mirror) mirrorSite := getMirror(centerDownloadOption.Mirror)
if mirrorSite == "" { if mirrorSite == "" {
......
...@@ -3,7 +3,7 @@ package cmd ...@@ -3,7 +3,7 @@ package cmd
import ( import (
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -22,9 +22,9 @@ func init() { ...@@ -22,9 +22,9 @@ func init() {
var centerUpgradeCmd = &cobra.Command{ var centerUpgradeCmd = &cobra.Command{
Use: "upgrade", Use: "upgrade",
Short: "Upgrade your Jenkins", Short: i18n.T("Upgrade your Jenkins"),
Long: `Upgrade your Jenkins`, Long: i18n.T("Upgrade your Jenkins"),
Run: func(cmd *cobra.Command, _ []string) { RunE: func(cmd *cobra.Command, _ []string) (err error) {
jenkins := getCurrentJenkinsFromOptionsOrDie() jenkins := getCurrentJenkinsFromOptionsOrDie()
jclient := &client.UpdateCenterManager{ jclient := &client.UpdateCenterManager{
...@@ -38,7 +38,7 @@ var centerUpgradeCmd = &cobra.Command{ ...@@ -38,7 +38,7 @@ var centerUpgradeCmd = &cobra.Command{
jclient.Proxy = jenkins.Proxy jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth jclient.ProxyAuth = jenkins.ProxyAuth
err := jclient.Upgrade() err = jclient.Upgrade()
helper.CheckErr(cmd, err) return
}, },
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -23,9 +24,9 @@ var centerWatchOption CenterWatchOption ...@@ -23,9 +24,9 @@ var centerWatchOption CenterWatchOption
func init() { func init() {
centerCmd.AddCommand(centerWatchCmd) centerCmd.AddCommand(centerWatchCmd)
centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilNeedRestart, "util-need-restart", "", false, centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilNeedRestart, "util-need-restart", "", false,
"The watch will be continue util Jenkins needs restart") i18n.T("The watch will be continue util Jenkins needs restart"))
centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilInstallComplete, "util-install-complete", "", false, centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilInstallComplete, "util-install-complete", "", false,
"The watch will be continue util all Jenkins plugins installation is completed") i18n.T("The watch will be continue util all Jenkins plugins installation is completed"))
centerWatchOption.SetFlag(centerWatchCmd) centerWatchOption.SetFlag(centerWatchCmd)
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
...@@ -96,7 +97,8 @@ type InteractiveOption struct { ...@@ -96,7 +97,8 @@ type InteractiveOption struct {
// SetFlag set the option flag to this cmd // SetFlag set the option flag to this cmd
func (b *InteractiveOption) SetFlag(cmd *cobra.Command) { func (b *InteractiveOption) SetFlag(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&b.Interactive, "interactive", "i", false, "Interactive mode") cmd.Flags().BoolVarP(&b.Interactive, "interactive", "i", false,
i18n.T("Interactive mode"))
} }
// HookOption is the option whether skip command hook // HookOption is the option whether skip command hook
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -10,8 +11,8 @@ func init() { ...@@ -10,8 +11,8 @@ func init() {
var completionCmd = &cobra.Command{ var completionCmd = &cobra.Command{
Use: "completion", Use: "completion",
Short: "Genereate bash completion scripts", Short: i18n.T("Genereate bash completion scripts"),
Long: `Genereate bash completion scripts`, Long: i18n.T("Genereate bash completion scripts"),
Example: `# Installing bash completion on macOS using homebrew Example: `# Installing bash completion on macOS using homebrew
## If running Bash 3.2 included with macOS ## If running Bash 3.2 included with macOS
brew install bash-completion brew install bash-completion
...@@ -20,7 +21,7 @@ var completionCmd = &cobra.Command{ ...@@ -20,7 +21,7 @@ var completionCmd = &cobra.Command{
## you may need add the completion to your completion directory ## you may need add the completion to your completion directory
jcli completion > $(brew --prefix)/etc/bash_completion.d/jcli jcli completion > $(brew --prefix)/etc/bash_completion.d/jcli
## If you get trouble, please visit https://github.com/jenkins-zh/jenkins-cli/issues/83.`, ## If you get trouble, please visit https://github.com/jenkins-zh/jenkins-cli/issues/83.`,
Run: func(cmd *cobra.Command, _ []string) { RunE: func(cmd *cobra.Command, _ []string) error {
rootCmd.GenBashCompletion(cmd.OutOrStdout()) return rootCmd.GenBashCompletion(cmd.OutOrStdout())
}, },
} }
...@@ -2,13 +2,17 @@ package cmd ...@@ -2,13 +2,17 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"io/ioutil"
"log"
"os"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.uber.org/zap" "go.uber.org/zap"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
) )
// ConfigOptions is the config cmd option // ConfigOptions is the config cmd option
...@@ -25,8 +29,8 @@ func init() { ...@@ -25,8 +29,8 @@ func init() {
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{
Use: "config", Use: "config",
Aliases: []string{"cfg"}, Aliases: []string{"cfg"},
Short: "Manage the config of jcli", Short: i18n.T("Manage the config of jcli"),
Long: `Manage the config of jcli`, Long: i18n.T("Manage the config of jcli"),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
current := getCurrentJenkins() current := getCurrentJenkins()
if current.Description != "" { if current.Description != "" {
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -16,13 +18,20 @@ var configAddOptions ConfigAddOptions ...@@ -16,13 +18,20 @@ var configAddOptions ConfigAddOptions
func init() { func init() {
configCmd.AddCommand(configAddCmd) configCmd.AddCommand(configAddCmd)
configAddCmd.Flags().StringVarP(&configAddOptions.Name, "name", "n", "", "Name of the Jenkins") configAddCmd.Flags().StringVarP(&configAddOptions.Name, "name", "n", "",
configAddCmd.Flags().StringVarP(&configAddOptions.URL, "url", "", "", "URL of the Jenkins") i18n.T("Name of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.UserName, "username", "u", "", "UserName of the Jenkins") configAddCmd.Flags().StringVarP(&configAddOptions.URL, "url", "", "",
configAddCmd.Flags().StringVarP(&configAddOptions.Token, "token", "t", "", "Token of the Jenkins") i18n.T("URL of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.Proxy, "proxy", "p", "", "Proxy of the Jenkins") configAddCmd.Flags().StringVarP(&configAddOptions.UserName, "username", "u", "",
configAddCmd.Flags().StringVarP(&configAddOptions.ProxyAuth, "proxyAuth", "a", "", "ProxyAuth of the Jenkins") i18n.T("UserName of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.Description, "description", "d", "", "Description of the Jenkins") configAddCmd.Flags().StringVarP(&configAddOptions.Token, "token", "t", "",
i18n.T("Token of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.Proxy, "proxy", "p", "",
i18n.T("Proxy of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.ProxyAuth, "proxyAuth", "a", "",
i18n.T("ProxyAuth of the Jenkins"))
configAddCmd.Flags().StringVarP(&configAddOptions.Description, "description", "d", "",
i18n.T("Description of the Jenkins"))
} }
var configAddCmd = &cobra.Command{ var configAddCmd = &cobra.Command{
......
...@@ -4,9 +4,11 @@ import ( ...@@ -4,9 +4,11 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"io/ioutil" "io/ioutil"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -16,8 +18,8 @@ func init() { ...@@ -16,8 +18,8 @@ func init() {
var configEditCmd = &cobra.Command{ var configEditCmd = &cobra.Command{
Use: "edit", Use: "edit",
Short: "Edit a Jenkins config", Short: i18n.T("Edit a Jenkins config"),
Long: `Edit a Jenkins config`, Long: i18n.T(`Edit a Jenkins config`),
Run: func(_ *cobra.Command, _ []string) { Run: func(_ *cobra.Command, _ []string) {
current := getCurrentJenkinsFromOptionsOrDie() current := getCurrentJenkinsFromOptionsOrDie()
configPath := configOptions.ConfigFileLocation configPath := configOptions.ConfigFileLocation
......
package cmd package cmd
import ( import (
"io/ioutil"
"os"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/atotto/clipboard" "github.com/atotto/clipboard"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil"
"os"
) )
// ConfigGenerateOption is the config generate cmd option // ConfigGenerateOption is the config generate cmd option
...@@ -20,15 +22,17 @@ var configGenerateOption ConfigGenerateOption ...@@ -20,15 +22,17 @@ var configGenerateOption ConfigGenerateOption
func init() { func init() {
configCmd.AddCommand(configGenerateCmd) configCmd.AddCommand(configGenerateCmd)
configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Interactive, "interactive", "i", true, "Interactive mode") configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Interactive, "interactive", "i", true,
configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Copy, "copy", "c", false, "Copy the output into clipboard") i18n.T("Interactive mode"))
configGenerateCmd.Flags().BoolVarP(&configGenerateOption.Copy, "copy", "c", false,
i18n.T("Copy the output into clipboard"))
} }
var configGenerateCmd = &cobra.Command{ var configGenerateCmd = &cobra.Command{
Use: "generate", Use: "generate",
Aliases: []string{"gen"}, Aliases: []string{"gen"},
Short: "Generate a sample config file for you", Short: i18n.T("Generate a sample config file for you"),
Long: `Generate a sample config file for you`, Long: i18n.T("Generate a sample config file for you"),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
data, err := generateSampleConfig() data, err := generateSampleConfig()
if err == nil { if err == nil {
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -24,8 +25,8 @@ func init() { ...@@ -24,8 +25,8 @@ func init() {
var configListCmd = &cobra.Command{ var configListCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List all Jenkins config items", Short: i18n.T("List all Jenkins config items"),
Long: `List all Jenkins config items`, Long: i18n.T("List all Jenkins config items"),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
current := getCurrentJenkins() current := getCurrentJenkins()
......
...@@ -2,8 +2,8 @@ package cmd ...@@ -2,8 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -12,18 +12,13 @@ func init() { ...@@ -12,18 +12,13 @@ func init() {
} }
var configRemoveCmd = &cobra.Command{ var configRemoveCmd = &cobra.Command{
Use: "remove", Use: "remove <name>",
Short: "Remove a Jenkins config", Short: i18n.T("Remove a Jenkins config"),
Long: `Remove a Jenkins config`, Long: i18n.T("Remove a Jenkins config"),
Run: func(_ *cobra.Command, args []string) { Args: cobra.MinimumNArgs(1),
if len(args) == 0 { RunE: func(_ *cobra.Command, args []string) error {
log.Fatal("You need to give a name")
}
target := args[0] target := args[0]
if err := removeJenkins(target); err != nil { return removeJenkins(target)
log.Fatal(err)
}
}, },
} }
......
...@@ -2,12 +2,16 @@ package cmd ...@@ -2,12 +2,16 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
) )
func init() { func init() {
...@@ -20,22 +24,25 @@ date: %s ...@@ -20,22 +24,25 @@ date: %s
title: "%s" title: "%s"
anchor: %s anchor: %s
url: %s url: %s
version: %s
--- ---
` `
) )
var docCmd = &cobra.Command{ var docCmd = &cobra.Command{
Use: "doc <output dir>", Use: "doc <output dir>",
Short: "Generate document for all jcl commands", Short: i18n.T("Generate document for all jcl commands"),
Long: `Generate document for all jcl commands`, Long: i18n.T("Generate document for all jcl commands"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) (err error) {
now := time.Now().Format(time.RFC3339) now := time.Now().Format(time.RFC3339)
prepender := func(filename string) string { prepender := func(filename string) string {
name := filepath.Base(filename) name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name)) base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/" url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url) return fmt.Sprintf(gendocFrontmatterTemplate, now,
strings.Replace(base, "_", " ", -1),
base, url, app.GetVersion())
} }
linkHandler := func(name string) string { linkHandler := func(name string) string {
...@@ -45,9 +52,8 @@ var docCmd = &cobra.Command{ ...@@ -45,9 +52,8 @@ var docCmd = &cobra.Command{
outputDir := args[0] outputDir := args[0]
err := doc.GenMarkdownTreeCustom(rootCmd, outputDir, prepender, linkHandler) rootCmd.DisableAutoGenTag = true
if err != nil { err = doc.GenMarkdownTreeCustom(rootCmd, outputDir, prepender, linkHandler)
cmd.PrintErr(err) return
}
}, },
} }
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"strconv" "strconv"
...@@ -29,8 +30,8 @@ func init() { ...@@ -29,8 +30,8 @@ func init() {
var jobArtifactCmd = &cobra.Command{ var jobArtifactCmd = &cobra.Command{
Use: "artifact <jobName> [buildID]", Use: "artifact <jobName> [buildID]",
Short: "Print the artifact list of target job", Short: i18n.T("Print the artifact list of target job"),
Long: `Print the artifact list of target job`, Long: i18n.T("Print the artifact list of target job"),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
argLen := len(args) argLen := len(args)
if argLen == 0 { if argLen == 0 {
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -17,8 +18,8 @@ func init() { ...@@ -17,8 +18,8 @@ func init() {
var pluginCmd = &cobra.Command{ var pluginCmd = &cobra.Command{
Use: "plugin", Use: "plugin",
Short: "Manage the plugins of Jenkins", Short: i18n.T("Manage the plugins of Jenkins"),
Long: `Manage the plugins of Jenkins`, Long: i18n.T("Manage the plugins of Jenkins"),
Example: ` jcli plugin list Example: ` jcli plugin list
jcli plugin search github jcli plugin search github
jcli plugin check`, jcli plugin check`,
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http" "net/http"
...@@ -21,19 +22,19 @@ var pluginDownloadOption PluginDownloadOption ...@@ -21,19 +22,19 @@ var pluginDownloadOption PluginDownloadOption
func init() { func init() {
pluginCmd.AddCommand(pluginDownloadCmd) pluginCmd.AddCommand(pluginDownloadCmd)
pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.SkipDependency, "skip-dependency", "", false, pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.SkipDependency, "skip-dependency", "", false,
"If you want to skip download dependency of plugin") i18n.T("If you want to skip download dependency of plugin"))
pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.SkipOptional, "skip-optional", "", true, pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.SkipOptional, "skip-optional", "", true,
"If you want to skip download optional dependency of plugin") i18n.T("If you want to skip download optional dependency of plugin"))
pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.UseMirror, "use-mirror", "", true, pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.UseMirror, "use-mirror", "", true,
"If you want to download plugin from a mirror site") i18n.T("If you want to download plugin from a mirror site"))
pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.ShowProgress, "show-progress", "", true, pluginDownloadCmd.Flags().BoolVarP(&pluginDownloadOption.ShowProgress, "show-progress", "", true,
"If you want to show the progress of download a plugin") i18n.T("If you want to show the progress of download a plugin"))
} }
var pluginDownloadCmd = &cobra.Command{ var pluginDownloadCmd = &cobra.Command{
Use: "download <keyword>", Use: "download <keyword>",
Short: "Download the plugins", Short: i18n.T("Download the plugins"),
Long: `Download the plugins which contain the target plugin and its dependencies`, Long: i18n.T(`Download the plugins which contain the target plugin and its dependencies`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
jClient := &client.PluginAPI{ jClient := &client.PluginAPI{
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
...@@ -23,15 +24,15 @@ var pluginInstallOption PluginInstallOption ...@@ -23,15 +24,15 @@ var pluginInstallOption PluginInstallOption
func init() { func init() {
pluginCmd.AddCommand(pluginInstallCmd) pluginCmd.AddCommand(pluginInstallCmd)
pluginInstallCmd.Flags().BoolVarP(&pluginInstallOption.UseMirror, "use-mirror", "", true, pluginInstallCmd.Flags().BoolVarP(&pluginInstallOption.UseMirror, "use-mirror", "", true,
"If you want to download plugin from a mirror site") i18n.T("If you want to download plugin from a mirror site"))
pluginInstallCmd.Flags().BoolVarP(&pluginInstallOption.ShowProgress, "show-progress", "", true, pluginInstallCmd.Flags().BoolVarP(&pluginInstallOption.ShowProgress, "show-progress", "", true,
"If you want to show the progress of download a plugin") i18n.T("If you want to show the progress of download a plugin"))
pluginInstallCmd.Flags().StringVarP(&pluginOpt.Suite, "suite", "", "", "Suite of plugins") pluginInstallCmd.Flags().StringVarP(&pluginOpt.Suite, "suite", "", "", "Suite of plugins")
} }
var pluginInstallCmd = &cobra.Command{ var pluginInstallCmd = &cobra.Command{
Use: "install [pluginName]", Use: "install [pluginName]",
Short: "Install the plugins", Short: i18n.T("Install the plugins"),
Long: `Install the plugins Long: `Install the plugins
Allow you to install a plugin with or without the version`, Allow you to install a plugin with or without the version`,
Example: `jcli plugin install localization-zh-cn Example: `jcli plugin install localization-zh-cn
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"strings" "strings"
...@@ -31,8 +32,8 @@ func init() { ...@@ -31,8 +32,8 @@ func init() {
var pluginListCmd = &cobra.Command{ var pluginListCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "Print all the plugins which are installed", Short: i18n.T("Print all the plugins which are installed"),
Long: `Print all the plugins which are installed`, Long: i18n.T("Print all the plugins which are installed"),
Example: ` jcli plugin list --filter name=github Example: ` jcli plugin list --filter name=github
jcli plugin list --filter hasUpdate jcli plugin list --filter hasUpdate
jcli plugin list --no-headers`, jcli plugin list --no-headers`,
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"strings" "strings"
...@@ -29,8 +30,8 @@ func init() { ...@@ -29,8 +30,8 @@ func init() {
var pluginSearchCmd = &cobra.Command{ var pluginSearchCmd = &cobra.Command{
Use: "search <keyword>", Use: "search <keyword>",
Short: "Print the plugins of your Jenkins", Short: i18n.T("Print the plugins of your Jenkins"),
Long: `Print the plugins of your Jenkins`, Long: i18n.T("Print the plugins of your Jenkins"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
keyword := args[0] keyword := args[0]
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
...@@ -22,8 +23,8 @@ func init() { ...@@ -22,8 +23,8 @@ func init() {
var pluginUninstallCmd = &cobra.Command{ var pluginUninstallCmd = &cobra.Command{
Use: "uninstall [pluginName]", Use: "uninstall [pluginName]",
Short: "Uninstall the plugins", Short: i18n.T("Uninstall the plugins"),
Long: `Uninstall the plugins`, Long: i18n.T("Uninstall the plugins"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
pluginName := args[0] pluginName := args[0]
......
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"strings" "strings"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
...@@ -28,8 +30,8 @@ func init() { ...@@ -28,8 +30,8 @@ func init() {
var pluginUpgradeCmd = &cobra.Command{ var pluginUpgradeCmd = &cobra.Command{
Use: "upgrade [plugin name]", Use: "upgrade [plugin name]",
Short: "Upgrade the specific plugin", Short: i18n.T("Upgrade the specific plugin"),
Long: `Upgrade the specific plugin`, Long: i18n.T("Upgrade the specific plugin"),
Example: `jcli plugin upgrade [tab][tab]`, Example: `jcli plugin upgrade [tab][tab]`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
jclient := &client.PluginManager{ jclient := &client.PluginManager{
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
...@@ -34,21 +35,28 @@ var pluginUploadOption PluginUploadOption ...@@ -34,21 +35,28 @@ var pluginUploadOption PluginUploadOption
func init() { func init() {
pluginCmd.AddCommand(pluginUploadCmd) pluginCmd.AddCommand(pluginUploadCmd)
pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.ShowProgress, "show-progress", "", true, "Whether show the upload progress") pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.ShowProgress, "show-progress", "", true,
pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.Remote, "remote", "r", "", "Remote plugin URL") i18n.T("Whether show the upload progress"))
pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteUser, "remote-user", "", "", "User of remote plugin URL") pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.Remote, "remote", "r", "",
pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemotePassword, "remote-password", "", "", "Password of remote plugin URL") i18n.T("Remote plugin URL"))
pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteJenkins, "remote-jenkins", "", "", "Remote Jenkins which will find from config list") pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteUser, "remote-user", "", "",
i18n.T("User of remote plugin URL"))
pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPreHook, "skip-prehook", "", false, "Whether skip the previous command hook") pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemotePassword, "remote-password", "", "",
pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPostHook, "skip-posthook", "", false, "Whether skip the post command hook") i18n.T("Password of remote plugin URL"))
pluginUploadCmd.Flags().StringVarP(&pluginUploadOption.RemoteJenkins, "remote-jenkins", "", "",
i18n.T("Remote Jenkins which will find from config list"))
pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPreHook, "skip-prehook", "", false,
i18n.T("Whether skip the previous command hook"))
pluginUploadCmd.Flags().BoolVarP(&pluginUploadOption.SkipPostHook, "skip-posthook", "", false,
i18n.T("Whether skip the post command hook"))
} }
var pluginUploadCmd = &cobra.Command{ var pluginUploadCmd = &cobra.Command{
Use: "upload", Use: "upload",
Aliases: []string{"up"}, Aliases: []string{"up"},
Short: "Upload a plugin to your Jenkins", Short: i18n.T("Upload a plugin to your Jenkins"),
Long: `Upload a plugin from local filesystem or remote URL to your Jenkins`, Long: i18n.T(`Upload a plugin from local filesystem or remote URL to your Jenkins`),
Example: ` jcli plugin upload --remote https://server/sample.hpi Example: ` jcli plugin upload --remote https://server/sample.hpi
jcli plugin upload sample.hpi jcli plugin upload sample.hpi
jcli plugin upload sample.hpi --show-progress=false`, jcli plugin upload sample.hpi --show-progress=false`,
......
...@@ -12,7 +12,4 @@ var queueCmd = &cobra.Command{ ...@@ -12,7 +12,4 @@ var queueCmd = &cobra.Command{
Use: "queue", Use: "queue",
Short: "Manage the queue of your Jenkins", Short: "Manage the queue of your Jenkins",
Long: `Manage the queue of your Jenkins`, Long: `Manage the queue of your Jenkins`,
Run: func(cmd *cobra.Command, _ []string) {
cmd.Help()
},
} }
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -25,8 +26,8 @@ func init() { ...@@ -25,8 +26,8 @@ func init() {
var restartCmd = &cobra.Command{ var restartCmd = &cobra.Command{
Use: "restart", Use: "restart",
Short: "Restart your Jenkins", Short: i18n.T("Restart your Jenkins"),
Long: `Restart your Jenkins`, Long: i18n.T("Restart your Jenkins"),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
jenkins := getCurrentJenkinsFromOptionsOrDie() jenkins := getCurrentJenkinsFromOptionsOrDie()
if !restartOption.Batch { if !restartOption.Batch {
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
...@@ -31,21 +32,19 @@ type RootOptions struct { ...@@ -31,21 +32,19 @@ type RootOptions struct {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "jcli", Use: "jcli",
Short: "jcli is a tool which could help you with your multiple Jenkins", Short: i18n.T("jcli is a tool which could help you with your multiple Jenkins"),
Long: `jcli is Jenkins CLI which could help with your multiple Jenkins, Long: `jcli is Jenkins CLI which could help with your multiple Jenkins,
Manage your Jenkins and your pipelines Manage your Jenkins and your pipelines
More information could found at https://jenkins-zh.cn`, More information could found at https://jenkins-zh.cn`,
BashCompletionFunction: jcliBashCompletionFunc, PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
PersistentPreRun: func(cmd *cobra.Command, args []string) { if logger, err = util.InitLogger(rootOptions.LoggerLevel); err == nil {
var err error
if logger, err = util.InitLogger(rootOptions.LoggerLevel); err != nil {
cmd.PrintErrln(err)
} else {
client.SetLogger(logger) client.SetLogger(logger)
} }
return
}, },
BashCompletionFunction: jcliBashCompletionFunc,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Jenkins CLI (jcli) manage your Jenkins") cmd.Println(i18n.T("Jenkins CLI (jcli) manage your Jenkins"))
if rootOptions.Version { if rootOptions.Version {
cmd.Printf("Version: %s\n", app.GetVersion()) cmd.Printf("Version: %s\n", app.GetVersion())
cmd.Printf("Commit: %s\n", app.GetCommit()) cmd.Printf("Commit: %s\n", app.GetCommit())
...@@ -58,7 +57,6 @@ var rootCmd = &cobra.Command{ ...@@ -58,7 +57,6 @@ var rootCmd = &cobra.Command{
cmd.Println("Cannot found the configuration") cmd.Println("Cannot found the configuration")
} }
} }
}, },
} }
...@@ -73,12 +71,15 @@ var rootOptions RootOptions ...@@ -73,12 +71,15 @@ var rootOptions RootOptions
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&rootOptions.ConfigFile, "configFile", "", "", "An alternative config file") rootCmd.PersistentFlags().StringVarP(&rootOptions.ConfigFile, "configFile", "", "",
rootCmd.PersistentFlags().StringVarP(&rootOptions.Jenkins, "jenkins", "j", "", "Select a Jenkins server for this time") i18n.T("An alternative config file"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.Version, "version", "v", false, "Print the version of Jenkins CLI") rootCmd.PersistentFlags().StringVarP(&rootOptions.Jenkins, "jenkins", "j", "",
i18n.T("Select a Jenkins server for this time"))
rootCmd.PersistentFlags().BoolVarP(&rootOptions.Debug, "debug", "", false, "Print the output into debug.html") rootCmd.PersistentFlags().BoolVarP(&rootOptions.Debug, "debug", "", false, "Print the output into debug.html")
rootCmd.PersistentFlags().StringVarP(&rootOptions.LoggerLevel, "logger-level", "", "warn", rootCmd.PersistentFlags().StringVarP(&rootOptions.LoggerLevel, "logger-level", "", "warn",
"Logger level which could be: debug, info, warn, error") "Logger level which could be: debug, info, warn, error")
rootCmd.Flags().BoolVarP(&rootOptions.Version, "version", "v", false,
i18n.T("Print the version of Jenkins CLI"))
rootCmd.SetOut(os.Stdout) rootCmd.SetOut(os.Stdout)
} }
......
package i18n
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/chai2010/gettext-go/gettext"
)
var knownTranslations = map[string][]string{
"jcli": {
"default",
"en_US",
"zh_CN",
},
// only used for unit tests.
"test": {
"default",
"en_US",
},
}
func loadSystemLanguage() string {
// Implements the following locale priority order: LC_ALL, LC_MESSAGES, LANG
// Similarly to: https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
langStr := os.Getenv("LC_ALL")
if langStr == "" {
langStr = os.Getenv("LC_MESSAGES")
}
if langStr == "" {
langStr = os.Getenv("LANG")
}
if langStr == "" {
return "default"
}
pieces := strings.Split(langStr, ".")
if len(pieces) != 2 {
return "default"
}
return pieces[0]
}
func findLanguage(root string, getLanguageFn func() string) string {
langStr := getLanguageFn()
translations := knownTranslations[root]
for ix := range translations {
if translations[ix] == langStr {
return langStr
}
}
return "default"
}
// LoadTranslations loads translation files. getLanguageFn should return a language
// string (e.g. 'en-US'). If getLanguageFn is nil, then the loadSystemLanguage function
// is used, which uses the 'LANG' environment variable.
func LoadTranslations(root string, getLanguageFn func() string) (err error) {
if getLanguageFn == nil {
getLanguageFn = loadSystemLanguage
}
langStr := findLanguage(root, getLanguageFn)
translationFiles := []string{
//"jcli/zh_CN/LC_MESSAGES/jcli.mo",
"jcli/zh_CN/LC_MESSAGES/jcli.po",
}
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
// Make sure to check the error on Close.
var f io.Writer
var data []byte
for _, file := range translationFiles {
filename := file
if f, err = w.Create(file); err != nil {
return
}
if data, err = Asset(filename); err != nil {
return
}
if _, err = f.Write(data); err != nil {
return
}
}
if err = w.Close(); err == nil {
gettext.BindTextdomain("jcli", root+".zip", buf.Bytes())
gettext.Textdomain("jcli")
gettext.SetLocale(langStr)
}
return
}
var i18nLoaded = false
// T translates a string, possibly substituting arguments into it along
// the way. If len(args) is > 0, args1 is assumed to be the plural value
// and plural translation is used.
func T(defaultValue string, args ...int) string {
if !i18nLoaded {
i18nLoaded = true
if err := LoadTranslations("jcli", nil); err != nil {
fmt.Println(err)
}
}
if len(args) == 0 {
return gettext.PGettext("", defaultValue)
}
return fmt.Sprintf(gettext.PNGettext("", defaultValue, defaultValue+".plural", args[0]),
args[0])
}
package i18n
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
)
var _ = Describe("test LoadTranslations", func() {
var (
root string
getLangFunc func() string
err error
)
JustBeforeEach(func() {
err = LoadTranslations(root, getLangFunc)
})
AfterEach(func() {
root = ""
getLangFunc = nil
})
It("default param", func() {
Expect(err).NotTo(HaveOccurred())
})
Context("unknown language", func() {
BeforeEach(func() {
getLangFunc = func() string {
return "fake"
}
})
It("should not have error", func() {
Expect(err).NotTo(HaveOccurred())
})
})
Context("given invalid environment of language", func() {
var (
osEnvErr error
)
BeforeEach(func() {
osEnvErr = os.Setenv("LC_ALL", "zh_CN")
})
It("should not have error", func() {
Expect(osEnvErr).NotTo(HaveOccurred())
Expect(err).NotTo(HaveOccurred())
})
})
Context("given valid environment of language", func() {
var (
osEnvErr error
)
BeforeEach(func() {
root = "jcli"
osEnvErr = os.Setenv("LC_ALL", "zh_CN.utf-8")
})
It("should not have error", func() {
Expect(osEnvErr).NotTo(HaveOccurred())
Expect(err).NotTo(HaveOccurred())
})
})
})
var _ = Describe("test i18n function T", func() {
var (
text string
args []int
result string
)
JustBeforeEach(func() {
result = T(text, args...)
})
It("simple case, without args", func() {
Expect(result).To(Equal(text))
})
Context("with args", func() {
BeforeEach(func() {
text = "fake %d"
args = []int{1}
})
It("should success", func() {
Expect(result).To(Equal(fmt.Sprintf(text, 1)))
})
})
})
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: EMAIL\n"
"POT-Creation-Date: 2019-11-21 13:00+0800\n"
"PO-Revision-Date: 2019-11-21 22:17+0800\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2.4\n"
"Last-Translator: rick <rick@jenkins-zh.cn>\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"Language: zh_CN\n"
#: app/cmd/restart.go:29 app/cmd/restart.go:30
msgid "Restart your Jenkins"
msgstr ""
#: app/cmd/root.go:79
msgid "Select a Jenkins server for this time"
msgstr "选择本次执行的 Jenkins"
#: app/cmd/config_add.go:30
msgid "Proxy of the Jenkins"
msgstr ""
#: app/cmd/plugin_upgrade.go:32 app/cmd/plugin_upgrade.go:33
msgid "Upgrade the specific plugin"
msgstr "升级指定插件"
#: app/cmd/config.go:31 app/cmd/config.go:32
msgid "Manage the config of jcli"
msgstr "管理 jcli 的配置"
#: app/cmd/config_generate.go:28
msgid "Copy the output into clipboard"
msgstr "拷贝输出到剪贴板"
#: app/cmd/root.go:77
msgid "An alternative config file"
msgstr "指定另外一个配置文件"
#: app/cmd/config_list.go:19 app/cmd/config_list.go:20
msgid "List all Jenkins config items"
msgstr "列出所有的 Jenkins 配置项"
#: app/cmd/doc.go:30 app/cmd/doc.go:31
msgid "Generate document for all jcl commands"
msgstr "为所有的 jcli 命令生成文档"
#: app/cmd/root.go:37
msgid "jcli is a tool which could help you with your multiple Jenkins"
msgstr "jcli 是帮助你管理多个 Jenkins 的工具"
#: app/cmd/root.go:55
msgid "Jenkins CLI (jcli) manage your Jenkins"
msgstr "Jenkins CLI (jcli) 管理你的 Jenkins"
#: app/cmd/plugin_upload.go:50
msgid "Whether skip the previous command hook"
msgstr "是否跳过前置命令钩子"
#: app/cmd/plugin.go:21 app/cmd/plugin.go:22
msgid "Manage the plugins of Jenkins"
msgstr "管理 Jenkins 的插件"
#: app/cmd/root.go:81
msgid "Print the version of Jenkins CLI"
msgstr "打印 Jenkins CLI 的版本"
#: app/cmd/completion.go:14 app/cmd/completion.go:15
msgid "Genereate bash completion scripts"
msgstr "生成 bash 自动补全的脚本"
#: app/cmd/plugin_download.go:31 app/cmd/plugin_install.go:29
msgid "If you want to show the progress of download a plugin"
msgstr "你是否希望显示插件下载的进度"
#: app/cmd/plugin_download.go:37
msgid "Download the plugins which contain the target plugin and its dependencies"
msgstr ""
#: app/cmd/plugin_uninstall.go:26 app/cmd/plugin_uninstall.go:27
msgid "Uninstall the plugins"
msgstr "卸载插件"
#: app/cmd/plugin_upload.go:41
msgid "Remote plugin URL"
msgstr "远程插件 URL"
#: app/cmd/plugin_upload.go:52
msgid "Whether skip the post command hook"
msgstr "是否跳过后置命令钩子"
#: app/cmd/center_download.go:32
msgid "Version of the Jenkins which you want to download"
msgstr ""
#: app/cmd/config_add.go:22
msgid "Name of the Jenkins"
msgstr ""
#: app/cmd/config_list.go:28 app/cmd/config_list.go:29
msgid "List all Jenkins config items"
msgstr ""
#: app/cmd/config_generate.go:34 app/cmd/config_generate.go:35
msgid "Generate a sample config file for you"
msgstr ""
#: app/cmd/config_add.go:28
msgid "Token of the Jenkins"
msgstr ""
#: app/cmd/config_add.go:32
msgid "ProxyAuth of the Jenkins"
msgstr ""
#: app/cmd/root.go:36
msgid "jcli is a tool which could help you with your multiple Jenkins"
msgstr ""
#: app/cmd/center_watch.go:27
msgid "The watch will be continue util Jenkins needs restart"
msgstr ""
#: app/cmd/config_add.go:26
msgid "UserName of the Jenkins"
msgstr ""
#: app/cmd/config_edit.go:21 app/cmd/config_edit.go:22
msgid "Edit a Jenkins config"
msgstr ""
#: app/cmd/plugin_download.go:25
msgid "If you want to skip download dependency of plugin"
msgstr "你是否想要跳过下载依赖插件"
#: app/cmd/plugin_upload.go:45
msgid "Password of remote plugin URL"
msgstr ""
#: app/cmd/plugin_upload.go:58
msgid "Upload a plugin to your Jenkins"
msgstr "上传插件到你的 Jenkins"
#: app/cmd/center.go:30
msgid "Manage your update center"
msgstr "管理你的 Jenkins 更新中心"
#: app/cmd/job_artifact.go:33 app/cmd/job_artifact.go:34
msgid "Print the artifact list of target job"
msgstr ""
#: app/cmd/plugin_download.go:27
msgid "If you want to skip download optional dependency of plugin"
msgstr ""
#: app/cmd/plugin_download.go:36
msgid "Download the plugins"
msgstr "下载插件"
#: app/cmd/plugin_install.go:35
msgid "Install the plugins"
msgstr "安装插件"
#: app/cmd/plugin_search.go:33 app/cmd/plugin_search.go:34
msgid "Print the plugins of your Jenkins"
msgstr ""
#: app/cmd/plugin_upload.go:39
msgid "Whether show the upload progress"
msgstr "是否显示上传进度"
#: app/cmd/plugin_upload.go:47
msgid "Remote Jenkins which will find from config list"
msgstr ""
#: app/cmd/plugin_download.go:29 app/cmd/plugin_install.go:27
msgid "If you want to download plugin from a mirror site"
msgstr "你是否想要从镜像站点下载插件"
#: app/cmd/plugin_list.go:35 app/cmd/plugin_list.go:36
msgid "Print all the plugins which are installed"
msgstr ""
#: app/cmd/center_watch.go:29
msgid "The watch will be continue util all Jenkins plugins installation is completed"
msgstr ""
#: app/cmd/common.go:101 app/cmd/config_generate.go:26
msgid "Interactive mode"
msgstr ""
#: app/cmd/plugin_download.go:29 app/cmd/plugin_install.go:27
msgid "If you want to download plugin from a mirror site"
msgstr ""
#: app/cmd/plugin_upload.go:43
msgid "User of remote plugin URL"
msgstr ""
#: app/cmd/plugin_upload.go:47
msgid "Remote Jenkins which will find from config list"
msgstr ""
#: app/cmd/plugin_upload.go:58
msgid "Upload a plugin to your Jenkins"
msgstr ""
#: app/cmd/root.go:76
msgid "An alternative config file"
msgstr ""
#: app/cmd/center.go:31 app/cmd/center.go:32
msgid "Manage your update center"
msgstr ""
#: app/cmd/center_download.go:36
msgid "If you want to show the download progress"
msgstr ""
#: app/cmd/completion.go:14 app/cmd/completion.go:15
msgid "Genereate bash completion scripts"
msgstr ""
#: app/cmd/config.go:32 app/cmd/config.go:33
msgid "Manage the config of jcli"
msgstr "管理 jcli 的配置信息"
#: app/cmd/plugin_upload.go:59
msgid "Upload a plugin from local filesystem or remote URL to your Jenkins"
msgstr "上传来自本地文件系统或者远程 URL 的插件到你的 Jenkins"
package i18n_test
import (
"testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestI18n(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("test-i18n.xml")
RunSpecsWithDefaultAndCustomReporters(t, "app", []Reporter{junitReporter})
}
package app package app
import "fmt" import (
"fmt"
)
var ( var (
version string version string
......
...@@ -7,10 +7,14 @@ require ( ...@@ -7,10 +7,14 @@ require (
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8
github.com/Pallinder/go-randomdata v1.2.0 github.com/Pallinder/go-randomdata v1.2.0
github.com/atotto/clipboard v0.1.2 github.com/atotto/clipboard v0.1.2
github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/mock v1.3.1 github.com/golang/mock v1.3.1
github.com/gosexy/gettext v0.0.0-20160830220431-74466a0a0c4a // indirect
github.com/gosuri/uilive v0.0.3 // indirect github.com/gosuri/uilive v0.0.3 // indirect
github.com/gosuri/uiprogress v0.0.1 github.com/gosuri/uiprogress v0.0.1
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.10.3 github.com/onsi/ginkgo v1.10.3
......
此差异已折叠。
## Tools requirement
[poedit](https://poedit.net/)
`make i18n-tools`
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册