diff --git a/.gitignore b/.gitignore index ba077a4..d567ba0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bin +target diff --git a/Makefile b/Makefile index 87add2f..754eefc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ # products -ENCODE=urlenc -DECODE=urldec +PRODUCT=urlenc # build and packaging MAIN := ./src/encode @@ -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 @@ -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) diff --git a/src/encode/encode.go b/src/encode/encode.go index 5f46333..96105c8 100644 --- a/src/encode/encode.go +++ b/src/encode/encode.go @@ -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) } diff --git a/tools/update-formula b/tools/update-formula index e049f96..2829a86 100755 --- a/tools/update-formula +++ b/tools/update-formula @@ -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 \ No newline at end of file