From fa50a2d6181150c791e0e2f307588fb00b744dd2 Mon Sep 17 00:00:00 2001 From: Nils Jakobi Date: Sat, 2 Jan 2021 22:17:15 +0100 Subject: [PATCH] forgot to add types.go --- types.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/types.go b/types.go index 8b4831b..af44a00 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,12 @@ package main -import "time" +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "time" +) type device struct { Name string `json:"name"` @@ -111,6 +117,26 @@ type output struct { Links []link `json:"links"` } +func (o *output) writeToFile() error { + file, err := json.MarshalIndent(o, "", " ") + if err != nil { + return fmt.Errorf("can't marshal to json: %+v", o) + } + + // write to file + if _, err := os.Stat("output"); os.IsNotExist(err) { + err = os.Mkdir("output", 0755) + if err != nil { + return fmt.Errorf("can't create output folder") + } + } + err = ioutil.WriteFile("output/meshviewer.json", file, 0644) + if err != nil { + return fmt.Errorf("can't write to json file meshviewer.json") + } + return nil +} + type apiResponse struct { StatusCode int `json:"statusCode"` Error string `json:"error"`