From 5bd3a1fde138e3c9e025c846aac71bf07493c53b Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Wed, 9 Jan 2019 17:54:21 -0800 Subject: [PATCH] added optional '-outfile' switch to output to a file (drops the need to redirect to output files) bumped minor version --- cmd/s2prot/s2prot.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/s2prot/s2prot.go b/cmd/s2prot/s2prot.go index bd4996a..fab2df9 100644 --- a/cmd/s2prot/s2prot.go +++ b/cmd/s2prot/s2prot.go @@ -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" ) @@ -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") ) @@ -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("", " ") }