-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Produce one binary, add support for key extraction
- Loading branch information
Brian W. Wolter
committed
Feb 26, 2019
1 parent
9071ecf
commit 1386011
Showing
4 changed files
with
84 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
bin | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters