|
1 | 1 | package main |
2 | 2 |
|
| 3 | +import () |
3 | 4 | import ( |
4 | | -) |
5 | | -import ( |
6 | | - "regexp" |
7 | | - "bytes" |
8 | | - "bufio" |
| 5 | + "bufio" |
| 6 | + "bytes" |
| 7 | + "regexp" |
9 | 8 | ) |
10 | 9 |
|
11 | 10 | // check if string is contained in an array |
12 | 11 | func contains(slice []string, item string) bool { |
13 | | - set := make(map[string]struct{}, len(slice)) |
14 | | - for _, s := range slice { |
15 | | - set[s] = struct{}{} |
16 | | - } |
| 12 | + set := make(map[string]struct{}, len(slice)) |
| 13 | + for _, s := range slice { |
| 14 | + set[s] = struct{}{} |
| 15 | + } |
17 | 16 |
|
18 | | - _, ok := set[item] |
19 | | - return ok |
| 17 | + _, ok := set[item] |
| 18 | + return ok |
20 | 19 | } |
21 | 20 |
|
22 | 21 | // Checks if there is a match in content, based on search options |
23 | | -func searchMatch(content string, changeset changeset) (bool) { |
24 | | - if changeset.Search.MatchString(content) { |
25 | | - return true |
26 | | - } |
| 22 | +func searchMatch(content string, changeset changeset) bool { |
| 23 | + if changeset.Search.MatchString(content) { |
| 24 | + return true |
| 25 | + } |
27 | 26 |
|
28 | | - return false |
| 27 | + return false |
29 | 28 | } |
30 | 29 |
|
31 | 30 | // Replace text in whole content based on search options |
32 | | -func replaceText(content string, changeset changeset) (string) { |
33 | | - // --regex-backrefs |
34 | | - if opts.RegexBackref { |
35 | | - return changeset.Search.ReplaceAllString(content, changeset.Replace) |
36 | | - } else { |
37 | | - return changeset.Search.ReplaceAllLiteralString(content, changeset.Replace) |
38 | | - } |
| 31 | +func replaceText(content string, changeset changeset) string { |
| 32 | + // --regex-backrefs |
| 33 | + if opts.RegexBackref { |
| 34 | + return changeset.Search.ReplaceAllString(content, changeset.Replace) |
| 35 | + } else { |
| 36 | + return changeset.Search.ReplaceAllLiteralString(content, changeset.Replace) |
| 37 | + } |
39 | 38 | } |
40 | 39 |
|
41 | 40 | func handleLineInFile(changesets []changeset, buffer bytes.Buffer) (*bytes.Buffer, bool) { |
42 | | - var ( |
43 | | - line string |
44 | | - writeBufferToFile bool |
45 | | - ) |
46 | | - |
47 | | - for _, changeset := range changesets { |
48 | | - if !changeset.MatchFound { |
49 | | - // just add line to file |
50 | | - line = changeset.Replace + "\n" |
51 | | - |
52 | | - // remove backrefs (no match) |
53 | | - if opts.RegexBackref { |
54 | | - line = regexp.MustCompile("\\$[0-9]+").ReplaceAllLiteralString(line, "") |
55 | | - } |
56 | | - |
57 | | - // --lineinfile-before |
58 | | - // --lineinfile-after |
59 | | - if opts.LineinfileBefore != "" || opts.LineinfileAfter != "" { |
60 | | - var matchFinder *regexp.Regexp |
61 | | - |
62 | | - if opts.LineinfileBefore != "" { |
63 | | - matchFinder = regexp.MustCompile(opts.LineinfileBefore) |
64 | | - } else { |
65 | | - matchFinder = regexp.MustCompile(opts.LineinfileAfter) |
66 | | - } |
67 | | - |
68 | | - var bufferCopy bytes.Buffer |
69 | | - |
70 | | - scanner := bufio.NewScanner(&buffer) |
71 | | - for scanner.Scan() { |
72 | | - originalLine := scanner.Text() |
73 | | - |
74 | | - if matchFinder.MatchString(originalLine) { |
75 | | - writeBufferToFile = true |
76 | | - |
77 | | - if opts.LineinfileBefore != "" { |
78 | | - bufferCopy.WriteString(line) |
79 | | - } |
80 | | - |
81 | | - bufferCopy.WriteString(originalLine + "\n") |
82 | | - |
83 | | - if opts.LineinfileAfter != "" { |
84 | | - bufferCopy.WriteString(line) |
85 | | - } |
86 | | - } else { |
87 | | - bufferCopy.WriteString(originalLine + "\n") |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - buffer.Reset() |
92 | | - buffer.WriteString(bufferCopy.String()) |
93 | | - } else { |
94 | | - buffer.WriteString(line) |
95 | | - writeBufferToFile = true |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - return &buffer, writeBufferToFile |
| 41 | + var ( |
| 42 | + line string |
| 43 | + writeBufferToFile bool |
| 44 | + ) |
| 45 | + |
| 46 | + for _, changeset := range changesets { |
| 47 | + if !changeset.MatchFound { |
| 48 | + // just add line to file |
| 49 | + line = changeset.Replace + "\n" |
| 50 | + |
| 51 | + // remove backrefs (no match) |
| 52 | + if opts.RegexBackref { |
| 53 | + line = regexp.MustCompile("\\$[0-9]+").ReplaceAllLiteralString(line, "") |
| 54 | + } |
| 55 | + |
| 56 | + // --lineinfile-before |
| 57 | + // --lineinfile-after |
| 58 | + if opts.LineinfileBefore != "" || opts.LineinfileAfter != "" { |
| 59 | + var matchFinder *regexp.Regexp |
| 60 | + |
| 61 | + if opts.LineinfileBefore != "" { |
| 62 | + matchFinder = regexp.MustCompile(opts.LineinfileBefore) |
| 63 | + } else { |
| 64 | + matchFinder = regexp.MustCompile(opts.LineinfileAfter) |
| 65 | + } |
| 66 | + |
| 67 | + var bufferCopy bytes.Buffer |
| 68 | + |
| 69 | + scanner := bufio.NewScanner(&buffer) |
| 70 | + for scanner.Scan() { |
| 71 | + originalLine := scanner.Text() |
| 72 | + |
| 73 | + if matchFinder.MatchString(originalLine) { |
| 74 | + writeBufferToFile = true |
| 75 | + |
| 76 | + if opts.LineinfileBefore != "" { |
| 77 | + bufferCopy.WriteString(line) |
| 78 | + } |
| 79 | + |
| 80 | + bufferCopy.WriteString(originalLine + "\n") |
| 81 | + |
| 82 | + if opts.LineinfileAfter != "" { |
| 83 | + bufferCopy.WriteString(line) |
| 84 | + } |
| 85 | + } else { |
| 86 | + bufferCopy.WriteString(originalLine + "\n") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + buffer.Reset() |
| 91 | + buffer.WriteString(bufferCopy.String()) |
| 92 | + } else { |
| 93 | + buffer.WriteString(line) |
| 94 | + writeBufferToFile = true |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return &buffer, writeBufferToFile |
101 | 100 | } |
0 commit comments