summaryrefslogtreecommitdiff
path: root/branding/motd-cli/main.go
blob: e1842bf20f5400edc4adb43339b5618cb8eff2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"

	"0xacab.org/leap/bitmask-vpn/pkg/motd"
)

const OK = "✓"
const WRONG = "☓"

func main() {
	file := flag.String("file", "", "file to validate")
	url := flag.String("url", "", "url to validate")
	flag.Parse()

	f := *file
	u := *url

	var m motd.Messages
	var err error

	if u != "" {
		fmt.Println("url:", u)
		f = downloadToTempFile(u)
	} else {
		if f == "" {
			f = filepath.Join("../../pkg/motd/", motd.ExampleFile)
		}
		fmt.Println("file:", f)
	}
	m, err = motd.ParseFile(f)
	if err != nil {
		panic(err)
	}
	fmt.Printf("count: %v\n", m.Length())
	fmt.Println()
	for i, msg := range m.Messages {
		fmt.Printf("Message %d %v\n-----------\n", i+1, mark(msg.IsValid()))
		fmt.Printf("Type: %s %v\n", msg.Type, mark(msg.IsValidType()))
		fmt.Printf("Platform: %s %v\n", msg.Platform, mark(msg.IsValidPlatform()))
		fmt.Printf("Urgency: %s %v\n", msg.Urgency, mark(msg.IsValidUrgency()))
		fmt.Printf("Languages: %d %v\n", len(msg.Text), mark(msg.HasLocalizedText()))
		for _, t := range msg.Text {
			fmt.Printf(t.Str)
		}
		if !msg.IsValid() {
			os.Exit(1)
		}
	}
}

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 mark(val bool) string {
	if val {
		return OK
	} else {
		return WRONG
	}
}