refactor: move regexp compile to top, reduce redundant logic (#1)

This commit is contained in:
Marc 2023-04-23 20:03:29 -04:00 committed by GitHub
parent 953f365c24
commit 8df06fcefd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

46
main.go
View file

@ -6,8 +6,8 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"strings"
"strconv" "strconv"
"strings"
) )
type Span struct { type Span struct {
@ -50,11 +50,16 @@ type Config struct {
depthLimit int depthLimit int
} }
var rVerboseFlag = regexp.MustCompile("^-(?P<verbosity>(v{1,2}))$")
var rDepthPattern = regexp.MustCompile("--depth=(?P<depth>([1-9][0-9]*|0))")
// 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.
func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config Config, depth int) { func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config Config, depth int) {
if depth > config.depthLimit { return } if depth > config.depthLimit {
return
}
currentSpan := idToSpanMap[current] currentSpan := idToSpanMap[current]
prefix := strings.Repeat(" ", depth) prefix := strings.Repeat(" ", depth)
@ -68,7 +73,9 @@ func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config
if config.verbosity > 1 { if config.verbosity > 1 {
meta, err := json.MarshalIndent(currentSpan.Meta, "", " ") meta, err := json.MarshalIndent(currentSpan.Meta, "", " ")
if err != nil { panic(err) } if err != nil {
panic(err)
}
log.Println(fmt.Sprintf("%s %s", prefix, meta)) log.Println(fmt.Sprintf("%s %s", prefix, meta))
} }
@ -123,30 +130,24 @@ func parseTraceJsonFromFile(path string) TraceData {
return fullTrace return fullTrace
} }
// Parses command-line arguments to extract flags and
// other useful contextual details.
func parseArgs(args []string) Config { func parseArgs(args []string) Config {
collectedArgs := map[string]bool{} rootOfInterest := os.Args[2]
tracePath := os.Args[1]
verbosity := 0
depth := 9999
for _, arg := range os.Args { for _, arg := range os.Args {
collectedArgs[arg] = true verbosity_match := rVerboseFlag.FindAllString(arg, -1)
if verbosity_match != nil {
verbosity = len(verbosity_match[0])
continue
} }
verbosity := 0
_, verbose := collectedArgs["-v"]
if verbose { verbosity = 1 }
_, veryVerbose := collectedArgs["-vv"]
if veryVerbose { verbosity = 2 }
depthPattern := "--depth=(?P<depth>([1-9][0-9]*|0))"
rDepthPattern := regexp.MustCompile(depthPattern)
depthMatch := rDepthPattern.FindStringSubmatch(strings.Join(os.Args, " ")) depthMatch := rDepthPattern.FindStringSubmatch(strings.Join(os.Args, " "))
depth := 9999
if depthMatch != nil { if depthMatch != nil {
depthValueIndex := rDepthPattern.SubexpIndex("depth") depthValueIndex := rDepthPattern.SubexpIndex("depth")
@ -159,10 +160,9 @@ func parseArgs(args []string) Config {
} }
depth = depthLimit depth = depthLimit
continue
}
} }
rootOfInterest := os.Args[2]
tracePath := os.Args[1]
return Config{rootOfInterest, tracePath, verbosity, depth} return Config{rootOfInterest, tracePath, verbosity, depth}
} }