Skip to content

Commit 9f4b51e

Browse files
sybrenstuveldominikh
authored andcommitted
lintcmd: allow use from library, without forcing a call to os.Exit
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]
1 parent ddff81c commit 9f4b51e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Diff for: lintcmd/cmd.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ func decodeGob(br io.ByteReader) ([]run, error) {
271271
return runs, nil
272272
}
273273

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

335-
// Exit with appropriate status
336-
os.Exit(exit)
335+
return exit
336+
}
337+
338+
// Run runs all registered analyzers and reports their findings.
339+
// It always calls os.Exit and does not return.
340+
func (cmd *Command) Run() {
341+
os.Exit(cmd.Execute())
337342
}
338343

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

0 commit comments

Comments
 (0)