-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli.go
More file actions
199 lines (161 loc) · 4.9 KB
/
cli.go
File metadata and controls
199 lines (161 loc) · 4.9 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cli
import (
"encoding/xml"
"errors"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/phayes/checkstyle"
"github.com/ryancurrah/gomodguard"
"github.com/ryancurrah/gomodguard/internal/filesearch"
"gopkg.in/yaml.v3"
)
const (
errFindingHomedir = "unable to find home directory, %w"
errReadingConfigFile = "could not read config file: %w"
errParsingConfigFile = "could not parse config file: %w"
)
var (
configFile = ".gomodguard.yaml"
logger = log.New(os.Stderr, "", 0)
errFindingConfigFile = errors.New("could not find config file")
)
// Run the gomodguard linter. Returns the exit code to use.
//
//nolint:funlen
func Run() int {
var (
args []string
help bool
noTest bool
report string
reportFile string
issuesExitCode int
cwd, _ = os.Getwd()
)
flag.BoolVar(&help, "h", false, "Show this help text")
flag.BoolVar(&help, "help", false, "")
flag.BoolVar(&noTest, "n", false, "Don't lint test files")
flag.BoolVar(&noTest, "no-test", false, "")
flag.StringVar(&report, "r", "", "Report results to one of the following formats: checkstyle. "+
"A report file destination must also be specified")
flag.StringVar(&report, "report", "", "")
flag.StringVar(&reportFile, "f", "", "Report results to the specified file. A report type must also be specified")
flag.StringVar(&reportFile, "file", "", "")
flag.IntVar(&issuesExitCode, "i", 2, "Exit code when issues were found")
flag.IntVar(&issuesExitCode, "issues-exit-code", 2, "")
flag.Parse()
report = strings.TrimSpace(strings.ToLower(report))
if help {
showHelp()
return 0
}
if report != "" && report != "checkstyle" {
logger.Fatalf("error: invalid report type '%s'", report)
}
if report != "" && reportFile == "" {
logger.Fatalf("error: a report file must be specified when a report is enabled")
}
if report == "" && reportFile != "" {
logger.Fatalf("error: a report type must be specified when a report file is enabled")
}
args = flag.Args()
if len(args) == 0 {
args = []string{"./..."}
}
config, err := GetConfig(configFile)
if err != nil {
logger.Fatalf("error: %s", err)
}
filteredFiles := filesearch.Find(cwd, noTest, args)
processor, err := gomodguard.NewProcessor(config)
if err != nil {
logger.Fatalf("error: %s", err)
}
logger.Printf("info: allowed modules, %+v", config.Allowed.Modules)
logger.Printf("info: allowed module domains, %+v", config.Allowed.Domains)
logger.Printf("info: blocked modules, %+v", config.Blocked.Modules.Get())
logger.Printf("info: blocked modules with version constraints, %+v", config.Blocked.Versions.Get())
results := processor.ProcessFiles(filteredFiles)
if report == "checkstyle" {
err := WriteCheckstyle(reportFile, results)
if err != nil {
logger.Fatalf("error: %s", err)
}
}
for _, r := range results {
fmt.Println(r.String())
}
if len(results) > 0 {
return issuesExitCode
}
return 0
}
// GetConfig from YAML file.
func GetConfig(configFile string) (*gomodguard.Configuration, error) {
config := gomodguard.Configuration{}
home, err := homedir.Dir()
if err != nil {
return nil, fmt.Errorf(errFindingHomedir, err)
}
homeDirCfgFile := filepath.Join(home, configFile)
var cfgFile string
switch {
case fileExists(configFile):
cfgFile = configFile
case fileExists(homeDirCfgFile):
cfgFile = homeDirCfgFile
default:
return nil, fmt.Errorf("%w: %s %s", errFindingConfigFile, configFile, homeDirCfgFile)
}
data, err := os.ReadFile(filepath.Clean(cfgFile))
if err != nil {
return nil, fmt.Errorf(errReadingConfigFile, err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf(errParsingConfigFile, err)
}
return &config, nil
}
// showHelp text for command line.
func showHelp() {
helpText := `Usage: gomodguard <file> [files...]
Also supports package syntax but will use it in relative path, i.e. ./pkg/...
Flags:`
fmt.Println(helpText)
flag.PrintDefaults()
}
// WriteCheckstyle takes the results and writes them to a checkstyle formated file.
func WriteCheckstyle(checkstyleFilePath string, results []gomodguard.Issue) error {
check := checkstyle.New()
for i := range results {
file := check.EnsureFile(results[i].FileName)
file.AddError(checkstyle.NewError(results[i].LineNumber, 1, checkstyle.SeverityError, results[i].Reason,
"gomodguard"))
}
body, err := xml.MarshalIndent(check, "", " ")
if err != nil {
return err
}
header := []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
checkstyleXML := slices.Concat([]byte{'\n'}, header, []byte{'\n'}, body)
err = os.WriteFile(checkstyleFilePath, checkstyleXML, 0644) //nolint:gosec
if err != nil {
return err
}
return nil
}
// fileExists returns true if the file path provided exists.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}