-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
71 changed files
with
9,911 additions
and
0 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 |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
uritool | ||
dist/ | ||
.idea/ |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# This is an example goreleaser.yaml file with some sane defaults. | ||
# Make sure to check the documentation at http://goreleaser.com | ||
before: | ||
hooks: | ||
# you may remove this if you don't need go generate | ||
- go generate ./... | ||
|
||
builds: | ||
- | ||
binary: uritool | ||
flags: | ||
- -mod=vendor | ||
env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- darwin | ||
- windows | ||
goarch: | ||
- 386 | ||
- amd64 | ||
|
||
archive: | ||
replacements: | ||
darwin: darwin | ||
linux: linux | ||
windows: windows | ||
386: i386 | ||
amd64: amd64 | ||
format_overrides: | ||
- goos: windows | ||
format: zip | ||
|
||
checksum: | ||
name_template: 'checksums.txt' | ||
|
||
snapshot: | ||
name_template: "{{ .Tag }}-next" | ||
|
||
snapcraft: | ||
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" | ||
replacements: | ||
amd64: 64-bit | ||
386: 32-bit | ||
darwin: macOS | ||
linux: Tux | ||
publish: true | ||
summary: Go based cmd tool for URI escaping, parsing and extraction. | ||
description: Go based command line tool for URI escaping/encoding, unescaping/decoding and parsing parts of the URI. | ||
grade: stable | ||
confinement: strict | ||
|
||
sign: | ||
artifacts: checksum | ||
cmd: gpg | ||
args: ["-u", "A6349E78C6E207654029D63D684AF415E326EE40", "--output", "${signature}", "--detach-sign", "${artifact}"] | ||
|
||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
GOCMD=go | ||
GOBUILD=$(GOCMD) build | ||
GOCLEAN=$(GOCMD) clean | ||
GOTEST=$(GOCMD) test | ||
GOGET=$(GOCMD) get | ||
BINARY_NAME=mybinary | ||
BINARY_UNIX=$(BINARY_NAME)_unix | ||
|
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# uritool | ||
|
||
Tool that helps working with URIs on the command line, processing parts or preparing information. | ||
|
||
## Installation | ||
|
||
The following methods are available: | ||
|
||
### Download | ||
|
||
Directly download a version from the [release page](https://github.com/adrianrudnik/uritool/releases) | ||
|
||
### Snap | ||
|
||
Install it on any system supporting snaps: | ||
|
||
```sh | ||
snap install uritool | ||
``` | ||
|
||
### Build yourself | ||
|
||
Download the source code, review it and build it: | ||
|
||
```sh | ||
go test ./... | ||
go build -mod=vendor . | ||
``` | ||
|
||
This should build a local binary for your system with the name `uritool` in your current working directory. | ||
|
||
## Usage | ||
|
||
### Query commands | ||
|
||
Encodes the given value to a valid query parameter value: | ||
|
||
```sh | ||
uritool query escape --no-newline "hello / world!%%" | ||
|
||
# > hello+%2F+world%25%25 | ||
``` | ||
|
||
Decodes the given escaped query value: | ||
|
||
```sh | ||
uritool query unescape "hello+%2F+world%25%25" | ||
|
||
# > hello / world!%% | ||
``` | ||
|
||
### Path commands | ||
|
||
Escape the given value to a valid escaped path value: | ||
|
||
```sh | ||
uritool path encode "hello world" | ||
|
||
# > hello%20world | ||
``` | ||
|
||
Unescape the given escaped path value: | ||
|
||
```sh | ||
uritool path decode "hello%20world" | ||
|
||
# > hello world | ||
``` | ||
|
||
### Parse commands | ||
|
||
Parse a given URI and return all information as JSON: | ||
|
||
```sh | ||
uritool parse uri "https://my:pass@the.example.com:8080/what/ ever?this=is&this=isnot#doing" | ||
|
||
# > { | ||
# > "Scheme": "https", | ||
# > "Opaque": "", | ||
# > "Username": "my", | ||
# > "Password": "pass", | ||
# > "PasswordIsGiven": true, | ||
# > "Host": "the.example.com:8080", | ||
# > "Hostname": "the.example.com", | ||
# > "Port": 8080, | ||
# > "Path": "/what/ ever", | ||
# > "PathEscaped": "/what/%20ever", | ||
# > "RawQuery": "this=is\u0026this=isnot", | ||
# > "Fragment": "doing", | ||
# > "Query": { | ||
# > "this": [ | ||
# > "is", | ||
# > "isnot" | ||
# > ] | ||
# > } | ||
# > } | ||
``` | ||
|
||
But sometimes you just want a specific part or combination of it, so use can use the [go template](https://golang.org/pkg/text/template/) language: | ||
|
||
```sh | ||
uritool parse uri --format="Welcome {{.Username}} from {{.Hostname}}" "https://adrian:something@the.example.com:8080/?uh=oh" | ||
|
||
# > Welcome adrian from the.example.com | ||
|
||
uritool parse uri --format="Second entry is {{index .Query.things 1}}" "https://adrian:something@the.example.com:8080/?things=one&things=two" | ||
|
||
# > Second entry is two | ||
``` | ||
|
||
You can also parse query strings (leading "?" will be removed): | ||
|
||
```sh | ||
uritool parse query "?this=is&this=isnot" | ||
|
||
# > { | ||
# > "this": [ | ||
# > "is", | ||
# > "isnot" | ||
# > ] | ||
# > } | ||
``` | ||
|
||
This is also workable with the [go template](https://golang.org/pkg/text/template/) language: | ||
|
||
```sh | ||
uritool parse query -n --format="{{index .search 0}}" "search=mister&filter=x" | ||
|
||
# > mister | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmdtest | ||
|
||
import ( | ||
"bytes" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ExecuteCommand(root *cobra.Command, args ...string) (output string, err error) { | ||
_, output, err = ExecuteCommandC(root, args...) | ||
return output, err | ||
} | ||
|
||
func ExecuteCommandC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) { | ||
buf := new(bytes.Buffer) | ||
root.SetOutput(buf) | ||
root.SetArgs(args) | ||
|
||
c, err = root.ExecuteC() | ||
|
||
return c, buf.String(), err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/spf13/cobra" | ||
"html/template" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
var parseCmd = &cobra.Command{ | ||
Use: "parse", | ||
Short: "collection of commands that allow to parse and extract information", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_ = cmd.Help() | ||
}, | ||
} | ||
|
||
func doFormat(cmd *cobra.Command, data interface{}) (bool, error) { | ||
format, err := cmd.Flags().GetString("format") | ||
|
||
if err != nil { | ||
return true, err | ||
} | ||
|
||
if format != "" { | ||
tmpl, err := template.New("input").Parse(format) | ||
|
||
if err != nil { | ||
return true, err | ||
} | ||
|
||
var out bytes.Buffer | ||
|
||
if err := tmpl.Execute(&out, data); err != nil { | ||
return true, err | ||
} | ||
|
||
output(cmd, out.String()) | ||
|
||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func doJson(cmd *cobra.Command, data interface{}) error { | ||
b, err := json.MarshalIndent(data, "", " ") | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
output(cmd, string(b)) | ||
|
||
return nil | ||
} | ||
|
||
var parseUriCmd = &cobra.Command{ | ||
Use: "uri", | ||
Short: "parses the given uri into single parts and returns them as json or formatted template", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
parsed, err := url.Parse(args[0]) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
wrapped := NewUrl(parsed) | ||
|
||
done, err := doFormat(cmd, wrapped) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if done { | ||
return nil | ||
} | ||
|
||
if err := doJson(cmd, wrapped); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var parseQueryCmd = &cobra.Command{ | ||
Use: "query", | ||
Short: "parses the given query string and returns them as json or formatted template", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// remove leading question mark | ||
in := strings.TrimLeft(args[0], "?") | ||
|
||
parsed, err := url.ParseQuery(in) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
done, err := doFormat(cmd, parsed) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if done { | ||
return nil | ||
} | ||
|
||
if err := doJson(cmd, parsed); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} |
Oops, something went wrong.