github.com/nilium/gitlab-runner@v12.5.0+incompatible/helpers/build_section.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  type RawLogger interface {
     9  	SendRawLog(args ...interface{})
    10  }
    11  
    12  type BuildSection struct {
    13  	Name        string
    14  	SkipMetrics bool
    15  	Run         func() error
    16  }
    17  
    18  const (
    19  	traceSectionStart = "section_start:%v:%s\r" + ANSI_CLEAR
    20  	traceSectionEnd   = "section_end:%v:%s\r" + ANSI_CLEAR
    21  )
    22  
    23  func nowUnixUTC() int64 {
    24  	return time.Now().UTC().Unix()
    25  }
    26  
    27  func (s *BuildSection) timestamp(format string, logger RawLogger) {
    28  	if s.SkipMetrics {
    29  		return
    30  	}
    31  
    32  	sectionLine := fmt.Sprintf(format, nowUnixUTC(), s.Name)
    33  	logger.SendRawLog(sectionLine)
    34  }
    35  
    36  func (s *BuildSection) start(logger RawLogger) {
    37  	s.timestamp(traceSectionStart, logger)
    38  }
    39  
    40  func (s *BuildSection) end(logger RawLogger) {
    41  	s.timestamp(traceSectionEnd, logger)
    42  }
    43  
    44  func (s *BuildSection) Execute(logger RawLogger) error {
    45  	s.start(logger)
    46  	defer s.end(logger)
    47  
    48  	return s.Run()
    49  }