Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flag: --sqldb #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions shotizam.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var (
base = flag.String("base", "", "base file to diff from; must be in json format")
mode = flag.String("mode", "sql", "output mode; tsv, json, sql, nameinfo")
output = flag.String("o", "", "output filename; default to stdout")
sqlite = flag.Bool("sqlite", false, "launch SQLite on data (when true, mode flag is ignored)")
verbose = flag.Bool("verbose", false, "verbose logging of file parsing")
)
Expand Down Expand Up @@ -251,12 +252,19 @@ func main() {
}

var w io.WriteCloser = os.Stdout
if !*sqlite && *output != "" && *output != "-" {
var err error
w, err = os.Create(*output)
if err != nil {
log.Fatalf("output fail: %s", err)
}
}
switch *mode {
case "sql":
case "json":
case "tsv":
case "nameinfo":
w = nopWriteCloser()

default:
log.Fatalf("unknown mode %q", *mode)
}
Expand Down Expand Up @@ -341,6 +349,7 @@ func main() {
newm := recMap(recs)
recs = diffMap(oldm, newm)
}
// TODO: output write new or diff?
je := json.NewEncoder(w)
je.SetIndent("", "\t")
if err := je.Encode(recs); err != nil {
Expand All @@ -359,8 +368,8 @@ func main() {
skip += len(name)
}
}
log.Printf(" total length of func names: %d", totNames)
log.Printf("bytes of func names which are prefixes of other func: %d", skip)
fmt.Fprintf(w, " total length of func names: %d\n", totNames)
fmt.Fprintf(w, "bytes of func names which are prefixes of other func: %d\n", skip)
return
}

Expand All @@ -369,6 +378,31 @@ func main() {
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}

if *output != "" {
(func() { // use func for defer close
r, err := os.Open(dbPath)
if err != nil {
log.Fatalf("output fail: %s", err)
}
defer r.Close()

var w io.WriteCloser = os.Stdout
if *output != "-" {
var err error
w, err = os.Create(*output)
if err != nil {
log.Fatalf("output fail: %s", err)
}
}
defer w.Close()

if _, err := io.Copy(w, r); err != nil {
log.Fatalf("output fail: %s", err)
}
})()
}

if err := syscall.Exec(cmd.Path, cmd.Args, cmd.Env); err != nil {
log.Fatal(err)
}
Expand Down