Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
jenkins-cli
提交
44ca7d17
J
jenkins-cli
项目概览
Jenkins 中文社区
/
jenkins-cli
通知
49
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins-cli
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
44ca7d17
编写于
12月 30, 2019
作者:
LinuxSuRen
提交者:
GitHub
12月 30, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add zsh and powerShell completion support (#296)
上级
4e3caf9c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
91 addition
and
17 deletion
+91
-17
app/cmd/completion.go
app/cmd/completion.go
+36
-5
app/cmd/completion_test.go
app/cmd/completion_test.go
+54
-11
util/logger.go
util/logger.go
+1
-1
未找到文件。
app/cmd/completion.go
浏览文件 @
44ca7d17
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
(
"Gener
e
ate bash completion scripts"
),
Short
:
i18n
.
T
(
"Generate bash completion scripts"
),
Long
:
i18n
.
T
(
"Gener
e
ate 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
},
},
}
}
app/cmd/completion_test.go
浏览文件 @
44ca7d17
...
@@ -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"
))
})
})
})
})
})
})
util/logger.go
浏览文件 @
44ca7d17
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录