Skip to content

Commit 4d0c12b

Browse files
committed
Initial revision
1 parent 40a24ae commit 4d0c12b

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.6
2+
/gr-*
3+
/goreplace
4+
/build/
5+
/.idea

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
SOURCE = $(wildcard *.go)
2+
TAG ?= $(shell git describe --tags)
3+
GOBUILD = go build -ldflags '-w'
4+
5+
ALL = \
6+
$(foreach arch,64 32,\
7+
$(foreach suffix,linux osx win.exe,\
8+
build/gr-$(arch)-$(suffix)))
9+
10+
all: $(ALL)
11+
12+
clean:
13+
rm -f $(ALL)
14+
15+
# os is determined as thus: if variable of suffix exists, it's taken, if not, then
16+
# suffix itself is taken
17+
win.exe = windows
18+
osx = darwin
19+
build/gr-64-%: $(SOURCE)
20+
@mkdir -p $(@D)
21+
CGO_ENABLED=0 GOOS=$(firstword $($*) $*) GOARCH=amd64 $(GOBUILD) -o $@
22+
23+
build/gr-32-%: $(SOURCE)
24+
@mkdir -p $(@D)
25+
CGO_ENABLED=0 GOOS=$(firstword $($*) $*) GOARCH=386 $(GOBUILD) -o $@
26+
27+
release: $(ALL)
28+
ifndef desc
29+
@echo "Run it as 'make release desc=tralala'"
30+
else
31+
github-release release -u webdevops -r goreplace -t "$(TAG)" -n "$(TAG)" --description "$(desc)"
32+
@for x in $(ALL); do \
33+
echo "Uploading $$x" && \
34+
github-release upload -u webdevops \
35+
-r goreplace \
36+
-t $(TAG) \
37+
-f "$$x" \
38+
-n "$$(basename $$x)"; \
39+
done
40+
endif

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# goreplace
22
Replace cli utility written in golang
3+
4+
Inspired by https://github.com/piranha/goreplace

goreplace.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
"regexp"
9+
flags "github.com/jessevdk/go-flags"
10+
)
11+
12+
const (
13+
Author = "webdevops.io"
14+
Version = "1.0"
15+
)
16+
17+
var opts struct {
18+
Search string `short:"s" long:"regex" description:"Search regexp" value-name:"RE"`
19+
Replace string `short:"r" long:"replace" description:"replace found substrings with RE" value-name:"RE"`
20+
IgnoreCase bool `short:"i" long:"ignore-case" description:"ignore pattern case"`
21+
PlainText bool `short:"p" long:"plain" description:"treat pattern as plain text"`
22+
Verbose bool `short:"v" long:"verbose" description:"verbose mode"`
23+
ShowVersion bool `short:"V" long:"version" description:"show version and exit"`
24+
ShowHelp bool `short:"h" long:"help" description:"show this help message"`
25+
}
26+
27+
var argparser = flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash)
28+
29+
func replaceInFile(file string) {
30+
var err error
31+
32+
read, err := ioutil.ReadFile(file)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
content := string(read)
38+
39+
if opts.PlainText {
40+
if strings.Contains(content, opts.Search) {
41+
content = strings.Replace(content, opts.Search, opts.Replace, -1)
42+
writeContentToFile(file, content)
43+
}
44+
} else {
45+
regex := opts.Search
46+
47+
if opts.IgnoreCase {
48+
regex = "(?i:" + regex + ")"
49+
}
50+
51+
re := regexp.MustCompile(regex)
52+
53+
if re.MatchString(content) {
54+
content = re.ReplaceAllString(content, opts.Replace)
55+
writeContentToFile(file, content)
56+
}
57+
}
58+
59+
}
60+
61+
func writeContentToFile(file string, content string) {
62+
var err error
63+
err = ioutil.WriteFile(file, []byte(content), 0)
64+
if err != nil {
65+
panic(err)
66+
}
67+
}
68+
69+
func main() {
70+
args, err := argparser.Parse()
71+
if err != nil {
72+
panic(err)
73+
os.Exit(1)
74+
}
75+
76+
if opts.ShowVersion {
77+
fmt.Printf("goreplace %s\n", Version)
78+
return
79+
}
80+
81+
if (opts.ShowHelp) || (opts.Search == "") || (opts.Replace == "") || (len(args) > 0) {
82+
fmt.Println("Usage: golang -s <search regex> -r <replace string> file1 file2 file3")
83+
argparser.WriteHelp(os.Stdout)
84+
os.Exit(1)
85+
}
86+
87+
for i := range args {
88+
var file string
89+
file = args[i]
90+
91+
if opts.Verbose {
92+
fmt.Printf(" - checking %s\n", file)
93+
}
94+
replaceInFile(file)
95+
}
96+
97+
os.Exit(0)
98+
}

0 commit comments

Comments
 (0)