forked from incu6us/goimports-reviser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
180 lines (147 loc) · 4.25 KB
/
main.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
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
// SPDX-FileCopyrightText: 2023 Christoph Mewes
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"github.com/incu6us/goimports-reviser/v3/pkg/module"
"github.com/spf13/pflag"
"go.xrstf.de/gimps/pkg/gimps"
)
// These variables get set by ldflags during compilation.
var (
BuildTag string
BuildCommit string
BuildDate string // RFC3339 format ("2006-01-02T15:04:05Z07:00")
)
func printVersion() {
// handle empty values in case `go install` was used
if BuildCommit == "" {
fmt.Printf("gimps dev, built with %s\n",
runtime.Version(),
)
} else {
fmt.Printf("gimps %s (%s), built with %s on %s\n",
BuildTag,
BuildCommit[:10],
runtime.Version(),
BuildDate,
)
}
}
func main() {
configFile := ""
dryRun := false
showVersion := false
stdout := false
verbose := false
pflag.StringVarP(&configFile, "config", "c", configFile, "Path to the config file (mandatory).")
pflag.BoolVarP(&stdout, "stdout", "s", stdout, "Print output to stdout instead of updating the source file(s).")
pflag.BoolVarP(&dryRun, "dry-run", "d", dryRun, "Do not update files.")
pflag.BoolVarP(&verbose, "verbose", "v", verbose, "List all instead of just changed files.")
pflag.BoolVarP(&showVersion, "version", "V", showVersion, "Show version and exit.")
pflag.Parse()
if showVersion {
printVersion()
return
}
if pflag.NArg() == 0 {
log.Fatal("Usage: gimps [--stdout] [--dry-run] [--config=(autodetect)] FILE_OR_DIRECTORY[, ...]")
}
inputs, err := cleanupArgs(pflag.Args())
if err != nil {
log.Fatalf("Invalid arguments: %v.", err)
}
// to auto-detect the .gimps.yaml, we need to find the go.mod; this can fail in
// some special repos, so the "guess .gimps.yaml location" logic is best effort only
modRoot, modRootErr := goModRootPath(inputs[0])
config, err := loadConfiguration(configFile, modRoot)
if err != nil {
log.Fatalf("Failed to load -config file %q: %v", configFile, err)
}
if config.ProjectName == "" {
if modRootErr != nil {
log.Fatalf("Failed to auto-detect module root: %v", err)
}
modName, err := module.Name(modRoot)
if err != nil {
log.Fatalf("Failed to auto-detect project name based on the first given file (%q): %v", inputs[0], err)
}
config.ProjectName = modName
}
aliaser, err := gimps.NewAliaser(config.ProjectName, config.AliasRules)
if err != nil {
log.Fatalf("Failed to initialize aliaser: %v", err)
}
for _, input := range inputs {
filenames, err := listFiles(input, modRoot, config.Exclude)
if err != nil {
log.Fatalf("Failed to process %q: %v", input, err)
}
for _, filename := range filenames {
if *config.DetectGeneratedFiles {
generated, err := isGeneratedFile(filename)
if err != nil {
log.Fatalf("Cannot check if file %q is generated: %v", filename, err)
}
if generated {
continue
}
}
relPath, err := filepath.Rel(modRoot, filename)
if err != nil {
log.Fatalf("This should never happen, could not determine relative path: %v", err)
}
if verbose {
log.Printf("> %s", relPath)
}
formattedOutput, hasChange, err := gimps.Execute(&config.Config, filename, aliaser)
if err != nil {
log.Fatalf("Failed to process %q: %v", filename, err)
}
if stdout {
fmt.Print(string(formattedOutput))
} else if hasChange {
if verbose {
log.Printf("! %s", relPath)
} else {
log.Printf("Fixed %s", relPath)
}
if !dryRun {
if err := os.WriteFile(filename, formattedOutput, 0644); err != nil {
log.Fatalf("Failed to write fixed result to file %q: %v", filename, err)
}
}
}
}
}
}
// cleanupArgs removes duplicates and turns every argument into an absolute
// filesystem path. The result is sorted alphabetically.
func cleanupArgs(args []string) ([]string, error) {
unique := map[string]struct{}{}
for _, arg := range args {
if arg == "" {
var err error
arg, err = os.Getwd()
if err != nil {
return nil, fmt.Errorf("invalid path %q: %v", arg, err)
}
}
abs, err := filepath.Abs(arg)
if err != nil {
return nil, fmt.Errorf("invalid path %q: %v", arg, err)
}
unique[abs] = struct{}{}
}
result := []string{}
for path := range unique {
result = append(result, path)
}
sort.Strings(result)
return result, nil
}