-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add -tail to job log #588
add -tail to job log #588
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,22 @@ import ( | |
"fmt" | ||
"github.com/jenkins-zh/jenkins-cli/app/cmd/common" | ||
"github.com/jenkins-zh/jenkins-cli/app/i18n" | ||
"github.com/jenkins-zh/jenkins-cli/client" | ||
"github.com/spf13/cobra" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jenkins-zh/jenkins-cli/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// JobLogOption is the job log option | ||
type JobLogOption struct { | ||
common.WatchOption | ||
History int | ||
|
||
LogText string | ||
LastBuildID int | ||
LastBuildURL string | ||
NumberOfLines int | ||
|
||
RoundTripper http.RoundTripper | ||
} | ||
|
@@ -34,6 +34,8 @@ func init() { | |
i18n.T("Watch the job logs")) | ||
jobLogCmd.Flags().IntVarP(&jobLogOption.Interval, "interval", "i", 1, | ||
i18n.T("Interval of watch")) | ||
jobLogCmd.Flags().IntVarP(&jobLogOption.NumberOfLines,"tail","t",-1, | ||
i18n.T("the last number of lines of the log")) | ||
} | ||
|
||
var jobLogCmd = &cobra.Command{ | ||
|
@@ -44,16 +46,24 @@ It'll print the log text of the last build if you don't give the build id.`), | |
Args: cobra.MinimumNArgs(1), | ||
Example: `jcli job log <jobName> [buildID] | ||
jcli job log <jobName> --history 1 | ||
jcli job log <jobName> --watch`, | ||
jcli job log <jobName> --watch | ||
jcli job log <jobName> --tail <numberOfLines>`, | ||
PreRunE: func(_ *cobra.Command, args []string) (err error) { | ||
if len(args) >= 2 && jobLogOption.History == -1 { | ||
if len(args) >= 3 && jobLogOption.History == -1 { | ||
var history int | ||
historyStr := args[1] | ||
if history, err = strconv.Atoi(historyStr); err == nil { | ||
jobLogOption.History = history | ||
} else { | ||
err = fmt.Errorf("job history must be a number instead of '%s'", historyStr) | ||
} | ||
numberOfLinesStr :=args[2] | ||
numberOfLines, err:=strconv.Atoi(numberOfLinesStr) | ||
if err!=nil||numberOfLines<=0 { | ||
err =fmt.Errorf(err.Error(),"lines of job must be a positive integer instead of '%s'",numberOfLinesStr) | ||
}else{ | ||
jobLogOption.NumberOfLines=numberOfLines | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not necessary to parse the value of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All righty. I've changed that. The lines of 'Current build number' and 'Current build url' are not counted in the 'tail number', but there was a bug about the number of lines to be printed and it has been fixed. Thx. |
||
} | ||
return | ||
}, | ||
|
@@ -83,7 +93,7 @@ jcli job log <jobName> --watch`, | |
cmd.Println("Current build number:", jobLogOption.LastBuildID) | ||
cmd.Println("Current build url:", jobLogOption.LastBuildURL) | ||
|
||
err = printLog(jclient, cmd, name, jobLogOption.History, 0) | ||
err = printLog(jclient, cmd, name, jobLogOption.History, 0,jobLogOption.NumberOfLines) | ||
} | ||
|
||
if err != nil || !jobLogOption.Watch { | ||
|
@@ -95,7 +105,7 @@ jcli job log <jobName> --watch`, | |
}, | ||
} | ||
|
||
func printLog(jclient *client.JobClient, cmd *cobra.Command, jobName string, history int, start int64) (err error) { | ||
func printLog(jclient *client.JobClient, cmd *cobra.Command, jobName string, history int, start int64,numberOfLines int) (err error) { | ||
var jobLog client.JobLog | ||
if jobLog, err = jclient.Log(jobName, history, start); err == nil { | ||
isNew := false | ||
|
@@ -111,12 +121,25 @@ func printLog(jclient *client.JobClient, cmd *cobra.Command, jobName string, his | |
} | ||
} | ||
|
||
if isNew { | ||
cmd.Print(jobLog.Text) | ||
numberOfLinesOfJobLogText :=strings.Count(jobLog.Text,"\n") | ||
if isNew && (numberOfLines >0 || numberOfLines==-1){ | ||
if numberOfLines>=numberOfLinesOfJobLogText || numberOfLines==-1 { | ||
cmd.Print(jobLog.Text) | ||
numberOfLines -=numberOfLinesOfJobLogText | ||
|
||
}else if numberOfLines< numberOfLinesOfJobLogText { | ||
text :=jobLog.Text | ||
for i:=0;i<=numberOfLinesOfJobLogText-numberOfLines;i++ { | ||
temp := strings.Index(text,"\n") | ||
text = text[temp+1:] | ||
} | ||
cmd.Print(text) | ||
numberOfLines=0 | ||
} | ||
} | ||
|
||
if jobLog.HasMore { | ||
err = printLog(jclient, cmd, jobName, history, jobLog.NextStart) | ||
err = printLog(jclient, cmd, jobName, history, jobLog.NextStart,numberOfLines) | ||
} | ||
} | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first letter should be capital.