From 06f84a87d1cf5ad49e423402552684af38c26457 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 3 Feb 2020 00:05:18 +0100 Subject: [PATCH] initial commit --- .drone.star | 150 +++++++++++++++++++++++++++++++++++++++++++ .drone.yml | 116 +++++++++++++++++++++++++++++++++ .gitignore | 16 +++++ LICENSE | 21 ++++++ README.md | 73 +++++++++++++++++++++ commands/commands.go | 20 ++++++ commands/password.go | 20 ++++++ commands/path.go | 40 ++++++++++++ commands/run.go | 17 +++++ commands/scheme.go | 17 +++++ commands/user.go | 19 ++++++ go.mod | 9 +++ go.sum | 25 ++++++++ 13 files changed, 543 insertions(+) create mode 100644 .drone.star create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 commands/commands.go create mode 100644 commands/password.go create mode 100644 commands/path.go create mode 100644 commands/run.go create mode 100644 commands/scheme.go create mode 100644 commands/user.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.drone.star b/.drone.star new file mode 100644 index 0000000..ec91fb4 --- /dev/null +++ b/.drone.star @@ -0,0 +1,150 @@ +def main(ctx): + before = testing() + + stages = [ + binaries([]), + ] + + after = notification() + + for b in before: + for s in stages: + s['depends_on'].append(b['name']) + + for s in stages: + for a in after: + a['depends_on'].append(s['name']) + + return before + stages + after + +def testing(): + return [{ + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'testing', + 'platform': { + 'os': 'linux', + 'arch': 'amd64', + }, + 'steps': [ + { + 'name': 'vet', + 'image': 'golang:1.12', + 'commands': [ + 'go vet ./...' + ], + }, + { + 'name': 'test', + 'image': 'golang:1.12', + 'commands': [ + 'go test -cover ./...' + ], + } + ], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + 'refs/pull/**' + ] + } + }] + +def binaries(arch): + return { + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'build-binaries', + 'steps': [ + { + 'name': 'build', + 'image': 'techknowlogick/xgo:latest', + 'commands': [ + '[ -z "${DRONE_TAG}" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}', + 'mkdir -p release/', + "xgo -ldflags \"-X main.Version=$BUILD_VERSION\" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier-$BUILD_VERSION .", + 'cp /build/* release/' + ] + }, + { + 'name': 'executable', + 'image': 'golang:1.12', + 'commands': [ + '$(find release/ -executable -type f | grep url-parser-*-linux-amd64) --help', + ] + }, + { + 'name': 'checksum', + 'image': 'alpine', + 'commands': [ + 'cd release/ && sha256sum * > sha256sum.txt', + ], + }, + { + 'name': 'publish', + 'image': 'plugins/github-release', + 'settings': { + 'overwrite': True, + 'api_key': { + 'from_secret': 'github_token' + }, + 'files': [ "release/*" ], + 'title': '${DRONE_TAG}', + 'note': 'CHANGELOG.md', + }, + 'when': { + 'ref': [ + 'refs/tags/**' + ] + } + } + ], + 'depends_on': [], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**', + 'refs/pull/**' + ] + } + } + +def notification(): + return [{ + 'kind': 'pipeline', + 'type': 'docker', + 'name': 'notification', + 'steps': [ + { + 'name': 'matrix', + 'image': 'plugins/matrix', + 'settings': { + 'homeserver': { + 'from_secret': 'matrix_homeserver', + }, + 'password': { + 'from_secret': 'matrix_password', + }, + 'roomid': { + 'from_secret': 'matrix_roomid', + }, + 'template': 'Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}', + 'username': { + 'from_secret': 'matrix_username', + }, + }, + }, + ], + 'depends_on': [], + 'trigger': { + 'ref': [ + 'refs/heads/master', + 'refs/tags/**' + ], + 'status': [ + 'success', + 'failure' + ] + } + }] diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..a2a0a99 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,116 @@ +--- +kind: pipeline +type: docker +name: testing + +platform: + os: linux + arch: amd64 + +steps: +- name: vet + image: golang:1.12 + commands: + - go vet ./... + +- name: test + image: golang:1.12 + commands: + - go test -cover ./... + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +--- +kind: pipeline +type: docker +name: build-binaries + +platform: + os: linux + arch: amd64 + +steps: +- name: build + image: techknowlogick/xgo:latest + commands: + - "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}" + - mkdir -p release/ + - xgo -ldflags "-X main.Version=$BUILD_VERSION" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier-$BUILD_VERSION . + - cp /build/* release/ + +- name: executable + image: golang:1.12 + commands: + - $(find release/ -executable -type f | grep url-parser-*-linux-amd64) --help + +- name: checksum + image: alpine + commands: + - cd release/ && sha256sum * > sha256sum.txt + +- name: publish + image: plugins/github-release + settings: + api_key: + from_secret: github_token + files: + - release/* + note: CHANGELOG.md + overwrite: true + title: ${DRONE_TAG} + when: + ref: + - refs/tags/** + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +depends_on: +- testing + +--- +kind: pipeline +type: docker +name: notification + +platform: + os: linux + arch: amd64 + +steps: +- name: matrix + image: plugins/matrix + settings: + homeserver: + from_secret: matrix_homeserver + password: + from_secret: matrix_password + roomid: + from_secret: matrix_roomid + template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}" + username: + from_secret: matrix_username + +trigger: + ref: + - refs/heads/master + - refs/tags/** + status: + - success + - failure + +depends_on: +- build-binaries + +--- +kind: signature +hmac: fbeb9dcf896d5f5af227722aeecda447c8aeb4bfe83becb2adcc0ca398f52798 + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54cb485 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +url-parser diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bb3e998 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Robert Kaussow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..09bde31 --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +# url-parser + +[![Build Status](https://img.shields.io/drone/build/xoxys/url-parser?logo=drone)](https://cloud.drone.io/xoxys/url-parser) +[![License: MIT](https://img.shields.io/github/license/xoxys/url-parser)](LICENSE) + +Inspired by [herloct/url-parser](https://github.com/herloct/url-parser), a simple command-line utility for parsing URLs. + +## Instalation + +Prebuild multiarch binaries are availabe for Linux only: + +```Shell +curl -L https://github.com/xoxys/url-parser/releases/download/0.1.0-beta4/url-parser-0.1.0-linux-amd64 > /usr/local/bin/url-parser +chmod +x /usr/local/bin/url-parser +url-parser --help +``` + +## Usage + +```Shell +$ url-parser --help +NAME: + url-parser - Parse URL and shows the part of it. + +USAGE: + url-parser [global options] command [command options] [arguments...] + +VERSION: + 0.1.0 + +COMMANDS: + all, a print out all parts from url + scheme, s print out scheme from url + user, u print out username from url + password, pw print out password from url + path, p print out the path from url + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --url value source url to parse [$URL_PARSER_URL] + --help, -h show help (default: false) + --version, -v print the version (default: false) +``` + +## Examples + +```Shell +$ url-parser host --url https://somedomain.com +somedomain.com + +$ url-parser user --url https://herloct@somedomain.com +herloct + +$ url-parser path --url https://somedomain.com/path/to +/path/to + +$ url-parser path --path-index=1 --url https://somedomain.com/path/to +to + +$ url-parser query --url https://somedomain.com/?some-key=somevalue +some-key=somevalue + +$ url-parser query --query-field=some-key --url https://somedomain.com/?some-key=somevalue +somevalue +``` + +### License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +### Maintainers and Contributors + +[Robert Kaussow](https://github.com/xoxys) diff --git a/commands/commands.go b/commands/commands.go new file mode 100644 index 0000000..8e8b598 --- /dev/null +++ b/commands/commands.go @@ -0,0 +1,20 @@ +package commands + +import ( + "net/url" + "strings" + + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +func parseURL(c *cli.Context) *url.URL { + urlString := strings.TrimSpace(c.String("url")) + + url, err := url.Parse(urlString) + if err != nil { + logrus.Fatal(err) + } + + return url +} diff --git a/commands/password.go b/commands/password.go new file mode 100644 index 0000000..0ae2755 --- /dev/null +++ b/commands/password.go @@ -0,0 +1,20 @@ +package commands + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +// Password prints out the password part from url +func Password(ctx *cli.Context) error { + parts := parseURL(ctx) + + if parts.User != nil { + pw, _ := parts.User.Password() + if len(pw) > 0 { + fmt.Println(pw) + } + } + return nil +} diff --git a/commands/path.go b/commands/path.go new file mode 100644 index 0000000..7b997c7 --- /dev/null +++ b/commands/path.go @@ -0,0 +1,40 @@ +package commands + +import ( + "fmt" + "strings" + + "github.com/urfave/cli/v2" +) + +// PathFlags defines flags for path subcommand +func PathFlags() []cli.Flag { + return []cli.Flag{ + &cli.IntFlag{ + Name: "path-index", + Usage: "filter parsed path by index", + EnvVars: []string{"URL_PARSER_PATH_INDEX"}, + Value: -1, + }, + } +} + +// Path prints out the path part from url +func Path(ctx *cli.Context) error { + parts := parseURL(ctx) + i := ctx.Int("path-index") + + if len(parts.Path) > 0 { + if i > -1 { + path := strings.Split(parts.Path, "/") + + if i = i + 1; i < len(path) { + fmt.Println(path[i]) + } + } else { + fmt.Println(parts.Path) + } + } + + return nil +} diff --git a/commands/run.go b/commands/run.go new file mode 100644 index 0000000..05ab789 --- /dev/null +++ b/commands/run.go @@ -0,0 +1,17 @@ +package commands + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +// Run default command and print out full url +func Run(ctx *cli.Context) error { + parts := parseURL(ctx) + + if len(parts.String()) > 0 { + fmt.Println(parts) + } + return nil +} diff --git a/commands/scheme.go b/commands/scheme.go new file mode 100644 index 0000000..1d57e34 --- /dev/null +++ b/commands/scheme.go @@ -0,0 +1,17 @@ +package commands + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +// Scheme prints out the scheme part from the url +func Scheme(ctx *cli.Context) error { + parts := parseURL(ctx) + + if len(parts.Scheme) > 0 { + fmt.Println(parts.Scheme) + } + return nil +} diff --git a/commands/user.go b/commands/user.go new file mode 100644 index 0000000..d3eec56 --- /dev/null +++ b/commands/user.go @@ -0,0 +1,19 @@ +package commands + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +// User prints out the user part from url +func User(ctx *cli.Context) error { + parts := parseURL(ctx) + + if parts.User != nil { + if len(parts.User.Username()) > 0 { + fmt.Println(parts.User.Username()) + } + } + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2dece06 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/xoxys/url-parser + +go 1.13 + +require ( + github.com/sirupsen/logrus v1.4.2 + github.com/urfave/cli v1.22.2 + github.com/urfave/cli/v2 v2.1.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d108ce7 --- /dev/null +++ b/go.sum @@ -0,0 +1,25 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= +github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= +github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=