feat: Add short output flag

Ref https://github.com/mr-karan/doggo/issues/35
pull/41/head v0.5.1
Karan Sharma 2022-05-17 10:34:55 +05:30
parent 7619cbdeb0
commit eec8374e6f
6 changed files with 14 additions and 2 deletions

View File

@ -62,7 +62,7 @@
- [ ] Add tests for CLI Output.
- [ ] Homebrew - Goreleaser
- [ ] Add support for `dig +trace` like functionality.
- [ ] Add `dig +x` short output
- [ ] Add `dig +short` short output
- [x] Add `--strategy` for picking nameservers.
- [ ] Explore `dig.rc` kinda file
- [x] Separate Authority/Answer in JSON output.

1
^
View File

@ -1 +0,0 @@
nameserver 127.0.w0.1

View File

@ -49,6 +49,7 @@ func main() {
// Output Options
f.BoolP("json", "J", false, "Set the output format as JSON")
f.Bool("short", false, "Short output format")
f.Bool("time", false, "Display how long it took for the response to arrive")
f.Bool("color", true, "Show colored output")
f.Bool("debug", false, "Enable debug mode")

View File

@ -56,6 +56,7 @@ var appHelpTextTemplate = `{{ "NAME" | color "" "heading" }}:
{{ "Output Options" | color "" "heading" }}:
{{"-J, --json " | color "yellow" ""}} Format the output as JSON.
{{"--short" | color "yellow" ""}} Short output format. Shows only the response section.
{{"--color " | color "yellow" ""}} Defaults to true. Set --color=false to disable colored output.
{{"--debug " | color "yellow" ""}} Enable debug logging.
{{"--time" | color "yellow" ""}} Shows how long the response took from the server.

View File

@ -21,6 +21,14 @@ func (app *App) outputJSON(rsp []resolvers.Response) {
fmt.Printf("%s", res)
}
func (app *App) outputShort(rsp []resolvers.Response) {
for _, r := range rsp {
for _, a := range r.Answers {
fmt.Printf("%s\n", a.Address)
}
}
}
func (app *App) outputTerminal(rsp []resolvers.Response) {
var (
green = color.New(color.FgGreen, color.Bold).SprintFunc()
@ -134,6 +142,8 @@ func (app *App) outputTerminal(rsp []resolvers.Response) {
func (app *App) Output(responses []resolvers.Response) {
if app.QueryFlags.ShowJSON {
app.outputJSON(responses)
} else if app.QueryFlags.ShortOutput {
app.outputShort(responses)
} else {
app.outputTerminal(responses)
}

View File

@ -33,6 +33,7 @@ type QueryFlags struct {
Color bool `koanf:"color" json:"-"`
DisplayTimeTaken bool `koanf:"time" json:"-"`
ShowJSON bool `koanf:"json" json:"-"`
ShortOutput bool `koanf:"short" short:"-"`
UseSearchList bool `koanf:"search" json:"-"`
ReverseLookup bool `koanf:"reverse" reverse:"-"`
Strategy string `koanf:"strategy" strategy:"-"`