aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 9cd9a9d1a3642ec635a4a90a0d0323e7c3e54bdd (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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"go/parser"
	"go/printer"
	"go/token"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"

	"github.com/codegangsta/cli"
	"github.com/mgutz/ansi"

	"golang.org/x/tools/go/ast/astutil"
)

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)
	}

}

func ProcessFileNative(filePath string, from string, to string) {

	// Open file to read
	fileContent, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println(err)
	}

	// Scan file line by line
	scanner := bufio.NewScanner(bytes.NewReader(fileContent))

	// Track line that is being scanned
	scanLine := 0
	// Track number of changes in file
	numChages := 0

	// Control variables
	isImportLine := false

	// Store final output text
	output := ""

	// Scan through the lines of go file
	for scanner.Scan() {

		scanLine++
		line := scanner.Text()
		bareLine := strings.Replace(line, " ", "", -1)

		// If it is a single import statement, replace the path in that line
		if strings.Contains(bareLine, "import\"") {
			fmt.Println("Found Import On Line", scanLine)
			newImport := strings.Replace(line, from, to, -1)
			output += newImport + "\n"
			numChages++
			continue
		}

		// Change isImportLine accordingly if import statements are detected
		if strings.Contains(bareLine, "import(") {
			fmt.Println("Found Multiple Imports Starting On Line", scanLine)
			isImportLine = true
		} else if isImportLine && strings.Contains(bareLine, ")") {
			fmt.Println("Imports Finish On Line", scanLine)
			isImportLine = false
		}

		// If it is a import line, replace the import
		if isImportLine {
			newImport := strings.Replace(line, from, to, -1)
			fmt.Println("Replacing", line, "to", newImport, "on line", scanLine)
			output += newImport + "\n"
			numChages++
			continue
		}

		// Just copy the rest of the lines to the output
		output += line + "\n"

	}

	// Only write if changes were made
	if numChages > 0 {
		ioutil.WriteFile(filePath, []byte(output), os.ModePerm)
	}
}

func ProcessFileAST(filePath string, from string, to string) {

	//Colors to be used on the console
	red := ansi.ColorCode("red+bh")
	white := ansi.ColorCode("white+bh")
	greenUnderline := ansi.ColorCode("green+buh")
	blackOnWhite := ansi.ColorCode("black+b:white+h")
	//Reset the color
	reset := ansi.ColorCode("reset")

	// If the file is a go file scan it
	if path.Ext(filePath) == ".go" {
		// New FileSet to parse the go file to
		fSet := token.NewFileSet()

		// Parse the file
		file, err := parser.ParseFile(fSet, filePath, nil, 0)
		if err != nil {
			fmt.Println(err)
		}

		// Get the list of imports from the ast
		imports := astutil.Imports(fSet, file)

		// Keep track of number of changes
		numChanges := 0

		// Iterate through the imports array
		for _, mPackage := range imports {
			for _, mImport := range mPackage {
				// Since astutil returns the path string with quotes, remove those
				importString := mImport.Path.Value

				// If the path matches the oldpath, replace it with the new one
				if strings.Contains(importString, from) {
					//If it needs to be replaced, increase numChanges so we can write the file later
					numChanges++

					// Join the path of the import package with the remainder from the old one after removing the old import package
					replacePackage := strings.Replace(importString, from, to, -1)

					fmt.Println(red + "Updating import " + importString + " from file " + reset + white + filePath + reset + red + " to " + reset + white + replacePackage + reset)

					// Remove the old import and replace it with the replacement
					astutil.DeleteImport(fSet, file, importString)
					astutil.AddImport(fSet, file, replacePackage)
				}
			}
		}

		// If the number of changes are more than 0, write file
		if numChanges > 0 {
			// Print the new AST tree to a new output buffer
			var outputBuffer bytes.Buffer
			printer.Fprint(&outputBuffer, fSet, file)

			ioutil.WriteFile(filePath, outputBuffer.Bytes(), os.ModePerm)
			fmt.Printf(blackOnWhite+"File "+filePath+" saved after %d changes."+reset+"\n", numChanges)
		} else {
			fmt.Println(greenUnderline + "No changes needed on file " + filePath + reset)
		}
	}
}