build_section.go 875 Bytes
Newer Older
Alessio Caiazza's avatar
Alessio Caiazza committed
1
package helpers
2 3 4 5 6 7

import (
	"fmt"
	"time"
)

8 9
type RawLogger interface {
	SendRawLog(args ...interface{})
10 11 12
}

type BuildSection struct {
13 14 15
	Name        string
	SkipMetrics bool
	Run         func() error
16 17 18
}

const (
19 20
	traceSectionStart = "section_start:%v:%s\r" + ANSI_CLEAR
	traceSectionEnd   = "section_end:%v:%s\r" + ANSI_CLEAR
21 22 23 24 25 26
)

func nowUnixUTC() int64 {
	return time.Now().UTC().Unix()
}

27
func (s *BuildSection) timestamp(format string, logger RawLogger) {
28 29 30 31
	if s.SkipMetrics {
		return
	}

Alessio Caiazza's avatar
Alessio Caiazza committed
32
	sectionLine := fmt.Sprintf(format, nowUnixUTC(), s.Name)
33
	logger.SendRawLog(sectionLine)
Alessio Caiazza's avatar
Alessio Caiazza committed
34
}
35

36
func (s *BuildSection) start(logger RawLogger) {
Alessio Caiazza's avatar
Alessio Caiazza committed
37 38
	s.timestamp(traceSectionStart, logger)
}
39

40
func (s *BuildSection) end(logger RawLogger) {
Alessio Caiazza's avatar
Alessio Caiazza committed
41 42 43
	s.timestamp(traceSectionEnd, logger)
}

44
func (s *BuildSection) Execute(logger RawLogger) error {
Alessio Caiazza's avatar
Alessio Caiazza committed
45 46
	s.start(logger)
	defer s.end(logger)
47

Alessio Caiazza's avatar
Alessio Caiazza committed
48
	return s.Run()
49
}