aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: e7c51dfeef86a5d80c2d91af9aadf12e5b3fa32d (plain)
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
package main

import (
	"os"
	"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 {
			RunApp(dir, from, to, c)
		}

	}

	app.Run(os.Args)
}

func RunApp(dir string, from string, to string, c *cli.Context) {

	if from != "" && to != "" {
		filepath.Walk(dir, func(filePath string, info os.FileInfo, err error) error {
			ProcessFileNative(filePath, from, to)
			return nil
		})

	} else {
		cli.ShowAppHelp(c)
	}

}