Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cmd/crie/conf/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ var LanguageList = []linter.Language{
Fmt: &cli.Lint{Bin: `autopep8`, FrontPar: cli.Par{`--in-place`, `--aggressive`, `--aggressive`}, Docker: cli.DockerCmd{Image: imgCriePip}},
Chk: &cli.Lint{Bin: `pylint`, Docker: cli.DockerCmd{Image: imgCriePip}},
},
{
Name: `pythondeps`,
Match: regexp.MustCompile(`requirements.txt$`),
Fmt: &cli.Lint{Bin: `pur`, FrontPar: cli.Par{`-r`}},
},
{
Name: `proto`,
Match: regexp.MustCompile(`.proto$`),
Expand Down Expand Up @@ -140,7 +135,7 @@ var LanguageList = []linter.Language{
{
Name: `ansible`,
Match: regexp.MustCompile(`playbook.yml$`),
//Chk: &imp.Lint{Bin: `ansible-lint`}
Chk: &cli.Lint{Bin: `ansible-lint`, Docker: cli.DockerCmd{Image: imgCriePip}},
},

{
Expand Down
2 changes: 1 addition & 1 deletion cmd/crie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var majorNum = "0"
var minorOffset = 0
var patchNum = "80"
var patchNum = "166"

//`
// |> crie: the act of crying and dying at the same time
Expand Down
1 change: 0 additions & 1 deletion img/pip/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ docker-compose==1.29.2
yamllint==1.26.3
pylint==2.12.2
autopep8==1.6.0
pur==6.0.1
ansible-lint==5.4.0
ansible==5.4.0
5 changes: 3 additions & 2 deletions pkg/crie/linter/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type Report struct {
type Linter interface {
Name() string
WillRun() error
DidRun()
Cleanup()
MaxConcurrency() int
Run(filepath string, rep chan Report)
WaitForCleanup() error
}

// Language is used to associate a file pattern to the relevant tools to check and format
Expand All @@ -33,7 +34,7 @@ type Language struct {

// GetLinter allows for string indexing to get fmt or chk...
// TODO remove requirement for this function
func (l Language) GetLinter(which string) Linter {
func (l *Language) GetLinter(which string) Linter {
if which == "fmt" {
return l.Fmt
} else if which == "chk" {
Expand Down
36 changes: 21 additions & 15 deletions pkg/crie/project/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func (s *LintConfiguration) NoStandards() {
table.Render()
}

func (s *LintConfiguration) tryLint(l linter.Language) error {
selectedLinter := l.GetLinter(s.lintType)
func (s *LintConfiguration) tryLint(l linter.Language) (selectedLinter linter.Linter, err error) {
selectedLinter = l.GetLinter(s.lintType)
toLog := log.WithFields(log.Fields{"lang": l.Name, "type": s.lintType})

if selectedLinter == nil {
toLog.Debug("there are no configurations associated for this action")
return nil
return
}

// Get the match for this formatter's files.
Expand All @@ -108,25 +108,19 @@ func (s *LintConfiguration) tryLint(l linter.Language) error {

// Skip language as no files found
if len(filteredFilepaths) == 0 {
return nil
return nil, err
}

defer selectedLinter.DidRun()

err := selectedLinter.WillRun()
defer func() { go selectedLinter.Cleanup() }()
err = selectedLinter.WillRun()
if err != nil {
toLog.Error(err.Error())
return err
return
}

toLog.WithFields(log.Fields{"files": len(filteredFilepaths)}).Info("running")
err = linter.LintFileList(selectedLinter, filteredFilepaths)
if err != nil {
toLog.Error(err.Error())
return err
}

return nil
return
}

// Run is the generic way to run everything based on the packages configuration
Expand All @@ -153,8 +147,20 @@ func (s *LintConfiguration) Run(lintType string) error {

// Run every linter.
for _, l := range currentLangs {
err := s.tryLint(l)
cleanupLinter, err := s.tryLint(l)

// If tryLint returns a non nil linter reference then we should call WaitForCleanup later
if cleanupLinter != nil {
defer func() {
err := cleanupLinter.WaitForCleanup()
if err != nil {
log.Error(err)
}
}()
}

if err != nil {
log.Error(err.Error())
errCount++
if !s.ContinueOnError {
break
Expand Down
39 changes: 33 additions & 6 deletions pkg/linter/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Lint struct {
EndPar Par
Docker DockerCmd
useDocker bool
cleanedUp chan error
}

// Par represents cli parameters
Expand All @@ -50,6 +51,13 @@ func (e *Lint) Name() string {
// WillRun does preflight checks for the 'Run'
func (e *Lint) WillRun() error {
e.useDocker = exec.Command("which", e.Bin).Run() != nil

// Ensure cleanup channel is created
if e.cleanedUp == nil {
e.cleanedUp = make(chan error)
}

// Startup sidecar container if needed
if e.useDocker {
if e.Docker.Image == "" {
return errors.New("could not find " + e.Bin + ", possibly not installed")
Expand All @@ -60,6 +68,7 @@ func (e *Lint) WillRun() error {
} else {
log.Debug("using local binary")
}

return nil
}

Expand Down Expand Up @@ -203,8 +212,14 @@ func (e *Lint) startDocker() error {
return e.Docker.client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
}

// DidRun should be called after all other Runs to clean up
func (e *Lint) DidRun() {
// Cleanup remove any additional resources created in the process
func (e *Lint) Cleanup() {
var err error = nil
defer func() {
e.cleanedUp <- err
close(e.cleanedUp)
}()

if e.useDocker && e.Docker.id != "" {
ctx := context.Background()
var timeout time.Duration
Expand All @@ -213,16 +228,28 @@ func (e *Lint) DidRun() {
d := log.WithFields(log.Fields{"dockerId": e.Docker.id})

d.Debug("stopping container")
if err := e.Docker.client.ContainerStop(ctx, e.Docker.id, &timeout); err != nil {
log.Error(err)
if err = e.Docker.client.ContainerStop(ctx, e.Docker.id, &timeout); err != nil {
return
}
d.Debug("removing container")
if err := e.Docker.client.ContainerRemove(ctx, e.Docker.id, types.ContainerRemoveOptions{}); err != nil {
log.Fatal(err)
if err = e.Docker.client.ContainerRemove(ctx, e.Docker.id, types.ContainerRemoveOptions{}); err != nil {
return
}
}
}

// WaitForCleanup Useful for when Cleanup is running in the background
func (e *Lint) WaitForCleanup() error {
var timeout time.Duration = 10

select {
case err := <-e.cleanedUp:
return err
case <-time.After(time.Second * timeout):
return fmt.Errorf("timeout waiting for cleanup for %s (%d seconds)", e.Name(), timeout)
}
}

// Run does the work required to lint the given filepath
func (e *Lint) Run(filepath string, rep chan linter.Report) {

Expand Down
11 changes: 8 additions & 3 deletions pkg/linter/shfmt/shfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ func (e *Lint) Name() string {

// WillRun do nothing as there are no external deps
func (e *Lint) WillRun() (err error) {
return
return nil
}

// DidRun cleanup funtion
func (e *Lint) DidRun() {
// Cleanup remove any additional resources created in the process
func (e *Lint) Cleanup() {
return
}

// WaitForCleanup Useful for when Cleanup is running in the background
func (e *Lint) WaitForCleanup() (err error) {
return nil
}

// MaxConcurrency return max amount of parallel files to fmt
func (e *Lint) MaxConcurrency() int {
return math.MaxInt32
Expand Down