Skip to content

Commit

Permalink
feat: Timeout (#34)
Browse files Browse the repository at this point in the history
* timeout

* update help

* remove temp file
  • Loading branch information
notJoon authored Jul 27, 2024
1 parent 34aea91 commit 9d144bd
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cmd/tlin/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"

"github.com/gnoswap-labs/lint/formatter"
"github.com/gnoswap-labs/lint/internal"
"github.com/gnoswap-labs/lint/internal/lints"
tt "github.com/gnoswap-labs/lint/internal/types"
)

const defaultTimeout = 5 * time.Minute

func main() {
timeout := flag.Duration("timeout", defaultTimeout, "Set a timeout for the linter. example: 1s, 1m, 1h")
// verbose := flag.Bool("verbose", false, "Enable verbose output")
// formatJSON := flag.Bool("json", false, "Output results in JSON format")
cyclomaticComplexity := flag.Bool("cyclo", false, "Run cyclomatic complexity analysis")
Expand Down Expand Up @@ -45,6 +50,9 @@ func main() {
os.Exit(1)
}

ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()

if *ignoreRules != "" {
rules := strings.Split(*ignoreRules, ",")
for _, rule := range rules {
Expand All @@ -53,9 +61,29 @@ func main() {
}

if *cyclomaticComplexity {
runCyclomaticComplexityAnalysis(args, *cyclomaticThreshold)
runWithTimeout(ctx, func() {
runCyclomaticComplexityAnalysis(args, *cyclomaticThreshold)
})
} else {
runNormalLintProcess(engine, args)
runWithTimeout(ctx, func() {
runNormalLintProcess(engine, args)
})
}
}

func runWithTimeout(ctx context.Context, f func()) {
done := make(chan struct{})
go func() {
f()
close(done)
}()

select {
case <-ctx.Done():
fmt.Println("Linter timed out")
os.Exit(1)
case <-done:
return
}
}

Expand Down

0 comments on commit 9d144bd

Please sign in to comment.