-
Notifications
You must be signed in to change notification settings - Fork 1
/
remover.go
executable file
·62 lines (57 loc) · 1.34 KB
/
remover.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
func main() {
fileName := flag.String("f", "", "path to the file")
flag.Parse()
if *fileName == "" {
fmt.Fprintln(os.Stderr, "Please provide a file name")
fmt.Fprintln(os.Stderr, "Usage: preambleInserter -f <file>")
os.Exit(1)
}
if _, err := os.Stat(*fileName); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "File %s does not exist\n", *fileName)
os.Exit(1)
}
file, err := os.Open(*fileName)
if err != nil {
panic(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
inImportBlock := false
numberOfLinesToSkip := 0
for scanner.Scan() {
line := scanner.Text()
if numberOfLinesToSkip > 0 {
numberOfLinesToSkip--
continue
} else if strings.Contains(line, "// ======= Preamble Start =======") {
numberOfLinesToSkip = 3
continue
} else if strings.Contains(line, "import (") {
inImportBlock = true
lines = append(lines, line)
} else if inImportBlock && strings.Contains(line, ")") {
inImportBlock = false
lines = append(lines, line)
} else if inImportBlock && strings.Contains(line, "\"advocate\"") {
continue
} else {
lines = append(lines, line)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
err = os.WriteFile(*fileName, []byte(strings.Join(lines, "\n")), 0644)
if err != nil {
panic(err)
}
}