Skip to content

Commit

Permalink
feat:add log print function with exit code
Browse files Browse the repository at this point in the history
ass log ptint func

Log:
Change-Id: I46cb977e39e8c00b794376c70a88384bd555e223
  • Loading branch information
feiguoL committed Jan 16, 2025
1 parent bd33a1b commit 8616825
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ gen-data-darwin: go-bindata-download-darwin

verify: dep tools lint

pre-build: fmt vet
pre-build:
export GO111MODULE=on
export GOPROXY=https://goproxy.io
go mod tidy
Expand Down
102 changes: 67 additions & 35 deletions app/cmd/job_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,31 @@ type JobLogOption struct {
LastBuildID int
LastBuildURL string
NumberOfLines int
ExitCode bool

RoundTripper http.RoundTripper
}

var jobLogOption JobLogOption

const (
LogFinishMsg = "finish end the job logs"
JobResultSuccess = "SUCCESS"
JobResultFailed = "FAILURE"
)

func init() {
jobCmd.AddCommand(jobLogCmd)
jobLogCmd.Flags().IntVarP(&jobLogOption.History, "history", "s", -1,
i18n.T("Specific build history of log"))
jobLogCmd.Flags().BoolVarP(&jobLogOption.Watch, "watch", "w", false,
i18n.T("Watch the job logs"))
jobLogCmd.Flags().IntVarP(&jobLogOption.Interval, "interval", "i", 1,
i18n.T("Interval of watch"))
jobLogCmd.Flags().IntVarP(&jobLogOption.Interval, "interval", "i", 10,
i18n.T("Interval of watch seconds"))
jobLogCmd.Flags().IntVarP(&jobLogOption.NumberOfLines, "tail", "t", -1,
i18n.T("The last number of lines of the log"))
jobLogCmd.Flags().BoolVarP(&jobLogOption.ExitCode, "exit-code", "e", false,
i18n.T("Watch the job logs with job state, failed exit 1"))
}

var jobLogCmd = &cobra.Command{
Expand All @@ -48,6 +57,7 @@ It'll print the log text of the last build if you don't give the build id.`),
Example: `jcli job log <jobName> [buildID]
jcli job log <jobName> --history 1
jcli job log <jobName> --watch
jcli job log <jobName> --watch --exit-code
jcli job log <jobName> --tail <numberOfLines>`,
PreRunE: func(_ *cobra.Command, args []string) (err error) {
if len(args) >= 3 && jobLogOption.History == -1 {
Expand All @@ -62,39 +72,9 @@ jcli job log <jobName> --tail <numberOfLines>`,
return
},
Run: func(cmd *cobra.Command, args []string) {
name := args[0]

jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: jobLogOption.RoundTripper,
},
}
getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))

lastBuildID := -1
var jobBuild *client.JobBuild
var err error
for {
if jobBuild, err = jclient.GetBuild(name, jobLogOption.History); err == nil {
jobLogOption.LastBuildID = jobBuild.Number
jobLogOption.LastBuildURL = jobBuild.URL
} else {
break
}

if lastBuildID != jobLogOption.LastBuildID {
lastBuildID = jobLogOption.LastBuildID
cmd.Println("Current build number:", jobLogOption.LastBuildID)
cmd.Println("Current build url:", jobLogOption.LastBuildURL)

err = printLog(jclient, cmd, name, jobLogOption.History, 0, jobLogOption.NumberOfLines)
}

if err != nil || !jobLogOption.Watch {
break
}

time.Sleep(time.Duration(jobLogOption.Interval) * time.Second)
err := printLogRunFunc(args[0], jobLogOption, cmd)
if err != nil {
logger.Sugar().Infof("[ERR] print log func error %v", err.Error())
}
},
}
Expand Down Expand Up @@ -133,7 +113,59 @@ func printLog(jclient *client.JobClient, cmd *cobra.Command, jobName string, his

if jobLog.HasMore {
err = printLog(jclient, cmd, jobName, history, jobLog.NextStart, numberOfLines)
} else {
err = fmt.Errorf(LogFinishMsg)
}
}
return
}

func printLogRunFunc(jobName string, jobLogOption JobLogOption, cmd *cobra.Command) (err error) {
name := jobName

jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: jobLogOption.RoundTripper,
},
}
getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))

lastBuildID := -1
var jobBuild *client.JobBuild
for {
if jobBuild, err = jclient.GetBuild(name, jobLogOption.History); err == nil {
jobLogOption.LastBuildID = jobBuild.Number
jobLogOption.LastBuildURL = jobBuild.URL
} else {
logger.Sugar().Fatal("[ERR] get job build info failed...")
}

if lastBuildID != jobLogOption.LastBuildID {
lastBuildID = jobLogOption.LastBuildID
cmd.Println("Current build number:", jobLogOption.LastBuildID)
cmd.Println("Current build url:", jobLogOption.LastBuildURL)

err = printLog(jclient, cmd, name, jobLogOption.History, 0, jobLogOption.NumberOfLines)
}

if err != nil || !jobLogOption.Watch {
if err.Error() == LogFinishMsg {
err = nil
cmd.Println("[INFO] current log finish output")
if jobLogOption.ExitCode {
if jobBuild, err = jclient.GetBuild(name, jobLogOption.History); err == nil {
if jobBuild.Result == JobResultFailed {
logger.Sugar().Fatalf("[ERR] job build %v end with build state failed [%vconsole], exit...", jobLogOption.LastBuildID, jobLogOption.LastBuildURL)
}
} else {
logger.Sugar().Fatal("[ERR] get job build info failed...")
}
}
}
break
}

time.Sleep(time.Duration(jobLogOption.Interval) * time.Second)
}
return
}

0 comments on commit 8616825

Please sign in to comment.