From 6122e39b55b590a6faaa0d043cd7a563ab9d20e6 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Thu, 27 Apr 2023 01:10:39 -0400 Subject: [PATCH] feat: add help text via --help, version via --version (#6) * feat: add help text via --help * feat: add --version flag --- main.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5cbd548..43e52ab 100644 --- a/main.go +++ b/main.go @@ -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(v{1,2}))$") var rDepthPattern = regexp.MustCompile("--depth=(?P([1-9][0-9]*|0))") +var helpText = fmt.Sprintf(` +Doggo (version %s) + +🐕🔎 Inspecting big Datadog traces in the CLI + +Usage: + +doggo [-vv] [--depth=] + +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= 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. @@ -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 { @@ -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)