From eec8374e6f04745a19430fe4cc87ec46e7a9a522 Mon Sep 17 00:00:00 2001 From: Karan Sharma Date: Tue, 17 May 2022 10:34:55 +0530 Subject: [PATCH] feat: Add short output flag Ref https://github.com/mr-karan/doggo/issues/35 --- TODO.md | 2 +- ^ | 1 - cmd/doggo/cli.go | 1 + cmd/doggo/help.go | 1 + internal/app/output.go | 10 ++++++++++ pkg/models/models.go | 1 + 6 files changed, 14 insertions(+), 2 deletions(-) delete mode 100644 ^ diff --git a/TODO.md b/TODO.md index c14727a..30cca2f 100644 --- a/TODO.md +++ b/TODO.md @@ -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. diff --git a/^ b/^ deleted file mode 100644 index 18b75d6..0000000 --- a/^ +++ /dev/null @@ -1 +0,0 @@ -nameserver 127.0.w0.1 diff --git a/cmd/doggo/cli.go b/cmd/doggo/cli.go index 9ca5afb..b6152a7 100644 --- a/cmd/doggo/cli.go +++ b/cmd/doggo/cli.go @@ -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") diff --git a/cmd/doggo/help.go b/cmd/doggo/help.go index 3b23ba4..50cdd01 100644 --- a/cmd/doggo/help.go +++ b/cmd/doggo/help.go @@ -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. diff --git a/internal/app/output.go b/internal/app/output.go index a4332b3..1c3415a 100644 --- a/internal/app/output.go +++ b/internal/app/output.go @@ -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) } diff --git a/pkg/models/models.go b/pkg/models/models.go index 6d340c2..758d72b 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -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:"-"`