| 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
 | package main
import (
	"os"
	"path"
	"path/filepath"
	"github.com/codegangsta/cli"
)
func main() {
	app := cli.NewApp()
	app.Name = "gomove"
	app.Usage = "Move Golang packages to a new path."
	app.Version = "0.0.1"
	app.ArgsUsage = "[old path] [new path]"
	app.Author = "Kaushal Subedi <kaushal@subedi.co>"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:  "dir, d",
			Value: "./",
			Usage: "directory to scan",
		},
		cli.StringFlag{
			Name:  "file, f",
			Usage: "only move imports in a file",
		},
	}
	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 != "" {
			ProcessFileNative(file, from, to)
		} 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 != "" {
		// Scan directory for files
		filepath.Walk(dir, func(filePath string, info os.FileInfo, err error) error {
			// Only process go files
			if path.Ext(filePath) == ".go" {
				ProcessFileNative(filePath, from, to)
			}
			return nil
		})
	} else {
		cli.ShowAppHelp(c)
	}
}
 |