2023-07-19 13:56:43 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2024-11-05 13:35:26 +00:00
|
|
|
"encoding/json"
|
2023-07-19 13:56:43 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/thegeeklab/url-parser/config"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run default command and print out full url.
|
2023-07-19 18:39:30 +00:00
|
|
|
func Run(cfg *config.Config) cli.ActionFunc {
|
2024-02-12 08:09:30 +00:00
|
|
|
return func(_ *cli.Context) error {
|
2024-11-05 13:35:26 +00:00
|
|
|
parts := NewURLParser(cfg.URL, cfg.QueryField, cfg.QuerySplit).parse()
|
2023-07-19 13:56:43 +00:00
|
|
|
|
|
|
|
if len(parts.String()) > 0 {
|
2024-11-05 13:35:26 +00:00
|
|
|
if cfg.JSONOutput {
|
|
|
|
json, _ := json.Marshal(parts)
|
|
|
|
fmt.Println(string(json))
|
|
|
|
} else {
|
|
|
|
fmt.Println(parts)
|
|
|
|
}
|
2023-07-19 13:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 13:35:26 +00:00
|
|
|
|
|
|
|
// AllFlags defines flags for all subcommand.
|
|
|
|
func AllFlags(cfg *config.Config) []cli.Flag {
|
|
|
|
return []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "json",
|
|
|
|
Usage: "output json",
|
|
|
|
EnvVars: []string{"URL_PARSER_JSON"},
|
|
|
|
Destination: &cfg.JSONOutput,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|