forked from daixiang0/gci
/
main.go
65 lines (53 loc) · 1.15 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
package main
import (
"flag"
"fmt"
"go/scanner"
"os"
"github.com/mathieupost/gci/pkg/gci"
)
var (
doWrite = flag.Bool("w", false, "doWrite result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
localFlag string
exitCode = 0
)
func report(err error) {
if err == nil {
return
}
scanner.PrintError(os.Stderr, err)
exitCode = 1
}
func parseFlags() []string {
flag.StringVar(&localFlag, "local", "", "put imports beginning with this string after 3rd-party packages, only support one string")
flag.Parse()
return flag.Args()
}
func usage() {
_, _ = fmt.Fprintf(os.Stderr, "usage: gci [flags] [path ...]\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
paths := parseFlags()
flagSet := &gci.FlagSet{
LocalFlag: localFlag,
DoWrite: doWrite,
DoDiff: doDiff,
}
for _, path := range paths {
switch dir, err := os.Stat(path); {
case err != nil:
report(err)
case dir.IsDir():
report(gci.WalkDir(path, flagSet))
default:
if err := gci.ProcessFile(path, os.Stdout, flagSet); err != nil {
report(err)
}
}
}
os.Exit(exitCode)
}