Skip to content

Commit

Permalink
added optional '-outfile' switch to output to a file (drops the need …
Browse files Browse the repository at this point in the history
…to redirect to output files)

bumped minor version
  • Loading branch information
lallousx86 committed Jan 10, 2019
1 parent d267999 commit 5bd3a1f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cmd/s2prot/s2prot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
appName = "s2prot"
appVersion = "v1.4.0"
appVersion = "v1.4.1"
appAuthor = "Andras Belicza"
appHome = "https://github.com/icza/s2prot"
)
Expand All @@ -36,6 +36,7 @@ var (
gameEvts = flag.Bool("gameevts", false, "print game events")
msgEvts = flag.Bool("msgevts", false, "print message events")
trackerEvts = flag.Bool("trackerevts", false, "print tracker events")
outfile = flag.String("outfile", "", "Optional output file name")

indent = flag.Bool("indent", true, "use indentation when formatting output")
)
Expand Down Expand Up @@ -86,7 +87,24 @@ func main() {
r.TrackerEvts = nil
}

enc := json.NewEncoder(os.Stdout)
var enc *json.Encoder = nil

if len(*outfile) == 0 {
enc = json.NewEncoder(os.Stdout)
} else {
fp, err := os.Create(*outfile)
if err != nil {
fmt.Printf("Failed to create output file: %v\n", err)
os.Exit(2)
}
enc = json.NewEncoder(fp)
defer func() {
if err := fp.Close(); err != nil {
panic(err)
}
}()
}

if *indent {
enc.SetIndent("", " ")
}
Expand Down

0 comments on commit 5bd3a1f

Please sign in to comment.