fix: fallback to stdin automatically if no url provided by flag (#67)
* fix: fallback to stdin automatically if no url provided by flag * cleanup
This commit is contained in:
parent
0af993f5b1
commit
8457c57394
|
@ -53,7 +53,6 @@ COMMANDS:
|
||||||
|
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
--url value source url to parse [$URL_PARSER_URL]
|
--url value source url to parse [$URL_PARSER_URL]
|
||||||
--stdin read url to parse from stdin (default: false) [$URL_PARSER_STDIN]
|
|
||||||
--help, -h show help
|
--help, -h show help
|
||||||
--version, -v print the version
|
--version, -v print the version
|
||||||
```
|
```
|
||||||
|
@ -78,6 +77,10 @@ some-key=somevalue
|
||||||
|
|
||||||
$ url-parser --url https://somedomain.com/?some-key=somevalue query --query-field=some-key
|
$ url-parser --url https://somedomain.com/?some-key=somevalue query --query-field=some-key
|
||||||
somevalue
|
somevalue
|
||||||
|
|
||||||
|
# It is also possible to read the URL from stdin
|
||||||
|
$ echo "https://somedomain.com" | url-parser host
|
||||||
|
somedomain.com
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributors
|
## Contributors
|
||||||
|
|
|
@ -37,12 +37,6 @@ func main() {
|
||||||
EnvVars: []string{"URL_PARSER_URL"},
|
EnvVars: []string{"URL_PARSER_URL"},
|
||||||
Destination: &cfg.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{
|
||||||
{
|
{
|
||||||
|
@ -103,31 +97,19 @@ func main() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Before: func(ctx *cli.Context) error {
|
Before: func(ctx *cli.Context) error {
|
||||||
if cfg.URL == "" && !cfg.Stdin {
|
if cfg.URL == "" {
|
||||||
_ = 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()
|
stat, _ := os.Stdin.Stat()
|
||||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||||
stdin, err := io.ReadAll(os.Stdin)
|
stdin, err := io.ReadAll(os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error: %w: %w", config.ErrEmptyStdin, err)
|
return fmt.Errorf("error: %w: %w", config.ErrReadStdin, err)
|
||||||
}
|
}
|
||||||
cfg.URL = strings.TrimSuffix(string(stdin), "\n")
|
cfg.URL = strings.TrimSuffix(string(stdin), "\n")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.URL == "" {
|
if cfg.URL == "" {
|
||||||
return fmt.Errorf("error: %w", config.ErrEmptyStdin)
|
return fmt.Errorf("error: %w", config.ErrEmptyURL)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -3,16 +3,13 @@ package config
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrRequiredFlagsNotSet = errors.New("either \"url\" or \"stdin\" must be set")
|
ErrEmptyURL = errors.New("no url provided either by \"url\" or \"stdin\"")
|
||||||
ErrExclusiveFlags = errors.New("\"url\" and \"stdin\" are mutually exclusive")
|
ErrReadStdin = errors.New("failed to read \"stdin\"")
|
||||||
ErrEmptyStdin = errors.New("\"stdin\" must not be empty")
|
ErrParseURL = errors.New("failed to parse url")
|
||||||
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