diff options
Diffstat (limited to 'branding/motd-cli/main.go')
-rw-r--r-- | branding/motd-cli/main.go | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/branding/motd-cli/main.go b/branding/motd-cli/main.go index 1c9191c..6828cc9 100644 --- a/branding/motd-cli/main.go +++ b/branding/motd-cli/main.go @@ -4,7 +4,9 @@ import ( "encoding/json" "flag" "fmt" + "io" "io/ioutil" + "net/http" "os" ) @@ -86,15 +88,22 @@ type LocalizedText struct { } func main() { - // TODO pass url flag too, to fetch and validate remote file file := flag.String("file", "", "file to validate") + url := flag.String("url", "", "url to validate") flag.Parse() + f := *file - if f == "" { - f = defaultFile - } + u := *url - fmt.Println("file:", f) + if u != "" { + fmt.Println("url:", u) + f = downloadToTempFile(u) + } else { + if f == "" { + f = defaultFile + } + fmt.Println("file:", f) + } m := parseFile(f) fmt.Printf("count: %v\n", m.Length()) fmt.Println() @@ -110,6 +119,24 @@ func main() { } } +func downloadToTempFile(url string) string { + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + out, err := ioutil.TempFile("/tmp/", "motd-linter") + if err != nil { + panic(err) + } + defer out.Close() + + _, _ = io.Copy(out, resp.Body) + fmt.Println("File downloaded to", out.Name()) + return out.Name() +} + func parseFile(f string) Messages { jsonFile, err := os.Open(f) if err != nil { |