From 3875ad7a2cae4bfacc721e1d9a85bf4e3ba5abba Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 19 Jul 2023 15:56:43 +0200 Subject: [PATCH] refactor: rework project structure to use config struct (#65) --- README.md | 21 ++--- cmd/url-parser/config.go | 84 ------------------- cmd/url-parser/main.go | 84 +++++++++++++++++-- {internal/command => command}/commands.go | 0 .../command => command}/commands_test.go | 14 ++-- command/fragment.go | 21 +++++ .../command => command}/fragment_test.go | 16 ++-- command/host.go | 21 +++++ {internal/command => command}/host_test.go | 16 ++-- command/password.go | 24 ++++++ .../command => command}/password_test.go | 16 ++-- command/path.go | 44 ++++++++++ {internal/command => command}/path_test.go | 24 ++---- command/port.go | 21 +++++ {internal/command => command}/port_test.go | 16 ++-- command/query.go | 40 +++++++++ {internal/command => command}/query_test.go | 21 ++--- command/run.go | 21 +++++ {internal/command => command}/run_test.go | 16 ++-- command/scheme.go | 21 +++++ {internal/command => command}/scheme_test.go | 16 ++-- command/user.go | 23 +++++ {internal/command => command}/user_test.go | 16 ++-- config/config.go | 7 ++ internal/command/fragment.go | 18 ---- internal/command/host.go | 18 ---- internal/command/password.go | 21 ----- internal/command/path.go | 40 --------- internal/command/port.go | 18 ---- internal/command/query.go | 36 -------- internal/command/run.go | 18 ---- internal/command/scheme.go | 18 ---- internal/command/user.go | 20 ----- 33 files changed, 403 insertions(+), 407 deletions(-) delete mode 100644 cmd/url-parser/config.go rename {internal/command => command}/commands.go (100%) rename {internal/command => command}/commands_test.go (70%) create mode 100644 command/fragment.go rename {internal/command => command}/fragment_test.go (70%) create mode 100644 command/host.go rename {internal/command => command}/host_test.go (70%) create mode 100644 command/password.go rename {internal/command => command}/password_test.go (70%) create mode 100644 command/path.go rename {internal/command => command}/path_test.go (59%) create mode 100644 command/port.go rename {internal/command => command}/port_test.go (70%) create mode 100644 command/query.go rename {internal/command => command}/query_test.go (61%) create mode 100644 command/run.go rename {internal/command => command}/run_test.go (70%) create mode 100644 command/scheme.go rename {internal/command => command}/scheme_test.go (70%) create mode 100644 command/user.go rename {internal/command => command}/user_test.go (70%) create mode 100644 config/config.go delete mode 100644 internal/command/fragment.go delete mode 100644 internal/command/host.go delete mode 100644 internal/command/password.go delete mode 100644 internal/command/path.go delete mode 100644 internal/command/port.go delete mode 100644 internal/command/query.go delete mode 100644 internal/command/run.go delete mode 100644 internal/command/scheme.go delete mode 100644 internal/command/user.go diff --git a/README.md b/README.md index 4498154..10b7fe5 100644 --- a/README.md +++ b/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 ``` diff --git a/cmd/url-parser/config.go b/cmd/url-parser/config.go deleted file mode 100644 index edbfea1..0000000 --- a/cmd/url-parser/config.go +++ /dev/null @@ -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(), - }, - } -} diff --git a/cmd/url-parser/main.go b/cmd/url-parser/main.go index ea85eec..c775c79 100644 --- a/cmd/url-parser/main.go +++ b/cmd/url-parser/main.go @@ -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) diff --git a/internal/command/commands.go b/command/commands.go similarity index 100% rename from internal/command/commands.go rename to command/commands.go diff --git a/internal/command/commands_test.go b/command/commands_test.go similarity index 70% rename from internal/command/commands_test.go rename to command/commands_test.go index 7d20ed7..867ab4e 100644 --- a/internal/command/commands_test.go +++ b/command/commands_test.go @@ -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, }, } diff --git a/command/fragment.go b/command/fragment.go new file mode 100644 index 0000000..49720fb --- /dev/null +++ b/command/fragment.go @@ -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 + } +} diff --git a/internal/command/fragment_test.go b/command/fragment_test.go similarity index 70% rename from internal/command/fragment_test.go rename to command/fragment_test.go index 18534c7..3a5a8a3 100644 --- a/internal/command/fragment_test.go +++ b/command/fragment_test.go @@ -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) diff --git a/command/host.go b/command/host.go new file mode 100644 index 0000000..9950c4e --- /dev/null +++ b/command/host.go @@ -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 + } +} diff --git a/internal/command/host_test.go b/command/host_test.go similarity index 70% rename from internal/command/host_test.go rename to command/host_test.go index d9d46f9..5f8b950 100644 --- a/internal/command/host_test.go +++ b/command/host_test.go @@ -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) diff --git a/command/password.go b/command/password.go new file mode 100644 index 0000000..5c03ed3 --- /dev/null +++ b/command/password.go @@ -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 + } +} diff --git a/internal/command/password_test.go b/command/password_test.go similarity index 70% rename from internal/command/password_test.go rename to command/password_test.go index 2abe96d..3a58b23 100644 --- a/internal/command/password_test.go +++ b/command/password_test.go @@ -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) diff --git a/command/path.go b/command/path.go new file mode 100644 index 0000000..2ffa421 --- /dev/null +++ b/command/path.go @@ -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 + } +} diff --git a/internal/command/path_test.go b/command/path_test.go similarity index 59% rename from internal/command/path_test.go rename to command/path_test.go index 8f76211..3ad0a1e 100644 --- a/internal/command/path_test.go +++ b/command/path_test.go @@ -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) diff --git a/command/port.go b/command/port.go new file mode 100644 index 0000000..5f76d6a --- /dev/null +++ b/command/port.go @@ -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 + } +} diff --git a/internal/command/port_test.go b/command/port_test.go similarity index 70% rename from internal/command/port_test.go rename to command/port_test.go index d2e0932..130b45b 100644 --- a/internal/command/port_test.go +++ b/command/port_test.go @@ -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) diff --git a/command/query.go b/command/query.go new file mode 100644 index 0000000..ab1db68 --- /dev/null +++ b/command/query.go @@ -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 + } +} diff --git a/internal/command/query_test.go b/command/query_test.go similarity index 61% rename from internal/command/query_test.go rename to command/query_test.go index 33d7ca3..2c72320 100644 --- a/internal/command/query_test.go +++ b/command/query_test.go @@ -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) diff --git a/command/run.go b/command/run.go new file mode 100644 index 0000000..75d3438 --- /dev/null +++ b/command/run.go @@ -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 + } +} diff --git a/internal/command/run_test.go b/command/run_test.go similarity index 70% rename from internal/command/run_test.go rename to command/run_test.go index 490b53d..300f391 100644 --- a/internal/command/run_test.go +++ b/command/run_test.go @@ -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) diff --git a/command/scheme.go b/command/scheme.go new file mode 100644 index 0000000..7f4451f --- /dev/null +++ b/command/scheme.go @@ -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 + } +} diff --git a/internal/command/scheme_test.go b/command/scheme_test.go similarity index 70% rename from internal/command/scheme_test.go rename to command/scheme_test.go index 5d00b41..6c40c02 100644 --- a/internal/command/scheme_test.go +++ b/command/scheme_test.go @@ -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) diff --git a/command/user.go b/command/user.go new file mode 100644 index 0000000..b564a9a --- /dev/null +++ b/command/user.go @@ -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 + } +} diff --git a/internal/command/user_test.go b/command/user_test.go similarity index 70% rename from internal/command/user_test.go rename to command/user_test.go index e28a1c7..af2104e 100644 --- a/internal/command/user_test.go +++ b/command/user_test.go @@ -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) diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..e81b56f --- /dev/null +++ b/config/config.go @@ -0,0 +1,7 @@ +package config + +type Config struct { + URL string + QueryField string + PathIndex int +} diff --git a/internal/command/fragment.go b/internal/command/fragment.go deleted file mode 100644 index 3a4f8f8..0000000 --- a/internal/command/fragment.go +++ /dev/null @@ -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 -} diff --git a/internal/command/host.go b/internal/command/host.go deleted file mode 100644 index 285ed53..0000000 --- a/internal/command/host.go +++ /dev/null @@ -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 -} diff --git a/internal/command/password.go b/internal/command/password.go deleted file mode 100644 index 6118134..0000000 --- a/internal/command/password.go +++ /dev/null @@ -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 -} diff --git a/internal/command/path.go b/internal/command/path.go deleted file mode 100644 index f802536..0000000 --- a/internal/command/path.go +++ /dev/null @@ -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 -} diff --git a/internal/command/port.go b/internal/command/port.go deleted file mode 100644 index ea1f38e..0000000 --- a/internal/command/port.go +++ /dev/null @@ -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 -} diff --git a/internal/command/query.go b/internal/command/query.go deleted file mode 100644 index bc5fe2e..0000000 --- a/internal/command/query.go +++ /dev/null @@ -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 -} diff --git a/internal/command/run.go b/internal/command/run.go deleted file mode 100644 index 8dfea5e..0000000 --- a/internal/command/run.go +++ /dev/null @@ -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 -} diff --git a/internal/command/scheme.go b/internal/command/scheme.go deleted file mode 100644 index 9bf0bab..0000000 --- a/internal/command/scheme.go +++ /dev/null @@ -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 -} diff --git a/internal/command/user.go b/internal/command/user.go deleted file mode 100644 index 44153a8..0000000 --- a/internal/command/user.go +++ /dev/null @@ -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 -}