Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
jenkins-cli
提交
0e8e1b34
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,发现更多精彩内容 >>
提交
0e8e1b34
编写于
6月 26, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add option to uninstall plugin
上级
69ee6327
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
127 addition
and
9 deletion
+127
-9
app/cmd/plugin.go
app/cmd/plugin.go
+27
-9
client/pluginManger.go
client/pluginManger.go
+100
-0
未找到文件。
app/cmd/plugin.go
浏览文件 @
0e8e1b34
...
...
@@ -24,6 +24,9 @@ type PluginOptions struct {
Upload
bool
CheckUpdate
bool
Open
bool
List
bool
Uninstall
string
}
func
init
()
{
...
...
@@ -31,6 +34,8 @@ func init() {
pluginCmd
.
PersistentFlags
()
.
BoolVarP
(
&
pluginOpt
.
Upload
,
"upload"
,
"u"
,
false
,
"Upload plugin to your Jenkins server"
)
pluginCmd
.
PersistentFlags
()
.
BoolVarP
(
&
pluginOpt
.
CheckUpdate
,
"check"
,
"c"
,
false
,
"Checkout update center server"
)
pluginCmd
.
PersistentFlags
()
.
BoolVarP
(
&
pluginOpt
.
Open
,
"open"
,
"o"
,
false
,
"Open the browse with the address of plugin manager"
)
pluginCmd
.
PersistentFlags
()
.
BoolVarP
(
&
pluginOpt
.
List
,
"list"
,
"l"
,
false
,
"Print all the plugins which are installed"
)
pluginCmd
.
PersistentFlags
()
.
StringVarP
(
&
pluginOpt
.
Uninstall
,
"uninstall"
,
""
,
""
,
"Uninstall a plugin by shortName"
)
viper
.
BindPFlag
(
"upload"
,
pluginCmd
.
PersistentFlags
()
.
Lookup
(
"upload"
))
}
...
...
@@ -80,15 +85,13 @@ var pluginCmd = &cobra.Command{
}
}
config
:=
getCurrentJenkins
()
jenkins
:=
getCurrentJenkins
()
jclient
:=
&
client
.
PluginManager
{}
jclient
.
URL
=
jenkins
.
URL
jclient
.
UserName
=
config
.
UserName
jclient
.
Token
=
config
.
Token
if
pluginOpt
.
CheckUpdate
{
jclient
:=
&
client
.
PluginManager
{}
crumb
,
config
:=
getCrumb
()
jclient
.
CrumbRequestField
=
crumb
.
CrumbRequestField
jclient
.
Crumb
=
crumb
.
Crumb
jclient
.
URL
=
config
.
URL
jclient
.
UserName
=
config
.
UserName
jclient
.
Token
=
config
.
Token
jclient
.
CheckUpdate
(
func
(
response
*
http
.
Response
)
{
code
:=
response
.
StatusCode
if
code
==
200
{
...
...
@@ -102,13 +105,28 @@ var pluginCmd = &cobra.Command{
}
if
pluginOpt
.
Open
{
jenkins
:=
getCurrentJenkins
()
if
jenkins
.
URL
!=
""
{
open
(
fmt
.
Sprintf
(
"%s/pluginManager"
,
jenkins
.
URL
))
}
else
{
log
.
Fatal
(
fmt
.
Sprintf
(
"No URL fond from %s"
,
jenkins
.
Name
))
}
}
if
pluginOpt
.
List
{
if
plugins
,
err
:=
jclient
.
GetPlugins
();
err
==
nil
{
for
i
,
plugin
:=
range
plugins
.
Plugins
{
fmt
.
Println
(
"num:"
,
i
,
"name:"
,
plugin
.
ShortName
,
"version:"
,
plugin
.
Version
)
}
}
else
{
log
.
Fatal
(
err
)
}
}
if
pluginOpt
.
Uninstall
!=
""
{
if
err
:=
jclient
.
UninstallPlugin
(
pluginOpt
.
Uninstall
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
},
}
...
...
client/pluginManger.go
浏览文件 @
0e8e1b34
...
...
@@ -2,7 +2,9 @@ package client
import
(
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
...
...
@@ -11,6 +13,30 @@ type PluginManager struct {
JenkinsCore
}
// PluginList represent a list of plugins
type
PluginList
struct
{
Plugins
[]
Plugin
}
// Plugin represent the plugin from Jenkins
type
Plugin
struct
{
Active
bool
ShortName
string
LongName
string
Version
string
URL
string
HasUpdate
bool
Enable
bool
Downgradable
bool
Pinned
bool
RequiredCoreVesion
string
MinimumJavaVersion
string
SupportDynamicLoad
string
Deleted
bool
Bundled
bool
BackVersion
string
}
// CheckUpdate fetch the lastest plugins from update center site
func
(
p
*
PluginManager
)
CheckUpdate
(
handle
func
(
*
http
.
Response
))
{
api
:=
fmt
.
Sprintf
(
"%s/pluginManager/checkUpdatesServer"
,
p
.
URL
)
...
...
@@ -36,6 +62,80 @@ func (p *PluginManager) CheckUpdate(handle func(*http.Response)) {
}
}
func
(
p
*
PluginManager
)
GetPlugins
()
(
pluginList
*
PluginList
,
err
error
)
{
api
:=
fmt
.
Sprintf
(
"%s/pluginManager/api/json?pretty=true&depth=1"
,
p
.
URL
)
var
(
req
*
http
.
Request
response
*
http
.
Response
)
req
,
err
=
http
.
NewRequest
(
"GET"
,
api
,
nil
)
if
err
==
nil
{
p
.
AuthHandle
(
req
)
}
else
{
return
}
tr
:=
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
},
}
client
:=
&
http
.
Client
{
Transport
:
tr
}
if
response
,
err
=
client
.
Do
(
req
);
err
==
nil
{
code
:=
response
.
StatusCode
var
data
[]
byte
data
,
err
=
ioutil
.
ReadAll
(
response
.
Body
)
if
code
==
200
{
if
err
==
nil
{
pluginList
=
&
PluginList
{}
err
=
json
.
Unmarshal
(
data
,
pluginList
)
}
}
else
{
log
.
Fatal
(
string
(
data
))
}
}
else
{
log
.
Fatal
(
err
)
}
return
}
// UninstallPlugin uninstall a plugin by name
func
(
p
*
PluginManager
)
UninstallPlugin
(
name
string
)
(
err
error
)
{
api
:=
fmt
.
Sprintf
(
"%s/pluginManager/plugin/%s/uninstall"
,
p
.
URL
,
name
)
var
(
req
*
http
.
Request
response
*
http
.
Response
)
req
,
err
=
http
.
NewRequest
(
"POST"
,
api
,
nil
)
if
err
==
nil
{
p
.
AuthHandle
(
req
)
}
else
{
return
}
if
err
=
p
.
CrumbHandle
(
req
);
err
!=
nil
{
return
}
tr
:=
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
},
}
client
:=
&
http
.
Client
{
Transport
:
tr
}
if
response
,
err
=
client
.
Do
(
req
);
err
==
nil
{
code
:=
response
.
StatusCode
var
data
[]
byte
data
,
err
=
ioutil
.
ReadAll
(
response
.
Body
)
if
code
==
200
{
fmt
.
Println
(
"uninstall succeed."
)
}
else
{
log
.
Fatal
(
string
(
data
))
}
}
else
{
log
.
Fatal
(
err
)
}
return
}
func
(
p
*
PluginManager
)
handleCheck
(
handle
func
(
*
http
.
Response
))
func
(
*
http
.
Response
)
{
if
handle
==
nil
{
handle
=
func
(
*
http
.
Response
)
{}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录