Skip to content
Snippets Groups Projects
Commit ce9aa9ab authored by Kali Kaneko's avatar Kali Kaneko
Browse files

parse motd from remote url

parent e901381e
Branches master
Tags 0.20.4
No related merge requests found
......@@ -54,6 +54,8 @@ Urgency: normal ✓
Languages: 2 ✓
```
Use `motd-cli -url https://example.com/motd.json` to validate a remote file.
Notes: I'm considering adding an explicit layer of verification of the motd
payload. Please comment on
[#554](https://0xacab.org/leap/bitmask-vpn/-/issues/554) if you have an opinion
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment