diff --git a/app/cmd/completion.go b/app/cmd/completion.go index 289ee7a47611644c8f4ba98ed9dd3ecdbd22a74d..166009a2ef6696d332f786fab5ba8c35abc099d5 100644 --- a/app/cmd/completion.go +++ b/app/cmd/completion.go @@ -1,18 +1,33 @@ package cmd import ( + "fmt" "github.com/jenkins-zh/jenkins-cli/app/i18n" "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() { 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{ Use: "completion", - Short: i18n.T("Genereate bash completion scripts"), - Long: i18n.T("Genereate bash completion scripts"), + Short: i18n.T("Generate bash completion scripts"), + Long: i18n.T("Generate bash completion scripts"), Example: `# Installing bash completion on macOS using homebrew ## If running Bash 3.2 included with macOS brew install bash-completion @@ -20,8 +35,24 @@ var completionCmd = &cobra.Command{ brew install bash-completion@2 ## you may need add the completion to your completion directory jcli completion > $(brew --prefix)/etc/bash_completion.d/jcli - ## 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()) + ## If you get trouble, please visit https://github.com/jenkins-zh/jenkins-cli/issues/83. + + # 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 }, } diff --git a/app/cmd/completion_test.go b/app/cmd/completion_test.go index a838fd9a2351c6b34e46d19461ed07ca7476562d..5451110d57b204f638eeac4fe9be280effe479d7 100644 --- a/app/cmd/completion_test.go +++ b/app/cmd/completion_test.go @@ -10,9 +10,12 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("job type command", func() { +var _ = Describe("completion command", func() { var ( - ctrl *gomock.Controller + ctrl *gomock.Controller + cmdArgs []string + buf *bytes.Buffer + err error ) BeforeEach(func() { @@ -20,6 +23,20 @@ var _ = Describe("job type command", func() { rootCmd.SetArgs([]string{}) rootOptions.Jenkins = "" 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() { @@ -29,21 +46,47 @@ var _ = Describe("job type command", func() { ctrl.Finish() }) - Context("basic cases", func() { - It("should success, empty list", func() { - data, err := generateSampleConfig() + Context("with default option value", func() { + BeforeEach(func() { + cmdArgs = []string{"completion"} + }) + + It("should success", func() { 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(buf.String()).NotTo(Equal("")) + }) + }) - rootCmd.SetArgs([]string{"completion"}) + Context("generate powerShell completion", func() { + BeforeEach(func() { + cmdArgs = []string{"completion", "--type", "powerShell"} + }) - buf := new(bytes.Buffer) - rootCmd.SetOutput(buf) - _, err = rootCmd.ExecuteC() + It("should success", func() { 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")) }) }) }) diff --git a/util/logger.go b/util/logger.go index 57f3924639ae5251430371a34c5fab2c3f29fda8..81aa18f561265f64106532e82e7e4a76c01c42da 100644 --- a/util/logger.go +++ b/util/logger.go @@ -11,7 +11,7 @@ func InitLogger(level string) (logger *zap.Logger, err error) { rawJSON := []byte(fmt.Sprintf(`{ "level": "%s", "encoding": "json", - "outputPaths": ["stdout", "/tmp/logs"], + "outputPaths": ["stdout"], "errorOutputPaths": ["stderr"], "encoderConfig": { "messageKey": "message",