Skip to content

Commit

Permalink
plist parsing table (#538)
Browse files Browse the repository at this point in the history
The existing osquery plist table does not work well with complex nested data. It's especially bad for the array-of-dict style structure.

This introduces a library to parse that kind of data structure; a table based on it; and a simple command line tool for testing.
  • Loading branch information
directionless authored Nov 1, 2019
1 parent 752fa6e commit 6cb383a
Show file tree
Hide file tree
Showing 16 changed files with 1,723 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/dataflatten/examples/flatten/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"

"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/pkg/dataflatten"
"github.com/peterbourgon/ff"
"github.com/pkg/errors"
)

func checkError(err error) {
if err != nil {
fmt.Printf("Got Error: %v\nStack:\n%+v\n", err, err)
os.Exit(1)
}
}

func main() {

flagset := flag.NewFlagSet("plist", flag.ExitOnError)

var (
flPlist = flagset.String("plist", "", "Path to plist")
flJson = flagset.String("json", "", "Path to json file")
flQuery = flagset.String("q", "", "query")

flDebug = flagset.Bool("debug", false, "use a debug logger")
)

if err := ff.Parse(flagset, os.Args[1:],
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
); err != nil {
checkError(errors.Wrap(err, "parsing flags"))
}

logger := logutil.NewCLILogger(*flDebug)

opts := []dataflatten.FlattenOpts{
dataflatten.WithLogger(logger),
}

if *flQuery != "" {
opts = append(opts, dataflatten.WithQuery(strings.Split(*flQuery, `/`)))
}

rows := []dataflatten.Row{}

if *flPlist != "" {
data, err := dataflatten.PlistFile(*flPlist, opts...)
checkError(errors.Wrap(err, "flattening plist file"))
rows = append(rows, data...)
}

if *flJson != "" {
data, err := dataflatten.JsonFile(*flJson, opts...)
checkError(errors.Wrap(err, "flattening json file"))
rows = append(rows, data...)
}

w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "path", "parent key", "key", "value")
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "----", "----------", "---", "-----")

for _, row := range rows {
p, k := row.ParentKey("/")
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", row.StringPath("/"), p, k, row.Value)
}
w.Flush()

return
}
Loading

0 comments on commit 6cb383a

Please sign in to comment.