Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
jenkins-cli
提交
fb4eb175
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,发现更多精彩内容 >>
提交
fb4eb175
编写于
7月 17, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add watch for job build log
上级
0a6ed1d9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
108 addition
and
5 deletion
+108
-5
app/cmd/common.go
app/cmd/common.go
+2
-1
app/cmd/job_log.go
app/cmd/job_log.go
+46
-3
client/job.go
client/job.go
+60
-1
未找到文件。
app/cmd/common.go
浏览文件 @
fb4eb175
...
...
@@ -67,5 +67,6 @@ func (b *BatchOption) Confirm(message string) bool {
// WatchOption for the resources which can be watched
type
WatchOption
struct
{
Watch
bool
Watch
bool
Interval
int
}
app/cmd/job_log.go
浏览文件 @
fb4eb175
...
...
@@ -3,20 +3,28 @@ package cmd
import
(
"fmt"
"log"
"time"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
type
JobLogOption
struct
{
WatchOption
History
int
LogText
string
LastBuildID
int
LastBuildURL
string
}
var
jobLogOption
JobLogOption
func
init
()
{
jobCmd
.
AddCommand
(
jobLogCmd
)
jobLogCmd
.
PersistentFlags
()
.
IntVarP
(
&
jobLogOption
.
History
,
"history"
,
"s"
,
-
1
,
"Specific build history of log"
)
jobLogCmd
.
Flags
()
.
IntVarP
(
&
jobLogOption
.
History
,
"history"
,
"s"
,
-
1
,
"Specific build history of log"
)
jobLogCmd
.
Flags
()
.
BoolVarP
(
&
jobLogOption
.
Watch
,
"watch"
,
"w"
,
false
,
"Watch the job logs"
)
jobLogCmd
.
Flags
()
.
IntVarP
(
&
jobLogOption
.
Interval
,
"interval"
,
"i"
,
1
,
"Interval of watch"
)
}
var
jobLogCmd
=
&
cobra
.
Command
{
...
...
@@ -37,13 +45,48 @@ var jobLogCmd = &cobra.Command{
jclient
.
Proxy
=
jenkins
.
Proxy
jclient
.
ProxyAuth
=
jenkins
.
ProxyAuth
printLog
(
jclient
,
jobOption
.
Name
,
jobLogOption
.
History
,
0
)
lastBuildID
:=
-
1
for
{
if
build
,
err
:=
jclient
.
GetBuild
(
jobOption
.
Name
,
-
1
);
err
==
nil
{
jobLogOption
.
LastBuildID
=
build
.
Number
jobLogOption
.
LastBuildURL
=
build
.
URL
}
if
lastBuildID
!=
jobLogOption
.
LastBuildID
{
lastBuildID
=
jobLogOption
.
LastBuildID
fmt
.
Println
(
"Current build number:"
,
jobLogOption
.
LastBuildID
)
fmt
.
Println
(
"Current build url:"
,
jobLogOption
.
LastBuildURL
)
printLog
(
jclient
,
jobOption
.
Name
,
jobLogOption
.
History
,
0
)
}
if
!
jobLogOption
.
Watch
{
break
}
time
.
Sleep
(
time
.
Duration
(
jobLogOption
.
Interval
)
*
time
.
Second
)
}
},
}
func
printLog
(
jclient
*
client
.
JobClient
,
jobName
string
,
history
int
,
start
int64
)
{
if
status
,
err
:=
jclient
.
Log
(
jobName
,
history
,
start
);
err
==
nil
{
fmt
.
Print
(
status
.
Text
)
isNew
:=
false
if
jobLogOption
.
LogText
!=
status
.
Text
{
jobLogOption
.
LogText
=
status
.
Text
isNew
=
true
}
else
if
history
==
-
1
{
if
build
,
err
:=
jclient
.
GetBuild
(
jobName
,
-
1
);
err
==
nil
&&
jobLogOption
.
LastBuildID
!=
build
.
Number
{
jobLogOption
.
LastBuildID
=
build
.
Number
jobLogOption
.
LastBuildURL
=
build
.
URL
isNew
=
true
}
}
if
isNew
{
fmt
.
Print
(
status
.
Text
)
}
if
status
.
HasMore
{
printLog
(
jclient
,
jobName
,
history
,
status
.
NextStart
)
}
...
...
client/job.go
浏览文件 @
fb4eb175
...
...
@@ -89,6 +89,48 @@ func (q *JobClient) Build(jobName string) (err error) {
return
}
func
(
q
*
JobClient
)
GetBuild
(
jobName
string
,
id
int
)
(
job
*
JobBuild
,
err
error
)
{
jobItems
:=
strings
.
Split
(
jobName
,
" "
)
path
:=
""
for
_
,
item
:=
range
jobItems
{
path
=
fmt
.
Sprintf
(
"%s/job/%s"
,
path
,
item
)
}
var
api
string
if
id
==
-
1
{
api
=
fmt
.
Sprintf
(
"%s/%s/lastBuild/api/json"
,
q
.
URL
,
path
)
}
else
{
api
=
fmt
.
Sprintf
(
"%s/%s/%d/api/json"
,
q
.
URL
,
path
,
id
)
}
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
{
job
=
&
JobBuild
{}
err
=
json
.
Unmarshal
(
data
,
job
)
}
else
{
log
.
Fatal
(
string
(
data
))
}
}
else
{
log
.
Fatal
(
err
)
}
return
}
func
(
q
*
JobClient
)
GetJob
(
name
string
)
(
job
*
Job
,
err
error
)
{
jobItems
:=
strings
.
Split
(
name
,
" "
)
path
:=
""
...
...
@@ -299,11 +341,28 @@ type Job struct {
Buildable
bool
}
type
JobBuild
struct
{
type
Simple
JobBuild
struct
{
Number
int
URL
string
}
type
JobBuild
struct
{
SimpleJobBuild
Building
bool
Description
string
DisplayName
string
Duration
int64
EstimatedDuration
int64
FullDisplayName
string
ID
string
KeepLog
bool
QueueID
int
Result
string
Timestamp
int64
PreviousBuild
SimpleJobBuild
NextBuild
SimpleJobBuild
}
type
Pipeline
struct
{
Script
string
Sandbox
bool
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录