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

Add zsh and powerShell completion support (#296)

上级 4e3caf9c
package cmd package cmd
import ( import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// CompletionOptions is the option of completion command
type CompletionOptions struct {
Type string
}
// ShellTypes contains all types of shell
var ShellTypes = []string{
"zsh", "bash", "powerShell",
}
var completionOptions CompletionOptions
func init() { func init() {
rootCmd.AddCommand(completionCmd) rootCmd.AddCommand(completionCmd)
completionCmd.Flags().StringVarP(&completionOptions.Type, "type", "", "bash",
i18n.T(fmt.Sprintf("Generate different types of shell which are %v", ShellTypes)))
} }
var completionCmd = &cobra.Command{ var completionCmd = &cobra.Command{
Use: "completion", Use: "completion",
Short: i18n.T("Genereate bash completion scripts"), Short: i18n.T("Generate bash completion scripts"),
Long: i18n.T("Genereate bash completion scripts"), Long: i18n.T("Generate 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,8 +35,24 @@ var completionCmd = &cobra.Command{ ...@@ -20,8 +35,24 @@ var completionCmd = &cobra.Command{
brew install bash-completion@2 brew install bash-completion@2
## 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.
RunE: func(cmd *cobra.Command, _ []string) error {
return rootCmd.GenBashCompletion(cmd.OutOrStdout()) # Load the jcli completion code for zsh[1] into the current shell
source <(jcli completion --type zsh)
# Set the jcli completion code for zsh[1] to autoload on startup
jcli completion --type zsh > "${fpath[1]}/_jcli"`,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
shellType := completionOptions.Type
switch shellType {
case "zsh":
err = rootCmd.GenZshCompletion(cmd.OutOrStdout())
case "powerShell":
err = rootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
case "bash":
err = rootCmd.GenBashCompletion(cmd.OutOrStdout())
default:
err = fmt.Errorf("unknown shell type %s", shellType)
}
return
}, },
} }
...@@ -10,9 +10,12 @@ import ( ...@@ -10,9 +10,12 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
var _ = Describe("job type command", func() { var _ = Describe("completion command", func() {
var ( var (
ctrl *gomock.Controller ctrl *gomock.Controller
cmdArgs []string
buf *bytes.Buffer
err error
) )
BeforeEach(func() { BeforeEach(func() {
...@@ -20,6 +23,20 @@ var _ = Describe("job type command", func() { ...@@ -20,6 +23,20 @@ var _ = Describe("job type command", func() {
rootCmd.SetArgs([]string{}) rootCmd.SetArgs([]string{})
rootOptions.Jenkins = "" rootOptions.Jenkins = ""
rootOptions.ConfigFile = "test.yaml" rootOptions.ConfigFile = "test.yaml"
var data []byte
data, err = generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
Expect(err).To(BeNil())
})
JustBeforeEach(func() {
rootCmd.SetArgs(cmdArgs)
buf = new(bytes.Buffer)
rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC()
}) })
AfterEach(func() { AfterEach(func() {
...@@ -29,21 +46,47 @@ var _ = Describe("job type command", func() { ...@@ -29,21 +46,47 @@ var _ = Describe("job type command", func() {
ctrl.Finish() ctrl.Finish()
}) })
Context("basic cases", func() { Context("with default option value", func() {
It("should success, empty list", func() { BeforeEach(func() {
data, err := generateSampleConfig() cmdArgs = []string{"completion"}
})
It("should success", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) Expect(buf.String()).To(ContainSubstring("bash completion for jcli"))
})
})
Context("generate zsh completion", func() {
BeforeEach(func() {
cmdArgs = []string{"completion", "--type", "zsh"}
})
It("should success", func() {
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(buf.String()).NotTo(Equal(""))
})
})
rootCmd.SetArgs([]string{"completion"}) Context("generate powerShell completion", func() {
BeforeEach(func() {
cmdArgs = []string{"completion", "--type", "powerShell"}
})
buf := new(bytes.Buffer) It("should success", func() {
rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(buf.String()).To(ContainSubstring("using namespace System.Management.Automation"))
})
})
Expect(buf.String()).NotTo(Equal("")) Context("generate unknown shell type completion", func() {
BeforeEach(func() {
cmdArgs = []string{"completion", "--type", "fake"}
})
It("error occurred", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unknown shell type"))
}) })
}) })
}) })
...@@ -11,7 +11,7 @@ func InitLogger(level string) (logger *zap.Logger, err error) { ...@@ -11,7 +11,7 @@ func InitLogger(level string) (logger *zap.Logger, err error) {
rawJSON := []byte(fmt.Sprintf(`{ rawJSON := []byte(fmt.Sprintf(`{
"level": "%s", "level": "%s",
"encoding": "json", "encoding": "json",
"outputPaths": ["stdout", "/tmp/logs"], "outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"], "errorOutputPaths": ["stderr"],
"encoderConfig": { "encoderConfig": {
"messageKey": "message", "messageKey": "message",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册