Skip to content

Commit 1386011

Browse files
author
Brian W. Wolter
committed
Produce one binary, add support for key extraction
1 parent 9071ecf commit 1386011

4 files changed

Lines changed: 84 additions & 47 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bin
2+
target

Makefile

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
# products
3-
ENCODE=urlenc
4-
DECODE=urldec
3+
PRODUCT=urlenc
54

65
# build and packaging
76
MAIN := ./src/encode
@@ -23,7 +22,7 @@ RELEASE_BASE = $(RELEASE_TARGETS)/$(RELEASE_PRODUCT)/bin
2322

2423
TEST_PKGS ?= encode/...
2524

26-
.PHONY: all test encode decode install release build build_release build_formula clean
25+
.PHONY: all test urlenc install release build build_release build_formula clean
2726

2827
all: build
2928

@@ -32,31 +31,23 @@ PREFIX ?= /usr/local
3231

3332
.PHONY: all encode decode
3433

35-
all: encode decode
34+
all: urlenc
3635

3736
$(TARGETS):
3837
mkdir -p $(TARGETS)
3938

40-
encode: $(BIN)/$(ENCODE)
39+
urlenc: $(BIN)/$(PRODUCT)
4140

42-
$(BIN)/$(ENCODE): $(TARGETS) $(SRC)
43-
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(ENCODE) $(MAIN)
44-
45-
decode: $(BIN)/$(DECODE)
46-
47-
$(BIN)/$(DECODE): $(TARGETS) $(SRC)
48-
go build -ldflags "-X main.mode=dec -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(DECODE) $(MAIN)
41+
$(BIN)/$(PRODUCT): $(TARGETS) $(SRC)
42+
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(PRODUCT) $(MAIN)
4943

5044
install: encode decode ## Build and install
51-
install -m 0755 $(BIN)/$(ENCODE) $(BIN)/$(DECODE) $(PREFIX)/bin
52-
53-
$(RELEASE_BASE)/$(ENCODE): $(SRC)
54-
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(RELEASE_BASE)/$(ENCODE) $(MAIN)
45+
install -m 0755 $(BIN)/$(PRODUCT) $(PREFIX)/bin
5546

56-
$(RELEASE_BASE)/$(DECODE): $(SRC)
57-
go build -ldflags "-X main.mode=dec -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(RELEASE_BASE)/$(DECODE) $(MAIN)
47+
$(RELEASE_BASE)/$(PRODUCT): $(SRC)
48+
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(RELEASE_BASE)/$(PRODUCT) $(MAIN)
5849

59-
$(RELEASE_PACKAGE): $(RELEASE_BASE)/$(ENCODE) $(RELEASE_BASE)/$(DECODE)
50+
$(RELEASE_PACKAGE): $(RELEASE_BASE)/$(PRODUCT)
6051
(cd $(RELEASE_TARGETS) && tar -zcf $(RELEASE_ARCHIVE) $(RELEASE_PRODUCT))
6152

6253
build_release: $(RELEASE_PACKAGE)

src/encode/encode.go

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,82 @@
11
package main
22

33
import (
4-
"os"
5-
"fmt"
6-
"strings"
7-
"net/url"
8-
"io/ioutil"
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"net/url"
8+
"os"
9+
"strings"
910
)
1011

11-
var mode string
12-
const (
13-
encode = "enc"
14-
decode = "dec"
12+
var ( // set at compile time via the linker
13+
version = "v0.0.0"
14+
githash = "000000"
1515
)
1616

1717
func main() {
18-
data, err := ioutil.ReadAll(os.Stdin)
19-
if err != nil {
20-
panic(err)
21-
}
22-
23-
v := strings.TrimSpace(string(data))
24-
switch mode {
25-
case encode:
26-
fmt.Println(url.QueryEscape(v))
27-
case decode:
28-
d, err := url.QueryUnescape(v)
29-
if err != nil {
30-
fmt.Printf("Invalid: %v\n", err)
31-
}
32-
fmt.Println(d)
33-
default:
34-
fmt.Printf("Unsupported mode: %v\n", mode)
35-
}
18+
var err error
19+
var extractKeys flagList
20+
21+
cmdline := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
22+
var (
23+
fEncode = cmdline.Bool("enc", false, "URLencode the input.")
24+
fDecode = cmdline.Bool("dec", false, "URLdecode the input.")
25+
fVerbose = cmdline.Bool("verbose", false, "Be more verbose.")
26+
fVersion = cmdline.Bool("version", false, "Display the version.")
27+
)
28+
cmdline.Var(&extractKeys, "key", "Extract the value associated with the provided key. Provide -key repeatedly to extract multiple values.")
29+
cmdline.Parse(os.Args[1:])
30+
31+
if *fVersion {
32+
if version == githash {
33+
fmt.Println(version)
34+
} else {
35+
fmt.Printf("%s (%s)\n", version, githash)
36+
}
37+
return
38+
}
39+
40+
data, err := ioutil.ReadAll(os.Stdin)
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
v := strings.TrimSpace(string(data))
46+
if *fEncode {
47+
v = url.QueryEscape(v)
48+
} else if *fDecode {
49+
v, err = url.QueryUnescape(v)
50+
if err != nil {
51+
fmt.Printf("Invalid: %v\n", err)
52+
}
53+
}
54+
55+
if len(extractKeys) < 1 {
56+
fmt.Println(v)
57+
return
58+
}
59+
60+
u, err := url.ParseQuery(v)
61+
if err != nil {
62+
fmt.Printf("Invalid: %v\n", err)
63+
}
64+
for _, e := range extractKeys {
65+
if *fVerbose {
66+
fmt.Printf("%s: %s\n", e, u.Get(e))
67+
} else {
68+
fmt.Println(u.Get(e))
69+
}
70+
}
71+
}
72+
73+
type flagList []string
74+
75+
func (s *flagList) Set(v string) error {
76+
*s = append(*s, v)
77+
return nil
78+
}
79+
80+
func (s *flagList) String() string {
81+
return fmt.Sprintf("%+v", *s)
3682
}

tools/update-formula

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class Urlencode < Formula
4545
def install
4646
system "install", "-d", "#{bin}"
4747
system "install", "-m", "0755", "bin/urlenc", "#{bin}/urlenc"
48-
system "install", "-m", "0755", "bin/urldec", "#{bin}/urldec"
4948
end
5049
end
5150
FORMULA

0 commit comments

Comments
 (0)