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

     1  package detector
     2  
     3  import (
     4  	"fmt"
     5  
     6  	log "github.com/Sirupsen/logrus"
     7  	"github.com/thoughtworks/talisman/git_repo"
     8  )
     9  
    10  type FileSizeDetector struct {
    11  	size int
    12  }
    13  
    14  func DefaultFileSizeDetector() Detector {
    15  	return NewFileSizeDetector(1 * 1024 * 1024)
    16  }
    17  
    18  func NewFileSizeDetector(size int) Detector {
    19  	return FileSizeDetector{size}
    20  }
    21  
    22  func (fd FileSizeDetector) Test(additions []git_repo.Addition, ignores Ignores, result *DetectionResults) {
    23  	for _, addition := range additions {
    24  		if ignores.Deny(addition) {
    25  			log.WithFields(log.Fields{
    26  				"filePath": addition.Path,
    27  			}).Info("Ignoring addition as it was specified to be ignored.")
    28  			result.Ignore(addition.Path, fmt.Sprintf("%s was ignored by .talismanignore", addition.Path))
    29  			continue
    30  		}
    31  		size := len(addition.Data)
    32  		if size > fd.size {
    33  			log.WithFields(log.Fields{
    34  				"filePath": addition.Path,
    35  				"fileSize": size,
    36  				"maxSize":  fd.size,
    37  			}).Info("Failing file as it is larger than max allowed file size.")
    38  			result.Fail(addition.Path, fmt.Sprintf("The file name %q with file size %d is larger than max allowed file size(%d)", addition.Path, size, fd.size))
    39  		}
    40  	}
    41  }