Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
feat: add help text via --help, version via --version (#6)
Browse files Browse the repository at this point in the history
* feat: add help text via --help

* feat: add --version flag
  • Loading branch information
mcataford authored Apr 27, 2023
1 parent 03a0440 commit 6122e39
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,33 @@ type Config struct {
tracePath string
verbosity int
depthLimit int
help bool
version bool
}

var rVersionFlag = regexp.MustCompile("^--version$")
var rHelpFlag = regexp.MustCompile("^--help$")
var rVerboseFlag = regexp.MustCompile("^-(?P<verbosity>(v{1,2}))$")
var rDepthPattern = regexp.MustCompile("--depth=(?P<depth>([1-9][0-9]*|0))")

var helpText = fmt.Sprintf(`
Doggo (version %s)
🐕🔎 Inspecting big Datadog traces in the CLI
Usage:
doggo <trace_path> <resource_name or partial string> [-vv] [--depth=<depthLimit>]
Given a trace_path leading to tracedata json and a query (either a resource name or a partial resource name), displays the spans and children of the sections of the trace that match.
By default, timing information for each span is shown.
-v adds more resource-related information for each span;
-vv adds even more resource-related information, including tags, for each span;
--depth=<depthLimit> allows limiting how many levels down each match the display should go.
`, Version)

// Prints traces recursively with increasing ident levels.
// More data is printed until there are no more traces available
// i.e. until the ChildrenIds key yields an empty array.
Expand Down Expand Up @@ -143,10 +165,22 @@ func parseTraceJsonFromFile(path string) TraceData {
// Parses command-line arguments to extract flags and
// other useful contextual details.
func parseArgs(args []string) Config {
rootOfInterest := os.Args[2]
tracePath := os.Args[1]
rootOfInterest := ""

if len(os.Args) >= 3 {
rootOfInterest = os.Args[2]
}

tracePath := ""

if len(os.Args) >= 2 {
tracePath = os.Args[1]
}

verbosity := 0
depth := 9999
help := false
version := false

verbosityMatchIndex := rVerboseFlag.SubexpIndex("verbosity")
for _, arg := range os.Args {
Expand All @@ -173,15 +207,33 @@ func parseArgs(args []string) Config {
depth = depthLimit
continue
}

if rHelpFlag.MatchString(arg) {
help = true
}

if rVersionFlag.MatchString(arg) {
version = true
}

}

return Config{rootOfInterest, tracePath, verbosity, depth}
return Config{rootOfInterest, tracePath, verbosity, depth, help, version}
}

func main() {
log.Println("Doggo version: ", Version)
config := parseArgs(os.Args)

if config.help {
fmt.Println(helpText)
return
}

if config.version {
fmt.Println(Version)
return
}

fullTrace := parseTraceJsonFromFile(config.tracePath)

spansById, spanIdsByResourceName := buildSpanIndexes(fullTrace)
Expand Down

0 comments on commit 6122e39

Please sign in to comment.