Skip to content

Commit

Permalink
Adds keypairs option
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Aug 27, 2018
1 parent 25cd4a7 commit 99ad735
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ unfurl
*.tgz
*.exe
*.zip
urls.txt
10 changes: 10 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ Sam
ExCo
```

### Query String Key/Value Pairs

```
▶ cat urls.txt | unfurl keypairs
id=123
name=Sam
org=ExCo
```

### Custom Formats

You can use the `format` mode to specify a custom output format:
Expand Down Expand Up @@ -143,6 +152,7 @@ Options:
Modes:
keys Keys from the query string (one per line)
values Values from the query string (one per line)
keypairs Key=value pairs from the query string (one per line)
domains The hostname (e.g. sub.example.com)
paths The request path (e.g. /users)
format Specify a custom format (see below)
Expand Down
30 changes: 23 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func main() {
fmtStr := flag.Arg(1)

procFn, ok := map[string]urlProc{
"keys": keys,
"values": values,
"domains": domains,
"domain": domains,
"paths": paths,
"path": paths,
"format": format,
"keys": keys,
"values": values,
"keypairs": keyPairs,
"domains": domains,
"domain": domains,
"paths": paths,
"path": paths,
"format": format,
}[mode]

if !ok {
Expand Down Expand Up @@ -134,6 +135,20 @@ func values(u *url.URL, _ string) []string {
return out
}

// keyPairs returns all the key=value pairs in
// the query string portion of the URL. E.g for
// /?one=1&two=2&three=3 it will return
// []string{"one=1", "two=2", "three=3"}
func keyPairs(u *url.URL, _ string) []string {
out := make([]string, 0)
for key, vals := range u.Query() {
for _, val := range vals {
out = append(out, fmt.Sprintf("%s=%s", key, val))
}
}
return out
}

// domains returns the domain portion of the URL. e.g.
// for http://sub.example.com/path it will return
// []string{"sub.example.com"}
Expand Down Expand Up @@ -298,6 +313,7 @@ func init() {
h += "Modes:\n"
h += " keys Keys from the query string (one per line)\n"
h += " values Values from the query string (one per line)\n"
h += " keypairs Key=value pairs from the query string (one per line)\n"
h += " domains The hostname (e.g. sub.example.com)\n"
h += " paths The request path (e.g. /users)\n"
h += " format Specify a custom format (see below)\n\n"
Expand Down

0 comments on commit 99ad735

Please sign in to comment.