Example #1
0
func Render(dst io.Writer, src eventstream.EventStream) int {
	var buildConfig event.TaskConfig

	exitStatus := 0

	for {
		ev, err := src.NextEvent()
		if err != nil {
			if err == io.EOF {
				return exitStatus
			} else {
				fmt.Fprintf(dst, "failed to parse next event: %s\n", err)
				return 255
			}
		}

		switch e := ev.(type) {
		case event.Log:
			fmt.Fprintf(dst, "%s", e.Payload)

		case event.InitializeTask:
			buildConfig = e.TaskConfig

			if buildConfig.Image != "" {
				fmt.Fprintf(dst, "\x1b[1minitializing with %s\x1b[0m\n", buildConfig.Image)
			} else {
				fmt.Fprintf(dst, "\x1b[1minitializing\x1b[0m\n")
			}

		case event.StartTask:
			argv := strings.Join(append([]string{buildConfig.Run.Path}, buildConfig.Run.Args...), " ")
			fmt.Fprintf(dst, "\x1b[1mrunning %s\x1b[0m\n", argv)

		case event.FinishTask:
			exitStatus = e.ExitStatus

		case event.Error:
			errCol := ui.ErroredColor.SprintFunc()
			fmt.Fprintf(dst, "%s\n", errCol(e.Message))

		case event.Status:
			var printColor *color.Color

			switch e.Status {
			case "started":
				continue
			case "succeeded":
				printColor = ui.SucceededColor
			case "failed":
				printColor = ui.FailedColor

				if exitStatus == 0 {
					exitStatus = 1
				}
			case "errored":
				printColor = ui.ErroredColor

				if exitStatus == 0 {
					exitStatus = 2
				}
			case "aborted":
				printColor = ui.AbortedColor

				if exitStatus == 0 {
					exitStatus = 3
				}
			default:
				fmt.Fprintf(dst, "unknown status: %s", e.Status)
				return 255
			}

			printColorFunc := printColor.SprintFunc()
			fmt.Fprintf(dst, "%s\n", printColorFunc(e.Status))

			return exitStatus
		}
	}

	return 255
}
Example #2
0
func newDesc(s string, c *color.Color) desc {
	return desc{s, c.SprintFunc()}
}
Example #3
0
// getPrefix generates the log prefix in the given color
func getPrefix(level string, color *color.Color) string {
	currentTime := time.Now().Format("15:04:05")
	toColoredString := color.SprintFunc()
	return toColoredString(fmt.Sprintf("%s[%s] ", level, currentTime))
}