mirror of
https://github.com/thegeeklab/url-parser.git
synced 2024-11-17 18:50:42 +00:00
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:
|
||||
--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
|
||||
--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
|
||||
somevalue
|
||||
|
||||
# It is also possible to read the URL from stdin
|
||||
$ echo "https://somedomain.com" | url-parser host
|
||||
somedomain.com
|
||||
```
|
||||
|
||||
## Contributors
|
||||
|
@ -37,12 +37,6 @@ func main() {
|
||||
EnvVars: []string{"URL_PARSER_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{
|
||||
{
|
||||
@ -103,31 +97,19 @@ func main() {
|
||||
},
|
||||
},
|
||||
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 {
|
||||
if cfg.URL == "" {
|
||||
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)
|
||||
return fmt.Errorf("error: %w: %w", config.ErrReadStdin, err)
|
||||
}
|
||||
cfg.URL = strings.TrimSuffix(string(stdin), "\n")
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("error: %w", config.ErrEmptyStdin)
|
||||
}
|
||||
if cfg.URL == "" {
|
||||
return fmt.Errorf("error: %w", config.ErrEmptyURL)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -3,16 +3,13 @@ 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")
|
||||
ErrEmptyURL = errors.New("no url provided either by \"url\" or \"stdin\"")
|
||||
ErrReadStdin = errors.New("failed to read \"stdin\"")
|
||||
ErrParseURL = errors.New("failed to parse url")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
Stdin bool
|
||||
QueryField string
|
||||
PathIndex int
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user