feat: add help text via --help, version via --version (#6)

* feat: add help text via --help

* feat: add --version flag
This commit is contained in:
Marc 2023-04-27 01:10:39 -04:00 committed by GitHub
parent 03a0440cbf
commit 6122e39b55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

60
main.go
View file

@ -50,11 +50,33 @@ type Config struct {
tracePath string tracePath string
verbosity int verbosity int
depthLimit 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 rVerboseFlag = regexp.MustCompile("^-(?P<verbosity>(v{1,2}))$")
var rDepthPattern = regexp.MustCompile("--depth=(?P<depth>([1-9][0-9]*|0))") 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. // Prints traces recursively with increasing ident levels.
// More data is printed until there are no more traces available // More data is printed until there are no more traces available
// i.e. until the ChildrenIds key yields an empty array. // 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 // Parses command-line arguments to extract flags and
// other useful contextual details. // other useful contextual details.
func parseArgs(args []string) Config { func parseArgs(args []string) Config {
rootOfInterest := os.Args[2] rootOfInterest := ""
tracePath := os.Args[1]
if len(os.Args) >= 3 {
rootOfInterest = os.Args[2]
}
tracePath := ""
if len(os.Args) >= 2 {
tracePath = os.Args[1]
}
verbosity := 0 verbosity := 0
depth := 9999 depth := 9999
help := false
version := false
verbosityMatchIndex := rVerboseFlag.SubexpIndex("verbosity") verbosityMatchIndex := rVerboseFlag.SubexpIndex("verbosity")
for _, arg := range os.Args { for _, arg := range os.Args {
@ -173,15 +207,33 @@ func parseArgs(args []string) Config {
depth = depthLimit depth = depthLimit
continue 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() { func main() {
log.Println("Doggo version: ", Version)
config := parseArgs(os.Args) config := parseArgs(os.Args)
if config.help {
fmt.Println(helpText)
return
}
if config.version {
fmt.Println(Version)
return
}
fullTrace := parseTraceJsonFromFile(config.tracePath) fullTrace := parseTraceJsonFromFile(config.tracePath)
spansById, spanIdsByResourceName := buildSpanIndexes(fullTrace) spansById, spanIdsByResourceName := buildSpanIndexes(fullTrace)