Skip to content

Commit

Permalink
lintcmd: allow use from library, without forcing a call to os.Exit
Browse files Browse the repository at this point in the history
Split Command.Run into Command.Execute and Command.Run, where the former
is responsible for running analyzers and reporting their results and the
latter for calling os.Exit with the former's result. This allows running
the Staticcheck CLI programmatically together with other tools.

Closes: gh-1597 [via git-merge-pr]
  • Loading branch information
sybrenstuvel authored and dominikh committed Sep 20, 2024
1 parent ddff81c commit 9f4b51e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lintcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ func decodeGob(br io.ByteReader) ([]run, error) {
return runs, nil
}

// Run runs all registered analyzers and reports their findings.
// It always calls os.Exit and does not return.
func (cmd *Command) Run() {
// Execute runs all registered analyzers and reports their findings.
// The status code returned can be used for os.Exit(cmd.Execute()).
func (cmd *Command) Execute() int {
// Set up profiling and tracing
if path := cmd.flags.debugCpuprofile; path != "" {
f, err := os.Create(path)
Expand Down Expand Up @@ -332,8 +332,13 @@ func (cmd *Command) Run() {
trace.Stop()
}

// Exit with appropriate status
os.Exit(exit)
return exit
}

// Run runs all registered analyzers and reports their findings.
// It always calls os.Exit and does not return.
func (cmd *Command) Run() {
os.Exit(cmd.Execute())
}

func (cmd *Command) analyzersAsSlice() []*lint.Analyzer {
Expand Down

0 comments on commit 9f4b51e

Please sign in to comment.