Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins-cli
提交
3a3f957d
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 搜索 >>
提交
3a3f957d
编写于
7月 01, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow user to edit a pipeline
上级
0b721233
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
178 addition
and
1 deletion
+178
-1
app/cmd/job.go
app/cmd/job.go
+1
-1
app/cmd/job_edit.go
app/cmd/job_edit.go
+75
-0
app/cmd/plugin.go
app/cmd/plugin.go
+7
-0
app/cmd/restart.go
app/cmd/restart.go
+1
-0
client/job.go
client/job.go
+94
-0
未找到文件。
app/cmd/job.go
浏览文件 @
3a3f957d
...
...
@@ -21,7 +21,7 @@ func init() {
rootCmd
.
AddCommand
(
jobCmd
)
jobCmd
.
PersistentFlags
()
.
StringVarP
(
&
jobOption
.
Format
,
"output"
,
"o"
,
"json"
,
"Format the output"
)
jobCmd
.
PersistentFlags
()
.
StringVarP
(
&
jobOption
.
Name
,
"name"
,
"n"
,
""
,
"Name of the job"
)
jobCmd
.
Persistent
Flags
()
.
BoolVarP
(
&
jobOption
.
History
,
"history"
,
""
,
false
,
"Print the build history of job"
)
jobCmd
.
Flags
()
.
BoolVarP
(
&
jobOption
.
History
,
"history"
,
""
,
false
,
"Print the build history of job"
)
}
var
jobCmd
=
&
cobra
.
Command
{
...
...
app/cmd/job_edit.go
0 → 100644
浏览文件 @
3a3f957d
package
cmd
import
(
"errors"
"fmt"
"log"
"github.com/AlecAivazis/survey"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
func
init
()
{
jobCmd
.
AddCommand
(
jobEditCmd
)
}
var
jobEditCmd
=
&
cobra
.
Command
{
Use
:
"edit -n"
,
Short
:
"Edit the job of your Jenkins"
,
Long
:
`Edit the job of your Jenkins`
,
Args
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
if
jobOption
.
Name
==
""
{
return
errors
.
New
(
"requires job name"
)
}
return
nil
},
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
var
content
string
var
err
error
if
content
,
err
=
getPipeline
(
jobOption
.
Name
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
prompt
:=
&
survey
.
Editor
{
Message
:
"Edit your pipeline script"
,
FileName
:
"*.sh"
,
Default
:
content
,
AppendDefault
:
true
,
}
fmt
.
Println
(
content
)
if
err
=
survey
.
AskOne
(
prompt
,
&
content
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Println
(
content
)
jenkins
:=
getCurrentJenkins
()
jclient
:=
&
client
.
JobClient
{}
jclient
.
URL
=
jenkins
.
URL
jclient
.
UserName
=
jenkins
.
UserName
jclient
.
Token
=
jenkins
.
Token
jclient
.
Proxy
=
jenkins
.
Proxy
jclient
.
ProxyAuth
=
jenkins
.
ProxyAuth
if
err
=
jclient
.
UpdatePipeline
(
jobOption
.
Name
,
content
);
err
!=
nil
{
fmt
.
Println
(
"update failed"
)
log
.
Fatal
(
err
)
}
},
}
func
getPipeline
(
name
string
)
(
script
string
,
err
error
)
{
jenkins
:=
getCurrentJenkins
()
jclient
:=
&
client
.
JobClient
{}
jclient
.
URL
=
jenkins
.
URL
jclient
.
UserName
=
jenkins
.
UserName
jclient
.
Token
=
jenkins
.
Token
jclient
.
Proxy
=
jenkins
.
Proxy
jclient
.
ProxyAuth
=
jenkins
.
ProxyAuth
var
job
*
client
.
Pipeline
if
job
,
err
=
jclient
.
GetPipeline
(
name
);
err
==
nil
{
script
=
job
.
Script
}
return
}
app/cmd/plugin.go
浏览文件 @
3a3f957d
...
...
@@ -50,6 +50,13 @@ var pluginCmd = &cobra.Command{
Use
:
"plugin"
,
Short
:
"Manage the plugins of Jenkins"
,
Long
:
`Manage the plugins of Jenkins`
,
PreRun
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
fmt
.
Printf
(
"Inside subCmd PreRun with args: %v
\n
"
,
args
)
if
pluginOpt
.
Upload
{
fmt
.
Println
(
"sdfsdf"
)
}
},
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
pluginOpt
.
Upload
{
crumb
,
config
:=
getCrumb
()
...
...
app/cmd/restart.go
浏览文件 @
3a3f957d
...
...
@@ -48,6 +48,7 @@ var restartCmd = &cobra.Command{
if
response
.
StatusCode
!=
200
{
if
data
,
err
:=
ioutil
.
ReadAll
(
response
.
Body
);
err
==
nil
{
fmt
.
Println
(
string
(
data
))
fmt
.
Println
(
response
.
StatusCode
)
}
else
{
log
.
Fatal
(
err
)
}
...
...
client/job.go
浏览文件 @
3a3f957d
...
...
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
...
...
@@ -125,6 +126,94 @@ func (q *JobClient) GetJob(name string) (job *Job, err error) {
return
}
func
(
q
*
JobClient
)
UpdatePipeline
(
name
,
script
string
)
(
err
error
)
{
jobItems
:=
strings
.
Split
(
name
,
" "
)
path
:=
""
for
i
,
item
:=
range
jobItems
{
if
i
==
0
{
path
=
fmt
.
Sprintf
(
"job/%s"
,
item
)
}
else
{
path
=
fmt
.
Sprintf
(
"%s/job/%s"
,
path
,
item
)
}
}
api
:=
fmt
.
Sprintf
(
"%s/%s/wfapisu/update"
,
q
.
URL
,
path
)
var
(
req
*
http
.
Request
response
*
http
.
Response
)
fmt
.
Println
(
api
)
formData
:=
url
.
Values
{
"script"
:
{
script
}}
payload
:=
strings
.
NewReader
(
formData
.
Encode
())
req
,
err
=
http
.
NewRequest
(
"POST"
,
api
,
payload
)
if
err
==
nil
{
q
.
AuthHandle
(
req
)
}
else
{
return
}
if
err
=
q
.
CrumbHandle
(
req
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
req
.
Header
.
Add
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
// req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
// req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client
:=
q
.
GetClient
()
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
(
"updated"
)
}
else
{
fmt
.
Println
(
"code"
,
code
)
log
.
Fatal
(
string
(
data
))
}
}
else
{
fmt
.
Println
(
"request is error"
)
log
.
Fatal
(
err
)
}
return
}
func
(
q
*
JobClient
)
GetPipeline
(
name
string
)
(
pipeline
*
Pipeline
,
err
error
)
{
jobItems
:=
strings
.
Split
(
name
,
" "
)
path
:=
""
for
_
,
item
:=
range
jobItems
{
path
=
fmt
.
Sprintf
(
"%s/job/%s"
,
path
,
item
)
}
api
:=
fmt
.
Sprintf
(
"%s/%s/wfapisu/script"
,
q
.
URL
,
path
)
var
(
req
*
http
.
Request
response
*
http
.
Response
)
req
,
err
=
http
.
NewRequest
(
"GET"
,
api
,
nil
)
if
err
==
nil
{
q
.
AuthHandle
(
req
)
}
else
{
return
}
client
:=
q
.
GetClient
()
if
response
,
err
=
client
.
Do
(
req
);
err
==
nil
{
code
:=
response
.
StatusCode
var
data
[]
byte
data
,
err
=
ioutil
.
ReadAll
(
response
.
Body
)
if
code
==
200
{
pipeline
=
&
Pipeline
{}
err
=
json
.
Unmarshal
(
data
,
pipeline
)
}
else
{
log
.
Fatal
(
string
(
data
))
}
}
else
{
log
.
Fatal
(
err
)
}
return
}
func
(
q
*
JobClient
)
GetHistory
(
name
string
)
(
builds
[]
JobBuild
,
err
error
)
{
var
job
*
Job
if
job
,
err
=
q
.
GetJob
(
name
);
err
==
nil
{
...
...
@@ -213,3 +302,8 @@ type JobBuild struct {
Number
int
URL
string
}
type
Pipeline
struct
{
Script
string
Sandbox
bool
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录