github.com/pkumar631/talisman@v0.3.2/runner.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/thoughtworks/talisman/detector"
     8  	"github.com/thoughtworks/talisman/git_repo"
     9  )
    10  
    11  const (
    12  	//CompletedSuccessfully is an exit status that says that the current runners run completed without errors
    13  	CompletedSuccessfully int = 0
    14  
    15  	//CompletedWithErrors is an exit status that says that the current runners run completed with failures
    16  	CompletedWithErrors int = 1
    17  )
    18  
    19  //Runner represents a single run of the validations for a given commit range
    20  type Runner struct {
    21  	additions []git_repo.Addition
    22  	results   *detector.DetectionResults
    23  }
    24  
    25  //NewRunner returns a new Runner.
    26  func NewRunner(additions []git_repo.Addition) *Runner {
    27  	return &Runner{additions, detector.NewDetectionResults()}
    28  }
    29  
    30  //RunWithoutErrors will validate the commit range for errors and return either COMPLETED_SUCCESSFULLY or COMPLETED_WITH_ERRORS
    31  func (r *Runner) RunWithoutErrors() int {
    32  	r.doRun()
    33  	r.printReport()
    34  	return r.exitStatus()
    35  }
    36  
    37  func (r *Runner) doRun() {
    38  	ignores := detector.ReadIgnoresFromFile(readRepoFile())
    39  	detector.DefaultChain().Test(r.additions, ignores, r.results)
    40  }
    41  
    42  func (r *Runner) printReport() {
    43  	if r.results.HasIgnores() || r.results.HasFailures() {
    44  		fmt.Println(r.results.Report())
    45  	}
    46  }
    47  
    48  func (r *Runner) exitStatus() int {
    49  	if r.results.HasFailures() {
    50  		return CompletedWithErrors
    51  	}
    52  	return CompletedSuccessfully
    53  }
    54  
    55  func readRepoFile() func(string) ([]byte, error) {
    56  	wd, _ := os.Getwd()
    57  	repo := git_repo.RepoLocatedAt(wd)
    58  	return repo.ReadRepoFileOrNothing
    59  }