-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
214 lines (181 loc) · 4.75 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/pkg/errors"
"github.com/zhuxubin01/bbimports/helper"
"github.com/zhuxubin01/bbimports/reviser"
)
const (
projectNameArg = "project-name"
filePathArg = "file-path"
versionArg = "version"
removeUnusedImportsArg = "rm-unused"
setAliasArg = "set-alias"
localPkgPrefixesArg = "local"
outputArg = "output"
formatArg = "format"
listDiffFileNameArg = "list-diff"
setExitStatusArg = "set-exit-status"
)
// Project build specific vars
var (
Tag string
Commit string
SourceURL string
GoVersion string
shouldShowVersion *bool
shouldRemoveUnusedImports *bool
shouldSetAlias *bool
shouldFormat *bool
listFileName *bool
setExitStatus *bool
)
var projectName, filePath, localPkgPrefixes, output string
func init() {
flag.StringVar(
&filePath,
filePathArg,
"",
"File path to fix imports(ex.: ./reviser/reviser.go). Optional parameter.",
)
flag.StringVar(
&projectName,
projectNameArg,
"",
"Your project name(ex.: github.com/incu6us/bbimports). Optional parameter.",
)
flag.StringVar(
&localPkgPrefixes,
localPkgPrefixesArg,
"",
"Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.",
)
flag.StringVar(
&output,
outputArg,
"file",
`Can be "file", "write" or "stdout". Whether to write the formatted content back to the file or to stdout. When "write" together with "-list" will list the file name and write back to the file. Optional parameter.`,
)
listFileName = flag.Bool(
listDiffFileNameArg,
false,
"Option will list files whose formatting differs from bbimports. Optional parameter.",
)
setExitStatus = flag.Bool(
setExitStatusArg,
false,
"set the exit status to 1 if a change is needed/made. Optional parameter.",
)
shouldRemoveUnusedImports = flag.Bool(
removeUnusedImportsArg,
false,
"Remove unused imports. Optional parameter.",
)
shouldSetAlias = flag.Bool(
setAliasArg,
false,
"Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. "+
"In this case import will be set as 'pg \"github.com/go-pg/pg/v9\"'. Optional parameter.",
)
shouldFormat = flag.Bool(
formatArg,
false,
"Option will perform additional formatting. Optional parameter.",
)
if Tag != "" {
shouldShowVersion = flag.Bool(
versionArg,
false,
"Show version.",
)
}
}
func printUsage() {
if _, err := fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]); err != nil {
log.Fatalf("failed to print usage: %s", err)
}
flag.PrintDefaults()
}
func printVersion() {
fmt.Printf(
"version: %s\nbuild with: %s\ntag: %s\ncommit: %s\nsource: %s\n",
strings.TrimPrefix(Tag, "v"),
GoVersion,
Tag,
Commit,
SourceURL,
)
}
func main() {
flag.Parse()
if shouldShowVersion != nil && *shouldShowVersion {
printVersion()
return
}
if filePath == "" {
filePath = reviser.StandardInput
}
if err := validateRequiredParam(filePath); err != nil {
fmt.Printf("%s\n\n", err)
printUsage()
os.Exit(1)
}
var options reviser.Options
if shouldRemoveUnusedImports != nil && *shouldRemoveUnusedImports {
options = append(options, reviser.OptionRemoveUnusedImports)
}
if shouldSetAlias != nil && *shouldSetAlias {
options = append(options, reviser.OptionUseAliasForVersionSuffix)
}
if shouldFormat != nil && *shouldFormat {
options = append(options, reviser.OptionFormat)
}
projectName, err := helper.DetermineProjectName(projectName, filePath)
if err != nil {
fmt.Printf("%s\n\n", err)
printUsage()
os.Exit(1)
}
formattedOutput, hasChange, err := reviser.Execute(projectName, filePath, localPkgPrefixes, options...)
if err != nil {
log.Fatalf("%+v", errors.WithStack(err))
}
if !hasChange && *listFileName {
return
}
if hasChange && *listFileName && output != "write" {
fmt.Println(filePath)
} else if output == "stdout" || filePath == reviser.StandardInput {
fmt.Print(string(formattedOutput))
} else if output == "file" || output == "write" {
if !hasChange {
return
}
if err := ioutil.WriteFile(filePath, formattedOutput, 0644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v", filePath, errors.WithStack(err))
}
if *listFileName {
fmt.Println(filePath)
}
} else {
log.Fatalf(`invalid output "%s" specified`, output)
}
if hasChange && *setExitStatus {
os.Exit(1)
}
}
func validateRequiredParam(filePath string) error {
if filePath == reviser.StandardInput {
stat, _ := os.Stdin.Stat()
if stat.Mode()&os.ModeNamedPipe == 0 {
// no data on stdin
return errors.Errorf("-%s should be set", filePathArg)
}
}
return nil
}