mirror of
https://github.com/thegeeklab/url-parser.git
synced 2024-11-22 05:00:40 +00:00
refactor: rework project structure to use config struct (#65)
This commit is contained in:
parent
3a220af100
commit
3875ad7a2c
21
README.md
21
README.md
@ -23,11 +23,6 @@ url-parser --help
|
||||
Build the binary from source with the following command:
|
||||
|
||||
```Shell
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
export CGO_ENABLED=0
|
||||
export GO111MODULE=on
|
||||
|
||||
make build
|
||||
```
|
||||
|
||||
@ -58,29 +53,29 @@ COMMANDS:
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
--url value source url to parse [$URL_PARSER_URL]
|
||||
--help, -h show help (default: false)
|
||||
--version, -v print the version (default: false)
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
```Shell
|
||||
$ url-parser host --url https://somedomain.com
|
||||
$ url-parser --url https://somedomain.com host
|
||||
somedomain.com
|
||||
|
||||
$ url-parser user --url https://herloct@somedomain.com
|
||||
$ url-parser --url https://herloct@somedomain.com user
|
||||
herloct
|
||||
|
||||
$ url-parser path --url https://somedomain.com/path/to
|
||||
$ url-parser --url https://somedomain.com/path/to path
|
||||
/path/to
|
||||
|
||||
$ url-parser path --path-index=1 --url https://somedomain.com/path/to
|
||||
$ url-parser --url https://somedomain.com/path/to path --path-index=1
|
||||
to
|
||||
|
||||
$ url-parser query --url https://somedomain.com/?some-key=somevalue
|
||||
$ url-parser --url https://somedomain.com/?some-key=somevalue query
|
||||
some-key=somevalue
|
||||
|
||||
$ url-parser query --query-field=some-key --url https://somedomain.com/?some-key=somevalue
|
||||
$ url-parser --url https://somedomain.com/?some-key=somevalue query --query-field=some-key
|
||||
somevalue
|
||||
```
|
||||
|
||||
|
@ -1,84 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/thegeeklab/url-parser/internal/command"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func globalFlags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "url",
|
||||
Usage: "source url to parse",
|
||||
EnvVars: []string{"URL_PARSER_URL"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func configCommands() []*cli.Command {
|
||||
return []*cli.Command{
|
||||
{
|
||||
Name: "all",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Get all parts from url",
|
||||
Action: command.Run,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "scheme",
|
||||
Aliases: []string{"s"},
|
||||
Usage: "Get scheme from url",
|
||||
Action: command.Scheme,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "user",
|
||||
Aliases: []string{"u"},
|
||||
Usage: "Get username from url",
|
||||
Action: command.User,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Aliases: []string{"pw"},
|
||||
Usage: "Get password from url",
|
||||
Action: command.Password,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "path",
|
||||
Aliases: []string{"pt"},
|
||||
Usage: "Get path from url",
|
||||
Action: command.Path,
|
||||
Flags: append(globalFlags(), command.PathFlags()...),
|
||||
},
|
||||
{
|
||||
Name: "host",
|
||||
Aliases: []string{"h"},
|
||||
Usage: "Get hostname from url",
|
||||
Action: command.Host,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "port",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Get port from url",
|
||||
Action: command.Port,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
{
|
||||
Name: "query",
|
||||
Aliases: []string{"q"},
|
||||
Usage: "Get query from url",
|
||||
Action: command.Query,
|
||||
Flags: append(globalFlags(), command.QueryFlags()...),
|
||||
},
|
||||
{
|
||||
Name: "fragment",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "Get fragment from url",
|
||||
Action: command.Fragment,
|
||||
Flags: globalFlags(),
|
||||
},
|
||||
}
|
||||
}
|
@ -5,7 +5,8 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/thegeeklab/url-parser/internal/command"
|
||||
"github.com/thegeeklab/url-parser/command"
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@ -20,13 +21,80 @@ func main() {
|
||||
fmt.Printf("%s version=%s date=%s\n", c.App.Name, c.App.Version, BuildDate)
|
||||
}
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "url-parser"
|
||||
app.Usage = "Parse URL and shows the part of it."
|
||||
app.Version = BuildVersion
|
||||
app.Action = command.Run
|
||||
app.Flags = globalFlags()
|
||||
app.Commands = configCommands()
|
||||
config := &config.Config{}
|
||||
|
||||
app := &cli.App{
|
||||
Name: "url-parser",
|
||||
Usage: "Parse URL and shows the part of it.",
|
||||
Version: BuildVersion,
|
||||
Action: command.Run(config),
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "url",
|
||||
Usage: "source url to parse",
|
||||
EnvVars: []string{"URL_PARSER_URL"},
|
||||
Destination: &config.URL,
|
||||
},
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "all",
|
||||
Aliases: []string{"a"},
|
||||
Usage: "Get all parts from url",
|
||||
Action: command.Run(config),
|
||||
},
|
||||
{
|
||||
Name: "scheme",
|
||||
Aliases: []string{"s"},
|
||||
Usage: "Get scheme from url",
|
||||
Action: command.Scheme(config),
|
||||
},
|
||||
{
|
||||
Name: "user",
|
||||
Aliases: []string{"u"},
|
||||
Usage: "Get username from url",
|
||||
Action: command.User(config),
|
||||
},
|
||||
{
|
||||
Name: "password",
|
||||
Aliases: []string{"pw"},
|
||||
Usage: "Get password from url",
|
||||
Action: command.Password(config),
|
||||
},
|
||||
{
|
||||
Name: "path",
|
||||
Aliases: []string{"pt"},
|
||||
Usage: "Get path from url",
|
||||
Action: command.Path(config),
|
||||
Flags: command.PathFlags(config),
|
||||
},
|
||||
{
|
||||
Name: "host",
|
||||
Aliases: []string{"h"},
|
||||
Usage: "Get hostname from url",
|
||||
Action: command.Host(config),
|
||||
},
|
||||
{
|
||||
Name: "port",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Get port from url",
|
||||
Action: command.Port(config),
|
||||
},
|
||||
{
|
||||
Name: "query",
|
||||
Aliases: []string{"q"},
|
||||
Usage: "Get query from url",
|
||||
Action: command.Query(config),
|
||||
Flags: command.QueryFlags(config),
|
||||
},
|
||||
{
|
||||
Name: "fragment",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "Get fragment from url",
|
||||
Action: command.Fragment(config),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
logrus.Fatal(err)
|
||||
|
@ -1,10 +1,14 @@
|
||||
package command
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
)
|
||||
|
||||
type TestParseData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestParseURL(t *testing.T) {
|
||||
@ -13,8 +17,8 @@ func TestParseURL(t *testing.T) {
|
||||
|
||||
tables := []TestParseData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: urlString,
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: urlString,
|
||||
},
|
||||
}
|
||||
|
21
command/fragment.go
Normal file
21
command/fragment.go
Normal file
@ -0,0 +1,21 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Fragment prints out the fragment part from the url.
|
||||
func Fragment(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Fragment)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestFragmentData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestFragment(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestFragment(t *testing.T) {
|
||||
|
||||
tables := []TestFragmentData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "some-fragment",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "some-fragment",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Fragment(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Fragment(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL fragment `%v`, should be `%v`", result, table.expected)
|
21
command/host.go
Normal file
21
command/host.go
Normal file
@ -0,0 +1,21 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Host prints out the host part from the url.
|
||||
func Host(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Hostname())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestHostnameData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestHost(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestHost(t *testing.T) {
|
||||
|
||||
tables := []TestHostnameData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "host.com",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "host.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Host(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Host(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL host `%v`, should be `%v`", result, table.expected)
|
24
command/password.go
Normal file
24
command/password.go
Normal file
@ -0,0 +1,24 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Password prints out the password part from url.
|
||||
func Password(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if parts.User != nil {
|
||||
pw, _ := parts.User.Password()
|
||||
if len(pw) > 0 {
|
||||
fmt.Println(pw)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestPasswordData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestPassword(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestPassword(t *testing.T) {
|
||||
|
||||
tables := []TestPasswordData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "pass",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "pass",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Password(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Password(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL password `%v`, should be `%v`", result, table.expected)
|
44
command/path.go
Normal file
44
command/path.go
Normal file
@ -0,0 +1,44 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// PathFlags defines flags for path subcommand.
|
||||
func PathFlags(config *config.Config) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "path-index",
|
||||
Usage: "filter parsed path by index",
|
||||
EnvVars: []string{"URL_PARSER_PATH_INDEX"},
|
||||
Value: -1,
|
||||
Destination: &config.PathIndex,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Path prints out the path part from url.
|
||||
func Path(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
i := config.PathIndex
|
||||
|
||||
if len(parts.Path) > 0 {
|
||||
if i > -1 {
|
||||
path := strings.Split(parts.Path, "/")
|
||||
|
||||
if i++; i < len(path) {
|
||||
fmt.Println(path[i])
|
||||
}
|
||||
} else {
|
||||
fmt.Println(parts.Path)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,18 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestPathData struct {
|
||||
urlString string
|
||||
pathIndex int
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestPath(t *testing.T) {
|
||||
@ -20,25 +19,20 @@ func TestPath(t *testing.T) {
|
||||
|
||||
tables := []TestPathData{
|
||||
{
|
||||
urlString: urlString,
|
||||
pathIndex: -1,
|
||||
expected: "/path/to",
|
||||
config: &config.Config{URL: urlString, PathIndex: -1},
|
||||
expected: "/path/to",
|
||||
},
|
||||
{
|
||||
urlString: urlString,
|
||||
pathIndex: 0,
|
||||
expected: "path",
|
||||
config: &config.Config{URL: urlString, PathIndex: 0},
|
||||
expected: "path",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
set.Int("path-index", table.pathIndex, "index")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Path(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Path(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL path `%v`, should be `%v`", result, table.expected)
|
21
command/port.go
Normal file
21
command/port.go
Normal file
@ -0,0 +1,21 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Port prints out the port from the url.
|
||||
func Port(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Port())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestPortData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestPort(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestPort(t *testing.T) {
|
||||
|
||||
tables := []TestPortData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "5432",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "5432",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Port(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Port(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL port `%v`, should be `%v`", result, table.expected)
|
40
command/query.go
Normal file
40
command/query.go
Normal file
@ -0,0 +1,40 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// QueryFlags defines flags for query subcommand.
|
||||
func QueryFlags(config *config.Config) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "query-field",
|
||||
Usage: "filter parsed query string by field name",
|
||||
EnvVars: []string{"URL_PARSER_QUERY_FIELD"},
|
||||
Destination: &config.QueryField,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Query prints out the query part from url.
|
||||
func Query(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
f := config.QueryField
|
||||
|
||||
if len(parts.RawQuery) > 0 {
|
||||
if f != "" {
|
||||
if result := parts.Query().Get(f); result != "" {
|
||||
fmt.Println(result)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(parts.RawQuery)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestQueryData struct {
|
||||
urlString string
|
||||
config *config.Config
|
||||
QueryField string
|
||||
expected string
|
||||
}
|
||||
@ -20,24 +20,21 @@ func TestQuery(t *testing.T) {
|
||||
|
||||
tables := []TestQueryData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "key=value&other=other%20value",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "key=value&other=other%20value",
|
||||
},
|
||||
{
|
||||
urlString: urlString,
|
||||
QueryField: "other",
|
||||
expected: "other value",
|
||||
config: &config.Config{URL: urlString, QueryField: "other"},
|
||||
|
||||
expected: "other value",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
set.String("query-field", table.QueryField, "index")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Query(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Query(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL query `%v`, should be `%v`", result, table.expected)
|
21
command/run.go
Normal file
21
command/run.go
Normal file
@ -0,0 +1,21 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Run default command and print out full url.
|
||||
func Run(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if len(parts.String()) > 0 {
|
||||
fmt.Println(parts)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestRunData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestRun(t *testing.T) {
|
||||
|
||||
tables := []TestRunData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: urlString,
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: urlString,
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Run(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Run(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL `%v`, should be `%v`", result, table.expected)
|
21
command/scheme.go
Normal file
21
command/scheme.go
Normal file
@ -0,0 +1,21 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Scheme prints out the scheme part from the url.
|
||||
func Scheme(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Scheme)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestSchemeData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestScheme(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestScheme(t *testing.T) {
|
||||
|
||||
tables := []TestSchemeData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "postgres",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "postgres",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Scheme(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Scheme(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL scheme `%v`, should be `%v`", result, table.expected)
|
23
command/user.go
Normal file
23
command/user.go
Normal file
@ -0,0 +1,23 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// User prints out the user part from url.
|
||||
func User(config *config.Config) cli.ActionFunc {
|
||||
return func(ctx *cli.Context) error {
|
||||
parts := parseURL(config.URL)
|
||||
|
||||
if parts.User != nil {
|
||||
if len(parts.User.Username()) > 0 {
|
||||
fmt.Println(parts.User.Username())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/url-parser/config"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/zenizh/go-capturer"
|
||||
)
|
||||
|
||||
type TestUserData struct {
|
||||
urlString string
|
||||
expected string
|
||||
config *config.Config
|
||||
expected string
|
||||
}
|
||||
|
||||
func TestUser(t *testing.T) {
|
||||
@ -19,18 +19,16 @@ func TestUser(t *testing.T) {
|
||||
|
||||
tables := []TestUserData{
|
||||
{
|
||||
urlString: urlString,
|
||||
expected: "user",
|
||||
config: &config.Config{URL: urlString},
|
||||
expected: "user",
|
||||
},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
app := cli.NewApp()
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String("url", table.urlString, "test url")
|
||||
ctx := cli.NewContext(app, nil, nil)
|
||||
|
||||
c := cli.NewContext(app, set, nil)
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = User(c) }))
|
||||
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = User(table.config)(ctx) }))
|
||||
|
||||
if result != table.expected {
|
||||
t.Fatalf("URL user `%v`, should be `%v`", result, table.expected)
|
7
config/config.go
Normal file
7
config/config.go
Normal file
@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
QueryField string
|
||||
PathIndex int
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Fragment prints out the fragment part from the url.
|
||||
func Fragment(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Fragment)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Host prints out the host part from the url.
|
||||
func Host(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Hostname())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Password prints out the password part from url.
|
||||
func Password(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if parts.User != nil {
|
||||
pw, _ := parts.User.Password()
|
||||
if len(pw) > 0 {
|
||||
fmt.Println(pw)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package command
|
||||
|
||||
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.String("url"))
|
||||
i := ctx.Int("path-index")
|
||||
|
||||
if len(parts.Path) > 0 {
|
||||
if i > -1 {
|
||||
path := strings.Split(parts.Path, "/")
|
||||
|
||||
if i++; i < len(path) {
|
||||
fmt.Println(path[i])
|
||||
}
|
||||
} else {
|
||||
fmt.Println(parts.Path)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Port prints out the port from the url.
|
||||
func Port(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Port())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// QueryFlags defines flags for query subcommand.
|
||||
func QueryFlags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "query-field",
|
||||
Usage: "filter parsed query string by field name",
|
||||
EnvVars: []string{"URL_PARSER_QUERY_FIELD"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Query prints out the query part from url.
|
||||
func Query(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
f := ctx.String("query-field")
|
||||
|
||||
if len(parts.RawQuery) > 0 {
|
||||
if f != "" {
|
||||
if result := parts.Query().Get(f); result != "" {
|
||||
fmt.Println(result)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(parts.RawQuery)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Run default command and print out full url.
|
||||
func Run(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if len(parts.String()) > 0 {
|
||||
fmt.Println(parts)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package command
|
||||
|
||||
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.String("url"))
|
||||
|
||||
if len(parts.Scheme) > 0 {
|
||||
fmt.Println(parts.Scheme)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// User prints out the user part from url.
|
||||
func User(ctx *cli.Context) error {
|
||||
parts := parseURL(ctx.String("url"))
|
||||
|
||||
if parts.User != nil {
|
||||
if len(parts.User.Username()) > 0 {
|
||||
fmt.Println(parts.User.Username())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user