Skip to content

Commit

Permalink
Produce one binary, add support for key extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian W. Wolter committed Feb 26, 2019
1 parent 9071ecf commit 1386011
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin
target
29 changes: 10 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

# products
ENCODE=urlenc
DECODE=urldec
PRODUCT=urlenc

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

TEST_PKGS ?= encode/...

.PHONY: all test encode decode install release build build_release build_formula clean
.PHONY: all test urlenc install release build build_release build_formula clean

all: build

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

.PHONY: all encode decode

all: encode decode
all: urlenc

$(TARGETS):
mkdir -p $(TARGETS)

encode: $(BIN)/$(ENCODE)
urlenc: $(BIN)/$(PRODUCT)

$(BIN)/$(ENCODE): $(TARGETS) $(SRC)
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(ENCODE) $(MAIN)

decode: $(BIN)/$(DECODE)

$(BIN)/$(DECODE): $(TARGETS) $(SRC)
go build -ldflags "-X main.mode=dec -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(DECODE) $(MAIN)
$(BIN)/$(PRODUCT): $(TARGETS) $(SRC)
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(BIN)/$(PRODUCT) $(MAIN)

install: encode decode ## Build and install
install -m 0755 $(BIN)/$(ENCODE) $(BIN)/$(DECODE) $(PREFIX)/bin

$(RELEASE_BASE)/$(ENCODE): $(SRC)
go build -ldflags "-X main.mode=enc -X main.version=$(VERSION) -X main.githash=$(GITHASH)" -o $(RELEASE_BASE)/$(ENCODE) $(MAIN)
install -m 0755 $(BIN)/$(PRODUCT) $(PREFIX)/bin

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

$(RELEASE_PACKAGE): $(RELEASE_BASE)/$(ENCODE) $(RELEASE_BASE)/$(DECODE)
$(RELEASE_PACKAGE): $(RELEASE_BASE)/$(PRODUCT)
(cd $(RELEASE_TARGETS) && tar -zcf $(RELEASE_ARCHIVE) $(RELEASE_PRODUCT))

build_release: $(RELEASE_PACKAGE)
Expand Down
100 changes: 73 additions & 27 deletions src/encode/encode.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,82 @@
package main

import (
"os"
"fmt"
"strings"
"net/url"
"io/ioutil"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
)

var mode string
const (
encode = "enc"
decode = "dec"
var ( // set at compile time via the linker
version = "v0.0.0"
githash = "000000"
)

func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}

v := strings.TrimSpace(string(data))
switch mode {
case encode:
fmt.Println(url.QueryEscape(v))
case decode:
d, err := url.QueryUnescape(v)
if err != nil {
fmt.Printf("Invalid: %v\n", err)
}
fmt.Println(d)
default:
fmt.Printf("Unsupported mode: %v\n", mode)
}
var err error
var extractKeys flagList

cmdline := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var (
fEncode = cmdline.Bool("enc", false, "URLencode the input.")
fDecode = cmdline.Bool("dec", false, "URLdecode the input.")
fVerbose = cmdline.Bool("verbose", false, "Be more verbose.")
fVersion = cmdline.Bool("version", false, "Display the version.")
)
cmdline.Var(&extractKeys, "key", "Extract the value associated with the provided key. Provide -key repeatedly to extract multiple values.")
cmdline.Parse(os.Args[1:])

if *fVersion {
if version == githash {
fmt.Println(version)
} else {
fmt.Printf("%s (%s)\n", version, githash)
}
return
}

data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}

v := strings.TrimSpace(string(data))
if *fEncode {
v = url.QueryEscape(v)
} else if *fDecode {
v, err = url.QueryUnescape(v)
if err != nil {
fmt.Printf("Invalid: %v\n", err)
}
}

if len(extractKeys) < 1 {
fmt.Println(v)
return
}

u, err := url.ParseQuery(v)
if err != nil {
fmt.Printf("Invalid: %v\n", err)
}
for _, e := range extractKeys {
if *fVerbose {
fmt.Printf("%s: %s\n", e, u.Get(e))
} else {
fmt.Println(u.Get(e))
}
}
}

type flagList []string

func (s *flagList) Set(v string) error {
*s = append(*s, v)
return nil
}

func (s *flagList) String() string {
return fmt.Sprintf("%+v", *s)
}
1 change: 0 additions & 1 deletion tools/update-formula
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Urlencode < Formula
def install
system "install", "-d", "#{bin}"
system "install", "-m", "0755", "bin/urlenc", "#{bin}/urlenc"
system "install", "-m", "0755", "bin/urldec", "#{bin}/urldec"
end
end
FORMULA

0 comments on commit 1386011

Please sign in to comment.