summaryrefslogtreecommitdiff
path: root/branding/motd-cli/main.go
blob: 0ac13161b0aa752a3725e0d91c2a18d967592363 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

/* TODO move structs to pkg/config/motd module, import from there */

const defaultFile = "motd-example.json"
const OK = "✓"
const WRONG = "☓"
const TimeString = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone

type Messages struct {
	Messages []Message `json:"motd"`
}

func (m *Messages) Length() int {
	return len(m.Messages)
}

type Message struct {
	Begin    string          `json:"begin"`
	End      string          `json:"end"`
	Type     string          `json:"type"`
	Platform string          `json:"platform"`
	Urgency  string          `json:"urgency"`
	Text     []LocalizedText `json:"text"`
}

func (m *Message) IsValid() bool {
	valid := (m.IsValidBegin() && m.IsValidEnd() &&
		m.IsValidType() && m.IsValidPlatform() && m.IsValidUrgency() &&
		m.HasLocalizedText())
	return valid
}

func (m *Message) IsValidBegin() bool {
	_, err := time.Parse(TimeString, m.Begin)
	if err != nil {
		log.Println(err)
		return false
	}
	return true
}

func (m *Message) IsValidEnd() bool {
	endTime, err := time.Parse(TimeString, m.End)
	if err != nil {
		log.Println(err)
		return false
	}
	beginTime, err := time.Parse(TimeString, m.Begin)
	if err != nil {
		log.Println(err)
		return false
	}
	if !beginTime.Before(endTime) {
		log.Println("begin ts should be before end")
		return false
	}
	return true
}

func (m *Message) IsValidType() bool {
	switch m.Type {
	case "once", "daily":
		return true
	default:
		return false
	}
}

func (m *Message) IsValidPlatform() bool {
	switch m.Platform {
	case "windows", "linux", "osx", "all":
		return true
	default:
		return false
	}
}

func (m *Message) IsValidUrgency() bool {
	switch m.Urgency {
	case "normal", "critical":
		return true
	default:
		return false
	}
}

func (m *Message) HasLocalizedText() bool {
	return len(m.Text) > 0
}

type LocalizedText struct {
	Lang string `json:"lang"`
	Str  string `json:"str"`
}

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

	f := *file
	u := *url

	if u != "" {
		fmt.Println("url:", u)
		f = downloadToTempFile(u)
	} else {
		if f == "" {
			f = defaultFile
		}
		fmt.Println("file:", f)
	}
	m, err := 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()))
		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 parseFile(f string) (Messages, error) {
	jsonFile, err := os.Open(f)
	if err != nil {
		panic(err)
	}
	defer jsonFile.Close()
	byteVal, err := ioutil.ReadAll(jsonFile)
	if err != nil {
		panic(err)
	}
	return parseJsonStr(byteVal)
}

func parseJsonStr(b []byte) (Messages, error) {
	var m Messages
	json.Unmarshal(b, &m)
	return m, nil
}

func mark(val bool) string {
	if val {
		return OK
	} else {
		return WRONG
	}
}