-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (41 loc) · 947 Bytes
/
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/krzysztofdrys/goimportstidy/format"
)
var local = flag.String("local", "", "local package name, used for grouping")
var write = flag.Bool("w", false, "write changes")
func usage() {
fmt.Fprintf(os.Stderr, "usage: goimportstidy [flags] [path]\n")
flag.PrintDefaults()
os.Exit(2)
}
func errAndExit(f string, args ...interface{}) {
fmt.Fprintf(os.Stderr, f, args...)
os.Exit(2)
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
usage()
}
file := flag.Arg(0)
s, err := os.Stat(file)
if err != nil {
errAndExit("failed to stat file: %v", err)
}
f, err := ioutil.ReadFile(file)
if err != nil {
errAndExit("failed to read file: %v", err)
}
output := format.File(string(f), *local)
if !*write {
fmt.Print(string(output))
}
if err := ioutil.WriteFile(file, []byte(output), s.Mode()); err != nil {
errAndExit("failed to format file: %v", err)
}
}