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:
parent
03a0440cbf
commit
6122e39b55
1 changed files with 56 additions and 4 deletions
60
main.go
60
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<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.
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
return Config{rootOfInterest, tracePath, verbosity, depth}
|
||||
if rVersionFlag.MatchString(arg) {
|
||||
version = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
Reference in a new issue