dev: new commands system ()

This commit is contained in:
Ludovic Fernandez 2024-02-27 00:03:48 +01:00 committed by GitHub
parent b5d7302867
commit 784264d72e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 1189 additions and 1153 deletions
docs/src/docs/contributing

View file

@ -22,90 +22,6 @@ graph LR
</ResponsiveContainer>
## Init
The execution starts here:
```go title=cmd/golangci-lint/main.go
func main() {
e := commands.NewExecutor(info)
if err := e.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)
os.Exit(exitcodes.Failure)
}
}
```
The **executer** is our abstraction:
```go title=pkg/commands/executor.go
type Executor struct {
rootCmd *cobra.Command
runCmd *cobra.Command
lintersCmd *cobra.Command
exitCode int
buildInfo BuildInfo
cfg *config.Config
log logutils.Log
reportData report.Data
DBManager *lintersdb.Manager
EnabledLintersSet *lintersdb.EnabledSet
contextLoader *lint.ContextLoader
goenv *goutil.Env
fileCache *fsutils.FileCache
lineCache *fsutils.LineCache
pkgCache *pkgcache.Cache
debugf logutils.DebugFunc
sw *timeutils.Stopwatch
loadGuard *load.Guard
flock *flock.Flock
}
```
We use dependency injection and all root dependencies are stored in this executor.
In the function `NewExecutor` we do the following:
1. Initialize dependencies.
2. Initialize [cobra](https://github.com/spf13/cobra) commands.
3. Parse the config file using [viper](https://github.com/spf13/viper) and merge it with command line arguments.
The following execution is controlled by `cobra`. If a user executes `golangci-lint run`
then `cobra` executes `e.runCmd`.
Different `cobra` commands have different runners, e.g. a `run` command is configured in the following way:
```go title=pkg/commands/run.go
func (e *Executor) initRun() {
e.runCmd = &cobra.Command{
Use: "run",
Short: "Run the linters",
Run: e.executeRun,
PreRunE: func(_ *cobra.Command, _ []string) error {
if ok := e.acquireFileLock(); !ok {
return errors.New("parallel golangci-lint is running")
}
return nil
},
PostRun: func(_ *cobra.Command, _ []string) {
e.releaseFileLock()
},
}
e.rootCmd.AddCommand(e.runCmd)
e.runCmd.SetOut(logutils.StdOut) // use custom output to properly color it in Windows terminals
e.runCmd.SetErr(logutils.StdErr)
e.initRunConfiguration(e.runCmd)
}
```
The primary execution function of the `run` command is `executeRun`.
## Load Packages
Loading packages is listing all packages and their recursive dependencies for analysis.