diff --git a/cmd/logsPipelineJob.go b/cmd/logsPipelineJob.go index 10172bf..1d15688 100644 --- a/cmd/logsPipelineJob.go +++ b/cmd/logsPipelineJob.go @@ -30,7 +30,16 @@ import ( "github.com/spf13/cobra" ) -var completedJobStatuses = []string{"Succeeded", "Failed", "Stopped"} +const ( + jobStatusRunning = "Running" + jobStatusFailed = "Failed" + jobStatusSucceeded = "Succeeded" + jobStatusStopped = "Stopped" + + stepStatusWaiting = "Waiting" +) + +var completedJobStatuses = []string{jobStatusSucceeded, jobStatusStopped, jobStatusFailed} // logsJobCmd represents the logsJobCmd command var logsJobCmd = &cobra.Command{ @@ -91,13 +100,16 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN if respJob == nil { continue } - if slices.Contains(completedJobStatuses, respJob.Payload.Status) { - log.Print(cmd, "radix-cli", fmt.Sprintf("Job is completed with status %s\n", respJob.Payload.Status), log.Red) - return nil + if isCompletedJob(respJob.Payload.Status) { + return errorAndLogCompletedJob(respJob.Payload.Status, cmd) } loggedForJob := false for i, step := range respJob.Payload.Steps { + if step.Status == stepStatusWaiting { + continue + } + // Sometimes, even though we get delta, the log is the same as previous previousLogLines := previousLogForStep[step.Name] stepLogsParams := pipeline_job.NewGetPipelineJobStepLogsParams() @@ -107,7 +119,7 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN jobStepLog, err := apiClient.PipelineJob.GetPipelineJobStepLogs(stepLogsParams, nil) if err != nil { - log.Print(cmd, "radix-cli", fmt.Sprintf("Failed to jet pipeline job logs. %v", err), log.Red) + log.Print(cmd, "radix-cli", fmt.Sprintf("Failed to get pipeline job logs. %v", err), log.Red) break } logLines := strings.Split(strings.Replace(jobStepLog.Payload, "\r\n", "\n", -1), "\n") @@ -131,17 +143,8 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN continue } jobSummary := respJob.Payload - if jobSummary.Status == "Succeeded" { - log.Print(cmd, "radix-cli", "Job complete", log.Green) - return nil - } - if jobSummary.Status == "Failed" { - log.Print(cmd, "radix-cli", "Job failed", log.Red) - return nil - } - if jobSummary.Status == "Stopped" { - log.Print(cmd, "radix-cli", "Job stopped", log.Red) - return nil + if isCompletedJob(jobSummary.Status) { + return errorAndLogCompletedJob(jobSummary.Status, cmd) } if jobSummary.Status == "Running" { // Reset timeout @@ -160,6 +163,20 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN } } +func isCompletedJob(status string) bool { + return slices.Contains(completedJobStatuses, status) +} + +func errorAndLogCompletedJob(status string, cmd *cobra.Command) error { + msg := fmt.Sprintf("job completed with status %s", status) + if status == jobStatusFailed { + fmt.Fprintln(cmd.OutOrStdout()) + return errors.New(msg) + } + log.Print(cmd, "radix-cli", msg, log.Red) + return nil +} + func init() { logsCmd.AddCommand(logsJobCmd) diff --git a/pkg/utils/log/log_util.go b/pkg/utils/log/log_util.go index f5934bf..0043401 100644 --- a/pkg/utils/log/log_util.go +++ b/pkg/utils/log/log_util.go @@ -9,12 +9,12 @@ import ( ) var ( - Yellow = color.New(color.FgHiYellow, color.BgBlack, color.Bold).SprintFunc() - Green = color.New(color.FgHiGreen, color.BgBlack, color.Bold).SprintFunc() - Blue = color.New(color.FgHiBlue, color.BgBlack, color.Underline).SprintFunc() + Yellow = color.New(color.FgHiYellow, color.BgBlack).SprintFunc() + Green = color.New(color.FgHiGreen, color.BgBlack).SprintFunc() + Blue = color.New(color.FgHiBlue, color.BgBlack).SprintFunc() Cyan = color.New(color.FgCyan, color.BgBlack).SprintFunc() - Red = color.New(color.FgHiRed, color.BgBlack).Add(color.Italic).SprintFunc() - Magenta = color.New(color.FgHiMagenta, color.BgBlack).Add(color.Italic).SprintFunc() + Red = color.New(color.FgHiRed, color.BgBlack).SprintFunc() + Magenta = color.New(color.FgHiMagenta, color.BgBlack).SprintFunc() Colors = []func(a ...interface{}) string{Yellow, Green, Blue, Cyan, Red, Magenta} )