refactor: move regexp compile to top, reduce redundant logic (#1)
This commit is contained in:
parent
953f365c24
commit
8df06fcefd
1 changed files with 50 additions and 50 deletions
48
main.go
48
main.go
|
@ -6,8 +6,8 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Span struct {
|
||||
|
@ -50,11 +50,16 @@ type Config struct {
|
|||
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.
|
||||
// More data is printed until there are no more traces available
|
||||
// i.e. until the ChildrenIds key yields an empty array.
|
||||
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]
|
||||
prefix := strings.Repeat(" ", depth)
|
||||
|
@ -68,7 +73,9 @@ func recursivelyPrintTraces(idToSpanMap map[string]Span, current string, config
|
|||
if config.verbosity > 1 {
|
||||
meta, err := json.MarshalIndent(currentSpan.Meta, "", " ")
|
||||
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("%s %s", prefix, meta))
|
||||
}
|
||||
|
@ -123,30 +130,24 @@ func parseTraceJsonFromFile(path string) TraceData {
|
|||
return fullTrace
|
||||
}
|
||||
|
||||
// Parses command-line arguments to extract flags and
|
||||
// other useful contextual details.
|
||||
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 {
|
||||
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, " "))
|
||||
|
||||
depth := 9999
|
||||
|
||||
if depthMatch != nil {
|
||||
depthValueIndex := rDepthPattern.SubexpIndex("depth")
|
||||
|
||||
|
@ -159,12 +160,11 @@ func parseArgs(args []string) Config {
|
|||
}
|
||||
|
||||
depth = depthLimit
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
rootOfInterest := os.Args[2]
|
||||
tracePath := os.Args[1]
|
||||
|
||||
return Config{ rootOfInterest, tracePath, verbosity, depth }
|
||||
return Config{rootOfInterest, tracePath, verbosity, depth}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
Reference in a new issue