refactor: rework project structure to use config struct (#65)

This commit is contained in:
Robert Kaussow 2023-07-19 15:56:43 +02:00 committed by GitHub
parent 3a220af100
commit 3875ad7a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 403 additions and 407 deletions

View File

@ -23,11 +23,6 @@ url-parser --help
Build the binary from source with the following command: Build the binary from source with the following command:
```Shell ```Shell
export GOOS=linux
export GOARCH=amd64
export CGO_ENABLED=0
export GO111MODULE=on
make build make build
``` ```
@ -58,29 +53,29 @@ COMMANDS:
GLOBAL OPTIONS: GLOBAL OPTIONS:
--url value source url to parse [$URL_PARSER_URL] --url value source url to parse [$URL_PARSER_URL]
--help, -h show help (default: false) --help, -h show help
--version, -v print the version (default: false) --version, -v print the version
``` ```
## Examples ## Examples
```Shell ```Shell
$ url-parser host --url https://somedomain.com $ url-parser --url https://somedomain.com host
somedomain.com somedomain.com
$ url-parser user --url https://herloct@somedomain.com $ url-parser --url https://herloct@somedomain.com user
herloct herloct
$ url-parser path --url https://somedomain.com/path/to $ url-parser --url https://somedomain.com/path/to path
/path/to /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 to
$ url-parser query --url https://somedomain.com/?some-key=somevalue $ url-parser --url https://somedomain.com/?some-key=somevalue query
some-key=somevalue 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 somevalue
``` ```

View File

@ -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(),
},
}
}

View File

@ -5,7 +5,8 @@ import (
"os" "os"
"github.com/sirupsen/logrus" "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" "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) fmt.Printf("%s version=%s date=%s\n", c.App.Name, c.App.Version, BuildDate)
} }
app := cli.NewApp() config := &config.Config{}
app.Name = "url-parser"
app.Usage = "Parse URL and shows the part of it." app := &cli.App{
app.Version = BuildVersion Name: "url-parser",
app.Action = command.Run Usage: "Parse URL and shows the part of it.",
app.Flags = globalFlags() Version: BuildVersion,
app.Commands = configCommands() 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 { if err := app.Run(os.Args); err != nil {
logrus.Fatal(err) logrus.Fatal(err)

View File

@ -1,10 +1,14 @@
package command package command
import "testing" import (
"testing"
"github.com/thegeeklab/url-parser/config"
)
type TestParseData struct { type TestParseData struct {
urlString string config *config.Config
expected string expected string
} }
func TestParseURL(t *testing.T) { func TestParseURL(t *testing.T) {
@ -13,8 +17,8 @@ func TestParseURL(t *testing.T) {
tables := []TestParseData{ tables := []TestParseData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: urlString, expected: urlString,
}, },
} }

21
command/fragment.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestFragmentData struct { type TestFragmentData struct {
urlString string config *config.Config
expected string expected string
} }
func TestFragment(t *testing.T) { func TestFragment(t *testing.T) {
@ -19,18 +19,16 @@ func TestFragment(t *testing.T) {
tables := []TestFragmentData{ tables := []TestFragmentData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "some-fragment", expected: "some-fragment",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Fragment(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Fragment(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL fragment `%v`, should be `%v`", result, table.expected) t.Fatalf("URL fragment `%v`, should be `%v`", result, table.expected)

21
command/host.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestHostnameData struct { type TestHostnameData struct {
urlString string config *config.Config
expected string expected string
} }
func TestHost(t *testing.T) { func TestHost(t *testing.T) {
@ -19,18 +19,16 @@ func TestHost(t *testing.T) {
tables := []TestHostnameData{ tables := []TestHostnameData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "host.com", expected: "host.com",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Host(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Host(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL host `%v`, should be `%v`", result, table.expected) t.Fatalf("URL host `%v`, should be `%v`", result, table.expected)

24
command/password.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestPasswordData struct { type TestPasswordData struct {
urlString string config *config.Config
expected string expected string
} }
func TestPassword(t *testing.T) { func TestPassword(t *testing.T) {
@ -19,18 +19,16 @@ func TestPassword(t *testing.T) {
tables := []TestPasswordData{ tables := []TestPasswordData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "pass", expected: "pass",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Password(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Password(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL password `%v`, should be `%v`", result, table.expected) t.Fatalf("URL password `%v`, should be `%v`", result, table.expected)

44
command/path.go Normal file
View 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
}
}

View File

@ -1,18 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestPathData struct { type TestPathData struct {
urlString string config *config.Config
pathIndex int expected string
expected string
} }
func TestPath(t *testing.T) { func TestPath(t *testing.T) {
@ -20,25 +19,20 @@ func TestPath(t *testing.T) {
tables := []TestPathData{ tables := []TestPathData{
{ {
urlString: urlString, config: &config.Config{URL: urlString, PathIndex: -1},
pathIndex: -1, expected: "/path/to",
expected: "/path/to",
}, },
{ {
urlString: urlString, config: &config.Config{URL: urlString, PathIndex: 0},
pathIndex: 0, expected: "path",
expected: "path",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
set.Int("path-index", table.pathIndex, "index")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Path(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Path(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL path `%v`, should be `%v`", result, table.expected) t.Fatalf("URL path `%v`, should be `%v`", result, table.expected)

21
command/port.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestPortData struct { type TestPortData struct {
urlString string config *config.Config
expected string expected string
} }
func TestPort(t *testing.T) { func TestPort(t *testing.T) {
@ -19,18 +19,16 @@ func TestPort(t *testing.T) {
tables := []TestPortData{ tables := []TestPortData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "5432", expected: "5432",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Port(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Port(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL port `%v`, should be `%v`", result, table.expected) t.Fatalf("URL port `%v`, should be `%v`", result, table.expected)

40
command/query.go Normal file
View 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
}
}

View File

@ -1,16 +1,16 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestQueryData struct { type TestQueryData struct {
urlString string config *config.Config
QueryField string QueryField string
expected string expected string
} }
@ -20,24 +20,21 @@ func TestQuery(t *testing.T) {
tables := []TestQueryData{ tables := []TestQueryData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "key=value&other=other%20value", expected: "key=value&other=other%20value",
}, },
{ {
urlString: urlString, config: &config.Config{URL: urlString, QueryField: "other"},
QueryField: "other",
expected: "other value", expected: "other value",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
set.String("query-field", table.QueryField, "index")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Query(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Query(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL query `%v`, should be `%v`", result, table.expected) t.Fatalf("URL query `%v`, should be `%v`", result, table.expected)

21
command/run.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestRunData struct { type TestRunData struct {
urlString string config *config.Config
expected string expected string
} }
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
@ -19,18 +19,16 @@ func TestRun(t *testing.T) {
tables := []TestRunData{ tables := []TestRunData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: urlString, expected: urlString,
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Run(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Run(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL `%v`, should be `%v`", result, table.expected) t.Fatalf("URL `%v`, should be `%v`", result, table.expected)

21
command/scheme.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestSchemeData struct { type TestSchemeData struct {
urlString string config *config.Config
expected string expected string
} }
func TestScheme(t *testing.T) { func TestScheme(t *testing.T) {
@ -19,18 +19,16 @@ func TestScheme(t *testing.T) {
tables := []TestSchemeData{ tables := []TestSchemeData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "postgres", expected: "postgres",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Scheme(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Scheme(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL scheme `%v`, should be `%v`", result, table.expected) t.Fatalf("URL scheme `%v`, should be `%v`", result, table.expected)

23
command/user.go Normal file
View 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
}
}

View File

@ -1,17 +1,17 @@
package command package command
import ( import (
"flag"
"strings" "strings"
"testing" "testing"
"github.com/thegeeklab/url-parser/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer" "github.com/zenizh/go-capturer"
) )
type TestUserData struct { type TestUserData struct {
urlString string config *config.Config
expected string expected string
} }
func TestUser(t *testing.T) { func TestUser(t *testing.T) {
@ -19,18 +19,16 @@ func TestUser(t *testing.T) {
tables := []TestUserData{ tables := []TestUserData{
{ {
urlString: urlString, config: &config.Config{URL: urlString},
expected: "user", expected: "user",
}, },
} }
for _, table := range tables { for _, table := range tables {
app := cli.NewApp() app := cli.NewApp()
set := flag.NewFlagSet("test", 0) ctx := cli.NewContext(app, nil, nil)
set.String("url", table.urlString, "test url")
c := cli.NewContext(app, set, nil) result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = User(table.config)(ctx) }))
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = User(c) }))
if result != table.expected { if result != table.expected {
t.Fatalf("URL user `%v`, should be `%v`", result, table.expected) t.Fatalf("URL user `%v`, should be `%v`", result, table.expected)

7
config/config.go Normal file
View File

@ -0,0 +1,7 @@
package config
type Config struct {
URL string
QueryField string
PathIndex int
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}