-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
109 lines (94 loc) · 2.4 KB
/
handler.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type unusedImportError struct {
filePath, pkg string
fileLine int
}
// handleErrors handles/fixes error(s) if it can in the gofile
func handleErrors(body string) (handleable []unusedImportError, unhandleableLines []string) {
handleableLines, unhandleableLines := distinguishLines(body)
if len(unhandleableLines) == 0 {
handleable = buildHandleables(handleableLines)
}
return
}
func distinguishLines(body string) (handleableLines, unhandleableLines []string) {
allLines := strings.Split(body, "\n")
for _, lineValue := range allLines {
lineValue = strings.TrimSpace(lineValue)
if len(lineValue) == 0 {
// empty line, do nothing
} else if strings.HasPrefix(lineValue, "#") {
// comment line, do nothing
} else if strings.Contains(lineValue, "imported and not used") {
handleableLines = append(handleableLines, lineValue)
} else {
unhandleableLines = append(unhandleableLines, lineValue)
}
}
return
}
func buildHandleables(lines []string) (errors []unusedImportError) {
for _, line := range lines {
sections := strings.Split(line, ":")
filePath := sections[0]
fileLine, err := strconv.Atoi(sections[1])
if err != nil {
panic(err)
}
pkg := strings.TrimSpace(sections[3])
anUnusedImportError := &unusedImportError{
filePath: filePath,
fileLine: fileLine,
pkg: pkg,
}
errors = append(errors, *anUnusedImportError)
}
return
}
func fixErrors(errors []unusedImportError) error {
for _, anUnusedImportError := range errors {
lines, err := readLines(anUnusedImportError.filePath)
if err != nil {
return err
}
linesPosition := anUnusedImportError.fileLine - 1
lines[linesPosition] = "//" + lines[linesPosition]
if err := writeLines(lines, anUnusedImportError.filePath); err != nil {
return err
}
formatFile(anUnusedImportError.filePath)
}
return nil
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}