-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
87 lines (74 loc) · 1.87 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
package main
import (
"os"
"path"
"path/filepath"
"strings"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "gomove"
app.Usage = "Move Golang packages to a new path."
app.Version = "0.2.17"
app.ArgsUsage = "[old path] [new path]"
app.Author = "Kaushal Subedi <[email protected]>"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dir, d",
Value: "./",
Usage: "directory to scan. files under vendor/ are ignored",
},
cli.StringFlag{
Name: "file, f",
Usage: "only move imports in a file",
},
cli.StringFlag{
Name: "safe-mode, s",
Value: "false",
Usage: "run program in safe mode (comments will be wiped)",
},
}
app.Action = func(c *cli.Context) {
file := c.String("file")
dir := c.String("dir")
from := c.Args().Get(0)
to := c.Args().Get(1)
if file != "" {
ProcessFile(file, from, to, c)
} else {
ScanDir(dir, from, to, c)
}
}
app.Run(os.Args)
}
// ScanDir scans a directory for go files and
func ScanDir(dir string, from string, to string, c *cli.Context) {
// If from and to are not empty scan all files
if from != "" && to != "" {
// construct the path of the vendor dir of the project for prefix matching
vendorDir := path.Join(dir, "vendor")
// Scan directory for files
filepath.Walk(dir, func(filePath string, info os.FileInfo, err error) error {
// ignore vendor path
if matched := strings.HasPrefix(filePath, vendorDir); matched {
return nil
}
// Only process go files
if path.Ext(filePath) == ".go" {
ProcessFile(filePath, from, to, c)
}
return nil
})
} else {
cli.ShowAppHelp(c)
}
}
// ProcessFile processes file according to what mode is chosen
func ProcessFile(filePath string, from string, to string, c *cli.Context) {
if c.String("safe-mode") == "true" {
ProcessFileAST(filePath, from, to)
} else {
ProcessFileNative(filePath, from, to)
}
}