diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2021-11-16 20:16:07 +0100 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-11-23 21:51:27 +0100 |
commit | cdc80c3677753ad6eea8d864552077042bd9abb0 (patch) | |
tree | 05d9592401cafcfe7e35ae1ceac2eb296274a6e1 /branding/motd-cli/main.go | |
parent | f219243eb77546afbaaa7bc25d347952e1e2bbae (diff) |
[feat] motd can parse remote url
Diffstat (limited to 'branding/motd-cli/main.go')
-rw-r--r-- | branding/motd-cli/main.go | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/branding/motd-cli/main.go b/branding/motd-cli/main.go index 6cfd29e..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" ) @@ -28,20 +30,24 @@ type Message struct { 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.HasLocalizedText()) + m.IsValidType() && m.IsValidPlatform() && m.IsValidUrgency() && + m.HasLocalizedText()) return valid } func (m *Message) IsValidBegin() bool { + // FIXME check that begin is before 1y for instance return true } func (m *Message) IsValidEnd() bool { + // FIXME check end is within next year/months return true } @@ -63,8 +69,17 @@ func (m *Message) IsValidPlatform() bool { } } +func (m *Message) IsValidUrgency() bool { + switch m.Urgency { + case "normal", "critical": + return true + default: + return false + } +} + func (m *Message) HasLocalizedText() bool { - return true + return len(m.Text) > 0 } type LocalizedText struct { @@ -73,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() @@ -89,6 +111,7 @@ func main() { 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) @@ -96,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 { |