forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.go
79 lines (58 loc) · 1.48 KB
/
custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package commands
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/golangci/golangci-lint/v2/pkg/commands/internal"
"github.com/golangci/golangci-lint/v2/pkg/logutils"
)
const envKeepTempFiles = "CUSTOM_GCL_KEEP_TEMP_FILES"
type customCommand struct {
cmd *cobra.Command
cfg *internal.Configuration
log logutils.Log
}
func newCustomCommand(logger logutils.Log) *customCommand {
c := &customCommand{log: logger}
customCmd := &cobra.Command{
Use: "custom",
Short: "Build a version of golangci-lint with custom linters",
Args: cobra.NoArgs,
PreRunE: c.preRunE,
RunE: c.runE,
SilenceUsage: true,
}
c.cmd = customCmd
return c
}
func (c *customCommand) preRunE(_ *cobra.Command, _ []string) error {
cfg, err := internal.LoadConfiguration()
if err != nil {
return err
}
err = cfg.Validate()
if err != nil {
return err
}
c.cfg = cfg
return nil
}
func (c *customCommand) runE(cmd *cobra.Command, _ []string) error {
tmp, err := os.MkdirTemp(os.TempDir(), "custom-gcl")
if err != nil {
return fmt.Errorf("create temporary directory: %w", err)
}
defer func() {
if os.Getenv(envKeepTempFiles) != "" {
log.Printf("WARN: The env var %s has been detected: the temporary directory is preserved: %s", envKeepTempFiles, tmp)
return
}
_ = os.RemoveAll(tmp)
}()
err = internal.NewBuilder(c.log, c.cfg, tmp).Build(cmd.Context())
if err != nil {
return fmt.Errorf("build process: %w", err)
}
return nil
}