Skip to content

Commit 4f013bd

Browse files
committed
Switch codestyle to official go codestyle
1 parent a0fbd7c commit 4f013bd

6 files changed

Lines changed: 619 additions & 621 deletions

File tree

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ indent_size = 2
2525
trim_trailing_whitespace = false
2626

2727
[*.go]
28+
indent_style = tab
2829
indent_size = 4

filehandling.go

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,86 @@
11
package main
22

33
import (
4-
"fmt"
5-
"bytes"
6-
"io/ioutil"
7-
"path/filepath"
8-
"bufio"
9-
"os"
10-
"regexp"
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
10+
"regexp"
1111
)
1212

1313
// Readln returns a single line (without the ending \n)
1414
// from the input buffered reader.
1515
// An error is returned iff there is an error with the
1616
// buffered reader.
1717
func Readln(r *bufio.Reader) (string, error) {
18-
var (isPrefix bool = true
19-
err error = nil
20-
line, ln []byte
21-
)
22-
for isPrefix && err == nil {
23-
line, isPrefix, err = r.ReadLine()
24-
ln = append(ln, line...)
25-
}
26-
return string(ln),err
18+
var (
19+
isPrefix bool = true
20+
err error = nil
21+
line, ln []byte
22+
)
23+
for isPrefix && err == nil {
24+
line, isPrefix, err = r.ReadLine()
25+
ln = append(ln, line...)
26+
}
27+
return string(ln), err
2728
}
2829

2930
// Write content to file
3031
func writeContentToFile(fileitem fileitem, content bytes.Buffer) (string, bool) {
31-
// --dry-run
32-
if opts.DryRun {
33-
return content.String(), true
34-
} else {
35-
var err error
36-
err = ioutil.WriteFile(fileitem.Output, content.Bytes(), 0644)
37-
if err != nil {
38-
panic(err)
39-
}
32+
// --dry-run
33+
if opts.DryRun {
34+
return content.String(), true
35+
} else {
36+
var err error
37+
err = ioutil.WriteFile(fileitem.Output, content.Bytes(), 0644)
38+
if err != nil {
39+
panic(err)
40+
}
4041

41-
return fmt.Sprintf("%s found and replaced match\n", fileitem.Path), true
42-
}
42+
return fmt.Sprintf("%s found and replaced match\n", fileitem.Path), true
43+
}
4344
}
4445

4546
// search files in path
4647
func searchFilesInPath(path string, callback func(os.FileInfo, string)) {
47-
var pathRegex *regexp.Regexp
48+
var pathRegex *regexp.Regexp
4849

49-
// --path-regex
50-
if (opts.PathRegex != "") {
51-
pathRegex = regexp.MustCompile(opts.PathRegex)
52-
}
50+
// --path-regex
51+
if opts.PathRegex != "" {
52+
pathRegex = regexp.MustCompile(opts.PathRegex)
53+
}
5354

54-
// collect all files
55-
filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
56-
filename := f.Name()
55+
// collect all files
56+
filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
57+
filename := f.Name()
5758

58-
// skip directories
59-
if f.IsDir() {
60-
if contains(pathFilterDirectories, f.Name()) {
61-
return filepath.SkipDir
62-
}
59+
// skip directories
60+
if f.IsDir() {
61+
if contains(pathFilterDirectories, f.Name()) {
62+
return filepath.SkipDir
63+
}
6364

64-
return nil
65-
}
65+
return nil
66+
}
6667

67-
// --path-pattern
68-
if (opts.PathPattern != "") {
69-
matched, _ := filepath.Match(opts.PathPattern, filename)
70-
if (!matched) {
71-
return nil
72-
}
73-
}
68+
// --path-pattern
69+
if opts.PathPattern != "" {
70+
matched, _ := filepath.Match(opts.PathPattern, filename)
71+
if !matched {
72+
return nil
73+
}
74+
}
7475

75-
// --path-regex
76-
if pathRegex != nil {
77-
if (!pathRegex.MatchString(path)) {
78-
return nil
79-
}
80-
}
76+
// --path-regex
77+
if pathRegex != nil {
78+
if !pathRegex.MatchString(path) {
79+
return nil
80+
}
81+
}
8182

82-
callback(f, path)
83-
return nil
84-
})
83+
callback(f, path)
84+
return nil
85+
})
8586
}

funcs.go

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,100 @@
11
package main
22

3+
import ()
34
import (
4-
)
5-
import (
6-
"regexp"
7-
"bytes"
8-
"bufio"
5+
"bufio"
6+
"bytes"
7+
"regexp"
98
)
109

1110
// check if string is contained in an array
1211
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+
}
1716

18-
_, ok := set[item]
19-
return ok
17+
_, ok := set[item]
18+
return ok
2019
}
2120

2221
// 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+
}
2726

28-
return false
27+
return false
2928
}
3029

3130
// 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+
}
3938
}
4039

4140
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
101100
}

log.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
package main
22

33
import (
4-
"fmt"
5-
"os"
6-
"strings"
4+
"fmt"
5+
"os"
6+
"strings"
77
)
88

99
// Log message
1010
func logMessage(message string) {
11-
if opts.Verbose {
12-
fmt.Fprintln(os.Stderr, message)
13-
}
11+
if opts.Verbose {
12+
fmt.Fprintln(os.Stderr, message)
13+
}
1414
}
1515

1616
// Log error object as message
1717
func logError(err error) {
18-
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s\n", err))
18+
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s\n", err))
1919
}
2020

21-
2221
// Log error object as message
2322
func logFatalErrorAndExit(err error, exitCode int) {
24-
cmdline := fmt.Sprintf("%s %s", argparser.Command.Name, strings.Join(os.Args[1:], " "))
23+
cmdline := fmt.Sprintf("%s %s", argparser.Command.Name, strings.Join(os.Args[1:], " "))
2524

26-
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
27-
fmt.Fprintln(os.Stderr, fmt.Sprintf("Command: %s", cmdline))
25+
fmt.Fprintln(os.Stderr, fmt.Sprintf("Error: %s", err))
26+
fmt.Fprintln(os.Stderr, fmt.Sprintf("Command: %s", cmdline))
2827

29-
os.Exit(exitCode)
28+
os.Exit(exitCode)
3029
}

0 commit comments

Comments
 (0)