From ad30e3688177deec08e2e3cfb40f59670dee8cb8 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 2 Dec 2018 23:49:48 +0000 Subject: [PATCH] Add -json flag for -genconf and -normaliseconf --- yggdrasil.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/yggdrasil.go b/yggdrasil.go index 8536fa13..0a82437f 100644 --- a/yggdrasil.go +++ b/yggdrasil.go @@ -76,9 +76,15 @@ func generateConfig(isAutoconf bool) *nodeConfig { // Generates a new configuration and returns it in HJSON format. This is used // with -genconf. -func doGenconf() string { +func doGenconf(isjson bool) string { cfg := generateConfig(false) - bs, err := hjson.Marshal(cfg) + var bs []byte + var err error + if isjson { + bs, err = json.MarshalIndent(cfg, "", " ") + } else { + bs, err = hjson.Marshal(cfg) + } if err != nil { panic(err) } @@ -92,6 +98,7 @@ func main() { useconf := flag.Bool("useconf", false, "read config from stdin") useconffile := flag.String("useconffile", "", "read config from specified file path") normaliseconf := flag.Bool("normaliseconf", false, "use in combination with either -useconf or -useconffile, outputs your configuration normalised") + confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") flag.Parse() @@ -186,7 +193,12 @@ func main() { // their configuration file with newly mapped names (like above) or to // convert from plain JSON to commented HJSON. if *normaliseconf { - bs, err := hjson.Marshal(cfg) + var bs []byte + if *confjson { + bs, err = json.MarshalIndent(cfg, "", " ") + } else { + bs, err = hjson.Marshal(cfg) + } if err != nil { panic(err) } @@ -195,7 +207,7 @@ func main() { } case *genconf: // Generate a new configuration and print it to stdout. - fmt.Println(doGenconf()) + fmt.Println(doGenconf(*confjson)) default: // No flags were provided, therefore print the list of flags to stdout. flag.PrintDefaults()