Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins-cli
提交
b33eb322
J
jenkins-cli
项目概览
LinuxSuRen
/
jenkins-cli
与 Fork 源项目一致
Fork自
Jenkins 中文社区 / jenkins-cli
通知
1
Star
0
Fork
0
代码
文件
提交
分支
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 搜索 >>
未验证
提交
b33eb322
编写于
8月 25, 2019
作者:
LinuxSuRen
提交者:
GitHub
8月 25, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #117 from LinuxSuRen/prehook
Add support for cmd pre hook
上级
17217cc8
892f8926
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
141 addition
and
8 deletion
+141
-8
.gitignore
.gitignore
+3
-1
Jenkinsfile
Jenkinsfile
+7
-5
Makefile
Makefile
+5
-0
app/cmd/config.go
app/cmd/config.go
+6
-0
app/cmd/plugin_upload.go
app/cmd/plugin_upload.go
+3
-1
app/cmd/root.go
app/cmd/root.go
+42
-0
app/cmd/root_test.go
app/cmd/root_test.go
+55
-0
app/cmd/setup_test.go
app/cmd/setup_test.go
+16
-0
util/setup_test.go
util/setup_test.go
+4
-1
未找到文件。
.gitignore
浏览文件 @
b33eb322
...
@@ -12,4 +12,6 @@
...
@@ -12,4 +12,6 @@
*.out
*.out
bin/
bin/
release/
release/
\ No newline at end of file
*.xml
Jenkinsfile
浏览文件 @
b33eb322
...
@@ -33,13 +33,15 @@ pipeline {
...
@@ -33,13 +33,15 @@ pipeline {
stage
(
'Test'
)
{
stage
(
'Test'
)
{
steps
{
steps
{
container
(
'golang'
)
{
container
(
'golang'
)
{
sh
label:
'go test'
,
script:
'''
sh
label:
'go test'
,
script:
'make test'
go test ./util/... -v
go test ./client/... -v
go test ./app/... -v
'''
}
}
}
}
}
}
}
}
post
{
always
{
junit
allowEmptyResults:
true
,
testResults:
"*/**/*.xml"
}
}
}
}
\ No newline at end of file
Makefile
浏览文件 @
b33eb322
...
@@ -31,6 +31,11 @@ release: clean build-all
...
@@ -31,6 +31,11 @@ release: clean build-all
clean
:
##
Clean the generated artifacts
clean
:
##
Clean the generated artifacts
rm
-rf
bin release
rm
-rf
bin release
test
:
go
test
./util/...
-v
-coverprofile
bin/testcover-util.log
go
test
./client/...
-v
-coverprofile
bin/testcover-client.log
go
test
./app/...
-v
-coverprofile
bin/testcover-app.log
dep
:
dep
:
go get github.com/AlecAivazis/survey/v2
go get github.com/AlecAivazis/survey/v2
go get github.com/gosuri/uiprogress
go get github.com/gosuri/uiprogress
...
...
app/cmd/config.go
浏览文件 @
b33eb322
...
@@ -50,9 +50,15 @@ type JenkinsServer struct {
...
@@ -50,9 +50,15 @@ type JenkinsServer struct {
Description
string
`yaml:"description"`
Description
string
`yaml:"description"`
}
}
type
PreHook
struct
{
Path
string
`yaml:"path"`
Command
string
`yaml:"cmd"`
}
type
Config
struct
{
type
Config
struct
{
Current
string
`yaml:"current"`
Current
string
`yaml:"current"`
JenkinsServers
[]
JenkinsServer
`yaml:"jenkins_servers"`
JenkinsServers
[]
JenkinsServer
`yaml:"jenkins_servers"`
PreHooks
[]
PreHook
`yaml:"preHooks"`
}
}
func
setCurrentJenkins
(
name
string
)
{
func
setCurrentJenkins
(
name
string
)
{
...
...
app/cmd/plugin_upload.go
浏览文件 @
b33eb322
...
@@ -40,7 +40,7 @@ var pluginUploadCmd = &cobra.Command{
...
@@ -40,7 +40,7 @@ var pluginUploadCmd = &cobra.Command{
Long
:
`Upload a plugin from local filesystem or remote URL to your Jenkins`
,
Long
:
`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`
,
PreRun
:
func
(
_
*
cobra
.
Command
,
args
[]
string
)
{
PreRun
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
pluginUploadOption
.
Remote
!=
""
{
if
pluginUploadOption
.
Remote
!=
""
{
file
,
err
:=
ioutil
.
TempFile
(
"."
,
"jcli-plugin"
)
file
,
err
:=
ioutil
.
TempFile
(
"."
,
"jcli-plugin"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -70,6 +70,8 @@ var pluginUploadCmd = &cobra.Command{
...
@@ -70,6 +70,8 @@ var pluginUploadCmd = &cobra.Command{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
}
else
if
len
(
args
)
==
0
{
}
else
if
len
(
args
)
==
0
{
executePreCmd
(
cmd
,
args
)
path
,
_
:=
os
.
Getwd
()
path
,
_
:=
os
.
Getwd
()
dirName
:=
filepath
.
Base
(
path
)
dirName
:=
filepath
.
Base
(
path
)
dirName
=
strings
.
Replace
(
dirName
,
"-plugin"
,
""
,
-
1
)
dirName
=
strings
.
Replace
(
dirName
,
"-plugin"
,
""
,
-
1
)
...
...
app/cmd/root.go
浏览文件 @
b33eb322
...
@@ -4,6 +4,8 @@ import (
...
@@ -4,6 +4,8 @@ import (
"fmt"
"fmt"
"log"
"log"
"os"
"os"
"os/exec"
"strings"
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
...
@@ -62,3 +64,43 @@ func initConfig() {
...
@@ -62,3 +64,43 @@ func initConfig() {
log
.
Fatalf
(
"Config file is invalid: %v"
,
err
)
log
.
Fatalf
(
"Config file is invalid: %v"
,
err
)
}
}
}
}
func
getCmdPath
(
cmd
*
cobra
.
Command
)
string
{
current
:=
cmd
.
Use
if
cmd
.
HasParent
()
{
parentName
:=
getCmdPath
(
cmd
.
Parent
())
if
parentName
==
""
{
return
current
}
return
fmt
.
Sprintf
(
"%s.%s"
,
parentName
,
current
)
}
// don't need the name of root cmd
return
""
}
func
executePreCmd
(
cmd
*
cobra
.
Command
,
_
[]
string
)
{
config
:=
getConfig
()
if
config
==
nil
{
log
.
Fatal
(
"Cannot find config file"
)
return
}
path
:=
getCmdPath
(
cmd
)
for
_
,
preHook
:=
range
config
.
PreHooks
{
if
path
!=
preHook
.
Path
{
continue
}
execute
(
preHook
.
Command
)
}
}
func
execute
(
command
string
)
{
array
:=
strings
.
Split
(
command
,
" "
)
cmd
:=
exec
.
Command
(
array
[
0
],
array
[
1
:
]
...
)
cmd
.
Stdout
=
os
.
Stdout
if
err
:=
cmd
.
Run
();
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
app/cmd/root_test.go
0 → 100644
浏览文件 @
b33eb322
package
cmd
import
(
"github.com/golang/mock/gomock"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
"github.com/spf13/cobra"
)
var
_
=
Describe
(
"Root cmd test"
,
func
()
{
var
(
ctrl
*
gomock
.
Controller
rootCmd
*
cobra
.
Command
)
BeforeEach
(
func
()
{
ctrl
=
gomock
.
NewController
(
GinkgoT
())
rootCmd
=
&
cobra
.
Command
{
Use
:
"root"
}
})
AfterEach
(
func
()
{
ctrl
.
Finish
()
})
Context
(
"PreHook test"
,
func
()
{
It
(
"only with root cmd"
,
func
()
{
path
:=
getCmdPath
(
rootCmd
)
Expect
(
path
)
.
To
(
Equal
(
""
))
})
It
(
"one sub cmd"
,
func
()
{
subCmd
:=
&
cobra
.
Command
{
Use
:
"sub"
,
}
rootCmd
.
AddCommand
(
subCmd
)
path
:=
getCmdPath
(
subCmd
)
Expect
(
path
)
.
To
(
Equal
(
"sub"
))
})
It
(
"two sub cmds"
,
func
()
{
sub1Cmd
:=
&
cobra
.
Command
{
Use
:
"sub1"
,
}
sub2Cmd
:=
&
cobra
.
Command
{
Use
:
"sub2"
,
}
rootCmd
.
AddCommand
(
sub1Cmd
)
sub1Cmd
.
AddCommand
(
sub2Cmd
)
path
:=
getCmdPath
(
sub2Cmd
)
Expect
(
path
)
.
To
(
Equal
(
"sub1.sub2"
))
})
})
})
app/cmd/setup_test.go
0 → 100644
浏览文件 @
b33eb322
package
cmd
import
(
"testing"
"github.com/onsi/ginkgo/reporters"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
)
func
TestApp
(
t
*
testing
.
T
)
{
RegisterFailHandler
(
Fail
)
junitReporter
:=
reporters
.
NewJUnitReporter
(
"test-app.xml"
)
RunSpecsWithDefaultAndCustomReporters
(
t
,
"app/cmd"
,
[]
Reporter
{
junitReporter
})
}
util/setup_test.go
浏览文件 @
b33eb322
...
@@ -3,11 +3,14 @@ package util
...
@@ -3,11 +3,14 @@ package util
import
(
import
(
"testing"
"testing"
"github.com/onsi/ginkgo/reporters"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
.
"github.com/onsi/gomega"
)
)
func
TestUtils
(
t
*
testing
.
T
)
{
func
TestUtils
(
t
*
testing
.
T
)
{
RegisterFailHandler
(
Fail
)
RegisterFailHandler
(
Fail
)
RunSpecs
(
t
,
"jcli utils test"
)
junitReporter
:=
reporters
.
NewJUnitReporter
(
"test-utils.xml"
)
RunSpecsWithDefaultAndCustomReporters
(
t
,
"util"
,
[]
Reporter
{
junitReporter
})
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录