Skip to content

Commit 8c51979

Browse files
authored
dev: clean up Executor (#4404)
1 parent 4068bb7 commit 8c51979

21 files changed

+692
-785
lines changed

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ issues:
141141
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
142142
- path: pkg/commands/run.go
143143
text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead"
144-
- path: pkg/commands/run.go
145-
text: "SA1019: e.cfg.Run.Deadline is deprecated: Deadline exists for historical compatibility and should not be used."
146144

147145
- path: pkg/golinters/gofumpt.go
148146
text: "SA1019: settings.LangVersion is deprecated: use the global `run.go` instead."

pkg/commands/cache.go

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package commands
22

33
import (
4+
"bytes"
5+
"crypto/sha256"
46
"fmt"
7+
"io"
58
"os"
69
"path/filepath"
10+
"strings"
711

812
"github.com/spf13/cobra"
13+
"gopkg.in/yaml.v3"
914

1015
"github.com/golangci/golangci-lint/internal/cache"
16+
"github.com/golangci/golangci-lint/pkg/config"
1117
"github.com/golangci/golangci-lint/pkg/fsutils"
1218
"github.com/golangci/golangci-lint/pkg/logutils"
1319
)
@@ -21,14 +27,13 @@ func (e *Executor) initCache() {
2127
return cmd.Help()
2228
},
2329
}
24-
e.rootCmd.AddCommand(cacheCmd)
2530

2631
cacheCmd.AddCommand(&cobra.Command{
2732
Use: "clean",
2833
Short: "Clean cache",
2934
Args: cobra.NoArgs,
3035
ValidArgsFunction: cobra.NoFileCompletions,
31-
RunE: e.executeCleanCache,
36+
RunE: e.executeCacheClean,
3237
})
3338
cacheCmd.AddCommand(&cobra.Command{
3439
Use: "status",
@@ -39,9 +44,11 @@ func (e *Executor) initCache() {
3944
})
4045

4146
// TODO: add trim command?
47+
48+
e.rootCmd.AddCommand(cacheCmd)
4249
}
4350

44-
func (e *Executor) executeCleanCache(_ *cobra.Command, _ []string) error {
51+
func (e *Executor) executeCacheClean(_ *cobra.Command, _ []string) error {
4552
cacheDir := cache.DefaultDir()
4653
if err := os.RemoveAll(cacheDir); err != nil {
4754
return fmt.Errorf("failed to remove dir %s: %w", cacheDir, err)
@@ -70,3 +77,68 @@ func dirSizeBytes(path string) (int64, error) {
7077
})
7178
return size, err
7279
}
80+
81+
// --- Related to cache but not used directly by the cache command.
82+
83+
func initHashSalt(version string, cfg *config.Config) error {
84+
binSalt, err := computeBinarySalt(version)
85+
if err != nil {
86+
return fmt.Errorf("failed to calculate binary salt: %w", err)
87+
}
88+
89+
configSalt, err := computeConfigSalt(cfg)
90+
if err != nil {
91+
return fmt.Errorf("failed to calculate config salt: %w", err)
92+
}
93+
94+
b := bytes.NewBuffer(binSalt)
95+
b.Write(configSalt)
96+
cache.SetSalt(b.Bytes())
97+
return nil
98+
}
99+
100+
func computeBinarySalt(version string) ([]byte, error) {
101+
if version != "" && version != "(devel)" {
102+
return []byte(version), nil
103+
}
104+
105+
if logutils.HaveDebugTag(logutils.DebugKeyBinSalt) {
106+
return []byte("debug"), nil
107+
}
108+
109+
p, err := os.Executable()
110+
if err != nil {
111+
return nil, err
112+
}
113+
f, err := os.Open(p)
114+
if err != nil {
115+
return nil, err
116+
}
117+
defer f.Close()
118+
h := sha256.New()
119+
if _, err := io.Copy(h, f); err != nil {
120+
return nil, err
121+
}
122+
return h.Sum(nil), nil
123+
}
124+
125+
// computeConfigSalt computes configuration hash.
126+
// We don't hash all config fields to reduce meaningless cache invalidations.
127+
// At least, it has a huge impact on tests speed.
128+
// Fields: `LintersSettings` and `Run.BuildTags`.
129+
func computeConfigSalt(cfg *config.Config) ([]byte, error) {
130+
lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings)
131+
if err != nil {
132+
return nil, fmt.Errorf("failed to json marshal config linter settings: %w", err)
133+
}
134+
135+
configData := bytes.NewBufferString("linters-settings=")
136+
configData.Write(lintersSettingsBytes)
137+
configData.WriteString("\nbuild-tags=%s" + strings.Join(cfg.Run.BuildTags, ","))
138+
139+
h := sha256.New()
140+
if _, err := h.Write(configData.Bytes()); err != nil {
141+
return nil, err
142+
}
143+
return h.Sum(nil), nil
144+
}

pkg/commands/config.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,42 @@ import (
1414
)
1515

1616
func (e *Executor) initConfig() {
17-
cmd := &cobra.Command{
17+
configCmd := &cobra.Command{
1818
Use: "config",
1919
Short: "Config file information",
2020
Args: cobra.NoArgs,
2121
RunE: func(cmd *cobra.Command, _ []string) error {
2222
return cmd.Help()
2323
},
2424
}
25-
e.rootCmd.AddCommand(cmd)
2625

2726
pathCmd := &cobra.Command{
2827
Use: "path",
2928
Short: "Print used config path",
3029
Args: cobra.NoArgs,
3130
ValidArgsFunction: cobra.NoFileCompletions,
32-
Run: e.executePathCmd,
31+
Run: e.executePath,
3332
}
3433

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

38-
initConfigFileFlagSet(fs, &e.cfg.Run)
37+
configCmd.AddCommand(pathCmd)
38+
e.rootCmd.AddCommand(configCmd)
39+
}
40+
41+
func (e *Executor) executePath(_ *cobra.Command, _ []string) {
42+
usedConfigFile := e.getUsedConfig()
43+
if usedConfigFile == "" {
44+
e.log.Warnf("No config file detected")
45+
os.Exit(exitcodes.NoConfigFileDetected)
46+
}
3947

40-
cmd.AddCommand(pathCmd)
48+
fmt.Println(usedConfigFile)
4149
}
4250

43-
// getUsedConfig returns the resolved path to the golangci config file, or the empty string
44-
// if no configuration could be found.
51+
// getUsedConfig returns the resolved path to the golangci config file,
52+
// or the empty string if no configuration could be found.
4553
func (e *Executor) getUsedConfig() string {
4654
usedConfigFile := viper.ConfigFileUsed()
4755
if usedConfigFile == "" {
@@ -57,15 +65,7 @@ func (e *Executor) getUsedConfig() string {
5765
return prettyUsedConfigFile
5866
}
5967

60-
func (e *Executor) executePathCmd(_ *cobra.Command, _ []string) {
61-
usedConfigFile := e.getUsedConfig()
62-
if usedConfigFile == "" {
63-
e.log.Warnf("No config file detected")
64-
os.Exit(exitcodes.NoConfigFileDetected)
65-
}
66-
67-
fmt.Println(usedConfigFile)
68-
}
68+
// --- Related to config but not used directly by the config command.
6969

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

0 commit comments

Comments
 (0)