Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
jenkins-cli
提交
06889be4
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,发现更多精彩内容 >>
未验证
提交
06889be4
编写于
7月 20, 2019
作者:
Z
Zhao Xiaojie
提交者:
GitHub
7月 20, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #36 from LinuxSuRen/fea/download-plugins
Add support to download plugins
上级
c32a9a12
b52cf41a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
126 addition
and
0 deletion
+126
-0
app/cmd/plugin_download.go
app/cmd/plugin_download.go
+25
-0
client/pluginManger.go
client/pluginManger.go
+101
-0
未找到文件。
app/cmd/plugin_download.go
0 → 100644
浏览文件 @
06889be4
package
cmd
import
(
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
func
init
()
{
pluginCmd
.
AddCommand
(
pluginDownloadCmd
)
}
var
pluginDownloadCmd
=
&
cobra
.
Command
{
Use
:
"download <keyword>"
,
Short
:
"Download the plugins"
,
Long
:
`Download the plugins`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
len
(
args
)
==
0
{
cmd
.
Help
()
return
}
jclient
:=
&
client
.
PluginDownloader
{}
jclient
.
DownloadPlugins
(
args
)
},
}
client/pluginManger.go
浏览文件 @
06889be4
...
@@ -65,6 +65,30 @@ type InstalledPlugin struct {
...
@@ -65,6 +65,30 @@ type InstalledPlugin struct {
BackVersion
string
BackVersion
string
}
}
type
PluginDependency
struct
{
Name
string
`json:"name"`
Implied
bool
`json:"implied"`
Optional
bool
`json:"optional"`
Title
string
`json:"title"`
Version
string
`json:"version"`
}
type
PluginInfo
struct
{
BuildDate
string
`json:"buildDate"`
Dependencies
[]
PluginDependency
`json:"dependencies"`
Excerpt
string
`json:"excerpt"`
FirstRelease
string
`json:"firstRelease"`
Gav
string
`json:"gav"`
Name
string
`json:"name"`
PreviousTimestamp
string
`json:"previousTimestamp"`
PreviousVersion
string
`json:"previousVersion"`
ReleaseTimestamp
string
`json:"releaseTimestamp"`
RequireCore
string
`json:"RequireCore"`
Title
string
`json:"title"`
URL
string
`json:"url"`
Version
string
`json:"version"`
}
// CheckUpdate fetch the lastest plugins from update center site
// CheckUpdate fetch the lastest plugins from update center site
func
(
p
*
PluginManager
)
CheckUpdate
(
handle
func
(
*
http
.
Response
))
{
func
(
p
*
PluginManager
)
CheckUpdate
(
handle
func
(
*
http
.
Response
))
{
api
:=
fmt
.
Sprintf
(
"%s/pluginManager/checkUpdatesServer"
,
p
.
URL
)
api
:=
fmt
.
Sprintf
(
"%s/pluginManager/checkUpdatesServer"
,
p
.
URL
)
...
@@ -328,3 +352,80 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path
...
@@ -328,3 +352,80 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path
req
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
req
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
return
req
,
err
return
req
,
err
}
}
type
PluginDownloader
struct
{
dependencyMap
map
[
string
]
string
}
// DownloadPlugins will download those plugins from update center
func
(
d
*
PluginDownloader
)
DownloadPlugins
(
names
[]
string
)
{
d
.
dependencyMap
=
make
(
map
[
string
]
string
)
fmt
.
Println
(
"Start to collect plugin dependencies..."
)
plugins
:=
make
([]
PluginInfo
,
0
)
for
_
,
name
:=
range
names
{
plugins
=
append
(
plugins
,
d
.
collectDependencies
(
name
)
...
)
}
fmt
.
Printf
(
"Ready to download plugins, total: %d.
\n
"
,
len
(
plugins
))
for
i
,
plugin
:=
range
plugins
{
fmt
.
Printf
(
"Start to download plugin %s, version: %s, number: %d
\n
"
,
plugin
.
Name
,
plugin
.
Version
,
i
)
d
.
download
(
plugin
.
URL
,
plugin
.
Name
)
}
}
func
(
d
*
PluginDownloader
)
download
(
url
string
,
name
string
)
{
if
resp
,
err
:=
http
.
Get
(
url
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
else
{
defer
resp
.
Body
.
Close
()
if
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
else
{
if
err
=
ioutil
.
WriteFile
(
fmt
.
Sprintf
(
"%s.hpi"
,
name
),
body
,
0644
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
}
}
}
func
(
d
*
PluginDownloader
)
getPlugin
(
name
string
)
(
plugin
*
PluginInfo
,
err
error
)
{
resp
,
err
:=
http
.
Get
(
"https://plugins.jenkins.io/api/plugin/"
+
name
)
if
err
!=
nil
{
return
plugin
,
err
}
defer
resp
.
Body
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
plugin
=
&
PluginInfo
{}
err
=
json
.
Unmarshal
(
body
,
plugin
)
if
err
!=
nil
{
log
.
Println
(
"error when unmarshal:"
,
string
(
body
))
}
return
}
func
(
d
*
PluginDownloader
)
collectDependencies
(
pluginName
string
)
(
plugins
[]
PluginInfo
)
{
plugin
,
err
:=
d
.
getPlugin
(
pluginName
)
if
err
!=
nil
{
log
.
Println
(
"can't get the plugin by name:"
,
pluginName
)
panic
(
err
)
}
plugins
=
make
([]
PluginInfo
,
0
)
plugins
=
append
(
plugins
,
*
plugin
)
for
_
,
dependency
:=
range
plugin
.
Dependencies
{
if
dependency
.
Optional
{
continue
}
if
_
,
ok
:=
d
.
dependencyMap
[
dependency
.
Name
];
!
ok
{
d
.
dependencyMap
[
dependency
.
Name
]
=
dependency
.
Version
plugins
=
append
(
plugins
,
d
.
collectDependencies
(
dependency
.
Name
)
...
)
}
}
return
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录