mirror of
https://github.com/thegeeklab/url-parser.git
synced 2024-07-05 08:31:00 +02:00
feat: add option to read url from stdin
This commit is contained in:
parent
db299322bc
commit
55b7c5fdef
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/thegeeklab/url-parser/command"
|
"github.com/thegeeklab/url-parser/command"
|
||||||
|
@ -21,19 +23,25 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &config.Config{}
|
cfg := &config.Config{}
|
||||||
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Name: "url-parser",
|
Name: "url-parser",
|
||||||
Usage: "Parse URL and shows the part of it.",
|
Usage: "Parse URL and shows the part of it.",
|
||||||
Version: BuildVersion,
|
Version: BuildVersion,
|
||||||
Action: command.Run(config),
|
Action: command.Run(cfg),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "url",
|
Name: "url",
|
||||||
Usage: "source url to parse",
|
Usage: "source url to parse",
|
||||||
EnvVars: []string{"URL_PARSER_URL"},
|
EnvVars: []string{"URL_PARSER_URL"},
|
||||||
Destination: &config.URL,
|
Destination: &cfg.URL,
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "stdin",
|
||||||
|
Usage: "read url to parse from stdin",
|
||||||
|
EnvVars: []string{"URL_PARSER_STDIN"},
|
||||||
|
Destination: &cfg.Stdin,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
|
@ -41,59 +49,89 @@ func main() {
|
||||||
Name: "all",
|
Name: "all",
|
||||||
Aliases: []string{"a"},
|
Aliases: []string{"a"},
|
||||||
Usage: "Get all parts from url",
|
Usage: "Get all parts from url",
|
||||||
Action: command.Run(config),
|
Action: command.Run(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "scheme",
|
Name: "scheme",
|
||||||
Aliases: []string{"s"},
|
Aliases: []string{"s"},
|
||||||
Usage: "Get scheme from url",
|
Usage: "Get scheme from url",
|
||||||
Action: command.Scheme(config),
|
Action: command.Scheme(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "user",
|
Name: "user",
|
||||||
Aliases: []string{"u"},
|
Aliases: []string{"u"},
|
||||||
Usage: "Get username from url",
|
Usage: "Get username from url",
|
||||||
Action: command.User(config),
|
Action: command.User(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "password",
|
Name: "password",
|
||||||
Aliases: []string{"pw"},
|
Aliases: []string{"pw"},
|
||||||
Usage: "Get password from url",
|
Usage: "Get password from url",
|
||||||
Action: command.Password(config),
|
Action: command.Password(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "path",
|
Name: "path",
|
||||||
Aliases: []string{"pt"},
|
Aliases: []string{"pt"},
|
||||||
Usage: "Get path from url",
|
Usage: "Get path from url",
|
||||||
Action: command.Path(config),
|
Action: command.Path(cfg),
|
||||||
Flags: command.PathFlags(config),
|
Flags: command.PathFlags(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "host",
|
Name: "host",
|
||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Get hostname from url",
|
Usage: "Get hostname from url",
|
||||||
Action: command.Host(config),
|
Action: command.Host(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "port",
|
Name: "port",
|
||||||
Aliases: []string{"p"},
|
Aliases: []string{"p"},
|
||||||
Usage: "Get port from url",
|
Usage: "Get port from url",
|
||||||
Action: command.Port(config),
|
Action: command.Port(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "query",
|
Name: "query",
|
||||||
Aliases: []string{"q"},
|
Aliases: []string{"q"},
|
||||||
Usage: "Get query from url",
|
Usage: "Get query from url",
|
||||||
Action: command.Query(config),
|
Action: command.Query(cfg),
|
||||||
Flags: command.QueryFlags(config),
|
Flags: command.QueryFlags(cfg),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "fragment",
|
Name: "fragment",
|
||||||
Aliases: []string{"f"},
|
Aliases: []string{"f"},
|
||||||
Usage: "Get fragment from url",
|
Usage: "Get fragment from url",
|
||||||
Action: command.Fragment(config),
|
Action: command.Fragment(cfg),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Before: func(ctx *cli.Context) error {
|
||||||
|
if cfg.URL == "" && !cfg.Stdin {
|
||||||
|
_ = cli.ShowAppHelp(ctx)
|
||||||
|
|
||||||
|
return fmt.Errorf("error: %w", config.ErrRequiredFlagsNotSet)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.URL != "" && cfg.Stdin {
|
||||||
|
_ = cli.ShowAppHelp(ctx)
|
||||||
|
|
||||||
|
return fmt.Errorf("error: %w", config.ErrExclusiveFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Stdin {
|
||||||
|
stat, _ := os.Stdin.Stat()
|
||||||
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||||
|
stdin, err := io.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error: %w: %w", config.ErrEmptyStdin, err)
|
||||||
|
}
|
||||||
|
cfg.URL = strings.TrimSuffix(string(stdin), "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.URL == "" {
|
||||||
|
return fmt.Errorf("error: %w", config.ErrEmptyStdin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/thegeeklab/url-parser/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseURL(raw string) *url.URL {
|
func parseURL(raw string) *url.URL {
|
||||||
|
@ -12,7 +14,7 @@ func parseURL(raw string) *url.URL {
|
||||||
|
|
||||||
url, err := url.Parse(urlString)
|
url, err := url.Parse(urlString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(fmt.Errorf("%w: %w", config.ErrParseURL, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return url
|
return url
|
||||||
|
|
|
@ -1,7 +1,18 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrRequiredFlagsNotSet = errors.New("either \"url\" or \"stdin\" must be set")
|
||||||
|
ErrExclusiveFlags = errors.New("\"url\" and \"stdin\" are mutually exclusive")
|
||||||
|
ErrEmptyStdin = errors.New("\"stdin\" must not be empty")
|
||||||
|
ErrReadStdin = errors.New("failed to read \"stdin\"")
|
||||||
|
ErrParseURL = errors.New("failed to parse url")
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
URL string
|
URL string
|
||||||
|
Stdin bool
|
||||||
QueryField string
|
QueryField string
|
||||||
PathIndex int
|
PathIndex int
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user