Skip to content

dev: clean up Executor #4404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 45 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
126988e
chore: remove deprecated flags
ldez Feb 18, 2024
d0eb53b
chore: re-organize methods and functions
ldez Feb 18, 2024
8db073c
chore: clean dead code
ldez Feb 18, 2024
62dd1a8
chore: re-organize comments related to cache
ldez Feb 18, 2024
1e2ba1f
chore: unify fonction naming
ldez Feb 18, 2024
865d6ea
chore: move BuildInfo to version
ldez Feb 18, 2024
d917656
fix: reports default flag values into defaultLintersSettings
ldez Feb 19, 2024
b7b86a6
chore: reformat some comments
ldez Feb 20, 2024
14bc543
chore(presets): converts methods to functions
ldez Feb 20, 2024
86be410
chore: same receiver for all Manager methods
ldez Feb 20, 2024
aaa99dc
chore: unexports some fields
ldez Feb 20, 2024
6076083
chore: move getConfigForCommandLine to executor file
ldez Feb 20, 2024
eaecfbd
chore: converts FlagSet methods to functions
ldez Feb 20, 2024
0bfebae
chore: linarize initVersionConfiguration
ldez Feb 20, 2024
4157048
chore: reformat some comments
ldez Feb 20, 2024
d1d6731
chore: isolate initHashSalt
ldez Feb 20, 2024
d938dae
chore: isolate init methods
ldez Feb 20, 2024
f8ff68d
chore: remove first call to lintersdb.NewManager from NewExecutor
ldez Feb 20, 2024
2b271d2
chore: move initCommands at the begining of NewExecutor
ldez Feb 20, 2024
34269cb
chore: isolate Executor initialization code
ldez Feb 20, 2024
ea79bbc
chore: fix imports
ldez Feb 20, 2024
fae562c
chore: reorganize initRunFlagSet
ldez Feb 20, 2024
7cc7c45
chore: remove useless needVersionOption method
ldez Feb 20, 2024
dfa6eb8
chore: inline help linters command
ldez Feb 20, 2024
13682c2
chore: reorganize initXX methods
ldez Feb 20, 2024
9b6eabd
chore: reorganize command methods
ldez Feb 20, 2024
283946f
chore: documment cmd fields
ldez Feb 20, 2024
1e8a199
chore: remove loadGuard field
ldez Feb 20, 2024
34572a8
chore: group executor fields
ldez Feb 20, 2024
18935d2
chore: clean goutil.Env
ldez Feb 20, 2024
b5b2a79
chore: remove sw and pkgCache fields
ldez Feb 20, 2024
f470c2c
chore: fixSlicesFlags before NewManager
ldez Feb 20, 2024
e7a19c7
chore: extract initConfiguration from initExecutor
ldez Feb 20, 2024
d995415
chore: remove Deadline tests
ldez Feb 20, 2024
9159274
chore: converts initHashSalt from methods to function
ldez Feb 20, 2024
51c1ee6
chore: removes useless flags from config command
ldez Feb 20, 2024
7a0bc08
chore: fix typos
ldez Feb 20, 2024
074c369
chore(offtopic): rename a variable inside NewGochecknoglobals
ldez Feb 20, 2024
3e97d56
chore(offtopic): extract govet validation from FileReader
ldez Feb 20, 2024
3b27685
chore: isolate Version field in Config
ldez Feb 20, 2024
c1d711d
chore: use errors.As
ldez Feb 20, 2024
858a41c
chore: annotate and re-organize configuration structures
ldez Feb 21, 2024
bfccfbd
chore: fix typo
ldez Feb 21, 2024
33416b2
fix: govet settings validation
ldez Feb 21, 2024
a3618d7
chore: remove allPresets function
ldez Feb 21, 2024
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
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ issues:
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
- path: pkg/commands/run.go
text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead"
- path: pkg/commands/run.go
text: "SA1019: e.cfg.Run.Deadline is deprecated: Deadline exists for historical compatibility and should not be used."

- path: pkg/golinters/gofumpt.go
text: "SA1019: settings.LangVersion is deprecated: use the global `run.go` instead."
Expand Down
78 changes: 75 additions & 3 deletions pkg/commands/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package commands

import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/golangci/golangci-lint/internal/cache"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
)
Expand All @@ -21,14 +27,13 @@ func (e *Executor) initCache() {
return cmd.Help()
},
}
e.rootCmd.AddCommand(cacheCmd)

cacheCmd.AddCommand(&cobra.Command{
Use: "clean",
Short: "Clean cache",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: e.executeCleanCache,
RunE: e.executeCacheClean,
})
cacheCmd.AddCommand(&cobra.Command{
Use: "status",
Expand All @@ -39,9 +44,11 @@ func (e *Executor) initCache() {
})

// TODO: add trim command?

e.rootCmd.AddCommand(cacheCmd)
}

func (e *Executor) executeCleanCache(_ *cobra.Command, _ []string) error {
func (e *Executor) executeCacheClean(_ *cobra.Command, _ []string) error {
cacheDir := cache.DefaultDir()
if err := os.RemoveAll(cacheDir); err != nil {
return fmt.Errorf("failed to remove dir %s: %w", cacheDir, err)
Expand Down Expand Up @@ -70,3 +77,68 @@ func dirSizeBytes(path string) (int64, error) {
})
return size, err
}

// --- Related to cache but not used directly by the cache command.

func initHashSalt(version string, cfg *config.Config) error {
binSalt, err := computeBinarySalt(version)
if err != nil {
return fmt.Errorf("failed to calculate binary salt: %w", err)
}

configSalt, err := computeConfigSalt(cfg)
if err != nil {
return fmt.Errorf("failed to calculate config salt: %w", err)
}

b := bytes.NewBuffer(binSalt)
b.Write(configSalt)
cache.SetSalt(b.Bytes())
return nil
}

func computeBinarySalt(version string) ([]byte, error) {
if version != "" && version != "(devel)" {
return []byte(version), nil
}

if logutils.HaveDebugTag(logutils.DebugKeyBinSalt) {
return []byte("debug"), nil
}

p, err := os.Executable()
if err != nil {
return nil, err
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return nil, err
}
return h.Sum(nil), nil
}

// computeConfigSalt computes configuration hash.
// We don't hash all config fields to reduce meaningless cache invalidations.
// At least, it has a huge impact on tests speed.
// Fields: `LintersSettings` and `Run.BuildTags`.
func computeConfigSalt(cfg *config.Config) ([]byte, error) {
lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings)
if err != nil {
return nil, fmt.Errorf("failed to json marshal config linter settings: %w", err)
}

configData := bytes.NewBufferString("linters-settings=")
configData.Write(lintersSettingsBytes)
configData.WriteString("\nbuild-tags=%s" + strings.Join(cfg.Run.BuildTags, ","))

h := sha256.New()
if _, err := h.Write(configData.Bytes()); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
32 changes: 16 additions & 16 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,42 @@ import (
)

func (e *Executor) initConfig() {
cmd := &cobra.Command{
configCmd := &cobra.Command{
Use: "config",
Short: "Config file information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}
e.rootCmd.AddCommand(cmd)

pathCmd := &cobra.Command{
Use: "path",
Short: "Print used config path",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: e.executePathCmd,
Run: e.executePath,
}

fs := pathCmd.Flags()
fs.SortFlags = false // sort them as they are defined here

initConfigFileFlagSet(fs, &e.cfg.Run)
configCmd.AddCommand(pathCmd)
e.rootCmd.AddCommand(configCmd)
}

func (e *Executor) executePath(_ *cobra.Command, _ []string) {
usedConfigFile := e.getUsedConfig()
if usedConfigFile == "" {
e.log.Warnf("No config file detected")
os.Exit(exitcodes.NoConfigFileDetected)
}

cmd.AddCommand(pathCmd)
fmt.Println(usedConfigFile)
}

// getUsedConfig returns the resolved path to the golangci config file, or the empty string
// if no configuration could be found.
// getUsedConfig returns the resolved path to the golangci config file,
// or the empty string if no configuration could be found.
func (e *Executor) getUsedConfig() string {
usedConfigFile := viper.ConfigFileUsed()
if usedConfigFile == "" {
Expand All @@ -57,15 +65,7 @@ func (e *Executor) getUsedConfig() string {
return prettyUsedConfigFile
}

func (e *Executor) executePathCmd(_ *cobra.Command, _ []string) {
usedConfigFile := e.getUsedConfig()
if usedConfigFile == "" {
e.log.Warnf("No config file detected")
os.Exit(exitcodes.NoConfigFileDetected)
}

fmt.Println(usedConfigFile)
}
// --- Related to config but not used directly by the config command.

func initConfigFileFlagSet(fs *pflag.FlagSet, cfg *config.Run) {
fs.StringVarP(&cfg.Config, "config", "c", "", wh("Read config from file path `PATH`"))
Expand Down
Loading