mirror of
https://github.com/thegeeklab/url-parser.git
synced 2024-11-26 06:50:39 +00:00
add missing commands
This commit is contained in:
parent
92635b4f33
commit
3dc3ac4e7a
16
README.md
16
README.md
@ -28,14 +28,18 @@ USAGE:
|
|||||||
url-parser [global options] command [command options] [arguments...]
|
url-parser [global options] command [command options] [arguments...]
|
||||||
|
|
||||||
VERSION:
|
VERSION:
|
||||||
0.1.0
|
devel
|
||||||
|
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
all, a print out all parts from url
|
all, a Get all parts from url
|
||||||
scheme, s print out scheme from url
|
scheme, s Get scheme from url
|
||||||
user, u print out username from url
|
user, u Get username from url
|
||||||
password, pw print out password from url
|
password, pw Get password from url
|
||||||
path, p print out the path from url
|
path, pt Get path from url
|
||||||
|
host, h Get hostname from url
|
||||||
|
port, p Get port from url
|
||||||
|
query, q Get query from url
|
||||||
|
fragment, f Get fragment from url
|
||||||
help, h Shows a list of commands or help for one command
|
help, h Shows a list of commands or help for one command
|
||||||
|
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
|
@ -20,37 +20,65 @@ func configCommands() []*cli.Command {
|
|||||||
{
|
{
|
||||||
Name: "all",
|
Name: "all",
|
||||||
Aliases: []string{"a"},
|
Aliases: []string{"a"},
|
||||||
Usage: "print out all parts from url",
|
Usage: "Get all parts from url",
|
||||||
Action: commands.Run,
|
Action: commands.Run,
|
||||||
Flags: globalFlags(),
|
Flags: globalFlags(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "scheme",
|
Name: "scheme",
|
||||||
Aliases: []string{"s"},
|
Aliases: []string{"s"},
|
||||||
Usage: "print out scheme from url",
|
Usage: "Get scheme from url",
|
||||||
Action: commands.Scheme,
|
Action: commands.Scheme,
|
||||||
Flags: globalFlags(),
|
Flags: globalFlags(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "user",
|
Name: "user",
|
||||||
Aliases: []string{"u"},
|
Aliases: []string{"u"},
|
||||||
Usage: "print out username from url",
|
Usage: "Get username from url",
|
||||||
Action: commands.User,
|
Action: commands.User,
|
||||||
Flags: globalFlags(),
|
Flags: globalFlags(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "password",
|
Name: "password",
|
||||||
Aliases: []string{"p"},
|
Aliases: []string{"pw"},
|
||||||
Usage: "print out password from url",
|
Usage: "Get password from url",
|
||||||
Action: commands.Password,
|
Action: commands.Password,
|
||||||
Flags: globalFlags(),
|
Flags: globalFlags(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "path",
|
Name: "path",
|
||||||
Aliases: []string{"pt"},
|
Aliases: []string{"pt"},
|
||||||
Usage: "print out the path from url",
|
Usage: "Get path from url",
|
||||||
Action: commands.Path,
|
Action: commands.Path,
|
||||||
Flags: append(globalFlags(), commands.PathFlags()...),
|
Flags: append(globalFlags(), commands.PathFlags()...),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "host",
|
||||||
|
Aliases: []string{"h"},
|
||||||
|
Usage: "Get hostname from url",
|
||||||
|
Action: commands.Host,
|
||||||
|
Flags: globalFlags(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "port",
|
||||||
|
Aliases: []string{"p"},
|
||||||
|
Usage: "Get port from url",
|
||||||
|
Action: commands.Port,
|
||||||
|
Flags: globalFlags(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "query",
|
||||||
|
Aliases: []string{"q"},
|
||||||
|
Usage: "Get query from url",
|
||||||
|
Action: commands.Query,
|
||||||
|
Flags: append(globalFlags(), commands.QueryFlags()...),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "fragment",
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Usage: "Get fragment from url",
|
||||||
|
Action: commands.Fragment,
|
||||||
|
Flags: globalFlags(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
commands/fragment.go
Normal file
17
commands/fragment.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
39
commands/fragment_test.go
Normal file
39
commands/fragment_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kami-zh/go-capturer"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestFragmentData struct {
|
||||||
|
urlString string
|
||||||
|
expected string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFragment(t *testing.T) {
|
||||||
|
urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment"
|
||||||
|
|
||||||
|
tables := []TestFragmentData{
|
||||||
|
{
|
||||||
|
urlString: urlString,
|
||||||
|
expected: "some-fragment",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, table := range tables {
|
||||||
|
app := cli.NewApp()
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.String("url", table.urlString, "test url")
|
||||||
|
|
||||||
|
c := cli.NewContext(app, set, nil)
|
||||||
|
result := strings.TrimSpace(capturer.CaptureStdout(func() { Fragment(c) }))
|
||||||
|
|
||||||
|
if result != table.expected {
|
||||||
|
t.Fatalf("URL fragment `%v`, should be `%v`", result, table.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
commands/host.go
Normal file
17
commands/host.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
39
commands/host_test.go
Normal file
39
commands/host_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kami-zh/go-capturer"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestHostnameData struct {
|
||||||
|
urlString string
|
||||||
|
expected string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHost(t *testing.T) {
|
||||||
|
urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment"
|
||||||
|
|
||||||
|
tables := []TestHostnameData{
|
||||||
|
{
|
||||||
|
urlString: urlString,
|
||||||
|
expected: "host.com",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, table := range tables {
|
||||||
|
app := cli.NewApp()
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.String("url", table.urlString, "test url")
|
||||||
|
|
||||||
|
c := cli.NewContext(app, set, nil)
|
||||||
|
result := strings.TrimSpace(capturer.CaptureStdout(func() { Host(c) }))
|
||||||
|
|
||||||
|
if result != table.expected {
|
||||||
|
t.Fatalf("URL host `%v`, should be `%v`", result, table.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
commands/port.go
Normal file
17
commands/port.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
39
commands/port_test.go
Normal file
39
commands/port_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kami-zh/go-capturer"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestPortData struct {
|
||||||
|
urlString string
|
||||||
|
expected string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPort(t *testing.T) {
|
||||||
|
urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment"
|
||||||
|
|
||||||
|
tables := []TestPortData{
|
||||||
|
{
|
||||||
|
urlString: urlString,
|
||||||
|
expected: "5432",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, table := range tables {
|
||||||
|
app := cli.NewApp()
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.String("url", table.urlString, "test url")
|
||||||
|
|
||||||
|
c := cli.NewContext(app, set, nil)
|
||||||
|
result := strings.TrimSpace(capturer.CaptureStdout(func() { Port(c) }))
|
||||||
|
|
||||||
|
if result != table.expected {
|
||||||
|
t.Fatalf("URL port `%v`, should be `%v`", result, table.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
commands/query.go
Normal file
36
commands/query.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user