summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2021-11-16 20:36:25 +0100
committerkali kaneko (leap communications) <kali@leap.se>2021-11-16 20:36:25 +0100
commitce9aa9ab67b962b7250c4b0db84e4151bca29475 (patch)
treeda6953735dc22e94203d7f6dc44d90cb7c1f5444
parente901381e7d2ca9ceefd26c42d0e760d8ffc57fb2 (diff)
parse motd from remote urlfeat/ui-redesign
-rw-r--r--branding/motd-cli/README.md2
-rw-r--r--branding/motd-cli/main.go37
2 files changed, 34 insertions, 5 deletions
diff --git a/branding/motd-cli/README.md b/branding/motd-cli/README.md
index 926a982..5d00f10 100644
--- a/branding/motd-cli/README.md
+++ b/branding/motd-cli/README.md
@@ -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
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 {