From 390ddd8c62f22137930a8f3dad2d23b27556226b Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:06:43 -0700 Subject: [PATCH 1/7] ignore messages with null message body string --- bot/bot.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index f08c894..957e69d 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -285,9 +285,10 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) { return } - // FIXME check and see if the message from mm is a bot message, if so, - // ignore it - pretty.Print(post) + if post.Message == "" { + // null message, we can probably ignore it. + return + } if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched { b.Shutdown() From 9ce4a9172ac97f8cfb06e6a324cca67a75a47d57 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:12:58 -0700 Subject: [PATCH 2/7] parse metar response json --- bot/metar.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/bot/metar.go b/bot/metar.go index cf4efc8..bf09a31 100644 --- a/bot/metar.go +++ b/bot/metar.go @@ -11,6 +11,81 @@ import ( "time" ) +type MetarResponse struct { + Meta struct { + Timestamp time.Time `json:"timestamp"` + StationsUpdated string `json:"stations_updated"` + } `json:"meta"` + Altimeter struct { + Repr string `json:"repr"` + Value float64 `json:"value"` + Spoken string `json:"spoken"` + } `json:"altimeter"` + Clouds []interface{} `json:"clouds"` + FlightRules string `json:"flight_rules"` + Other []interface{} `json:"other"` + Sanitized string `json:"sanitized"` + Visibility struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"visibility"` + WindDirection struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"wind_direction"` + WindGust struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"wind_gust"` + WindSpeed struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"wind_speed"` + WxCodes []interface{} `json:"wx_codes"` + Raw string `json:"raw"` + Station string `json:"station"` + Time struct { + Repr string `json:"repr"` + Dt time.Time `json:"dt"` + } `json:"time"` + Remarks string `json:"remarks"` + Dewpoint struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"dewpoint"` + RemarksInfo struct { + DewpointDecimal struct { + Repr string `json:"repr"` + Value float64 `json:"value"` + Spoken string `json:"spoken"` + } `json:"dewpoint_decimal"` + TemperatureDecimal struct { + Repr string `json:"repr"` + Value float64 `json:"value"` + Spoken string `json:"spoken"` + } `json:"temperature_decimal"` + } `json:"remarks_info"` + RunwayVisibility []interface{} `json:"runway_visibility"` + Temperature struct { + Repr string `json:"repr"` + Value int `json:"value"` + Spoken string `json:"spoken"` + } `json:"temperature"` + WindVariableDirection []interface{} `json:"wind_variable_direction"` + Units struct { + Altimeter string `json:"altimeter"` + Altitude string `json:"altitude"` + Temperature string `json:"temperature"` + Visibility string `json:"visibility"` + WindSpeed string `json:"wind_speed"` + } `json:"units"` +} + func (b *Bot) HandleWeatherRequest(channelid string, postid string, message string) { // we are using a very bare image with no CA cert bundle @@ -52,7 +127,16 @@ func (b *Bot) HandleWeatherRequest(channelid string, postid string, message stri data, _ := ioutil.ReadAll(resp.Body) - log.Info().Msgf("weather %s: %s", loc, data) - b.SendMsgToChannel(fmt.Sprintf("weather %s: %s", loc, data), postid, channelid) + var parsedMetarResponse MetarResponse + log.Info().Msgf("weather %s: %s", loc, data) + + err = nil + err = json.Unmarshal([]byte(data), &parsedMetarResponse) + + if err != nil { + b.SendMsgToChannel("error deserializing metar data", postid, channelid) + return + } + b.SendMsgToChannel(fmt.Sprintf("weather for `%s`: \n```\n%+v\n```\n", loc, parsedMetarResponse), postid, channelid) } From f6749f4e9e9369e00073469673f02b7ea92d8aa7 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:13:53 -0700 Subject: [PATCH 3/7] i really should setup goimports --- bot/bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/bot.go b/bot/bot.go index 957e69d..f507001 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -1,6 +1,6 @@ package bot -import "github.com/kr/pretty" +//import "github.com/kr/pretty" import ( "fmt" "github.com/mattermost/mattermost-server/v5/model" From 98e9eb44d3b4921e8de05910fccfa1aeb67a47e1 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:15:50 -0700 Subject: [PATCH 4/7] maybe works now --- Makefile | 1 + bot/bot.go | 5 +++-- bot/logger.go | 5 +++-- bot/metar.go | 4 +++- cmd/sco/main.go | 7 +++++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 977cde2..15c02c9 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ default: fmt fmt: go fmt ./... + goimports -l -w . docker-build: docker build -t $(IMAGENAME) . diff --git a/bot/bot.go b/bot/bot.go index f507001..2051599 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -3,14 +3,15 @@ package bot //import "github.com/kr/pretty" import ( "fmt" - "github.com/mattermost/mattermost-server/v5/model" - "github.com/rs/zerolog/log" "os" "os/signal" "regexp" "strings" "syscall" "time" + + "github.com/mattermost/mattermost-server/v5/model" + "github.com/rs/zerolog/log" ) type Bot struct { diff --git a/bot/logger.go b/bot/logger.go index d7c4073..8ba7c93 100644 --- a/bot/logger.go +++ b/bot/logger.go @@ -1,11 +1,12 @@ package bot import ( + "os" + "time" + "github.com/mattn/go-isatty" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "os" - "time" ) func (b *Bot) setupLogging() { diff --git a/bot/metar.go b/bot/metar.go index bf09a31..a93b37d 100644 --- a/bot/metar.go +++ b/bot/metar.go @@ -2,13 +2,15 @@ package bot //import "github.com/kr/pretty" import ( + "encoding/json" "fmt" - "github.com/rs/zerolog/log" "io/ioutil" "net/http" "os" "regexp" "time" + + "github.com/rs/zerolog/log" ) type MetarResponse struct { diff --git a/cmd/sco/main.go b/cmd/sco/main.go index 3837b07..12e1dd4 100644 --- a/cmd/sco/main.go +++ b/cmd/sco/main.go @@ -1,7 +1,10 @@ package main -import "os" -import "git.eeqj.de/sneak/sco/bot" +import ( + "os" + + "git.eeqj.de/sneak/sco/bot" +) var Version string var Commit string From d22bf0a3b5248df3beb4003d10ea57aad17db804 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:21:13 -0700 Subject: [PATCH 5/7] now must be formatted/vetted --- Makefile | 2 ++ bot/bot.go | 3 +-- cmd/sco/main.go | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 15c02c9..58ca4b1 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ default: fmt fmt: go fmt ./... goimports -l -w . + go vet ./... docker-build: docker build -t $(IMAGENAME) . @@ -43,4 +44,5 @@ go-get: cd cmd/$(FN) && go get -v ./$(FN): */*.go cmd/*/*.go go-get + go vet ./... cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) . diff --git a/bot/bot.go b/bot/bot.go index 2051599..955f8a3 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -52,7 +52,7 @@ func (b *Bot) identify() { Msg("starting") } -func (b *Bot) Main() int { +func (b *Bot) Main() { println(b.BotName) b.StartupUnixTime = time.Now().Unix() @@ -107,7 +107,6 @@ func (b *Bot) Main() int { // You can block forever with select {} - return 0 } func (b *Bot) MakeSureServerIsRunning() { diff --git a/cmd/sco/main.go b/cmd/sco/main.go index 12e1dd4..63a609f 100644 --- a/cmd/sco/main.go +++ b/cmd/sco/main.go @@ -26,5 +26,5 @@ func main() { b.AccountFirstname = "LSV" b.AccountLastname = "Serious Callers Only" }) - os.Exit(mybot.Main()) + mybot.Main() } From f6ee57d25ce53c06ea61e03d0590c5c70443fb36 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:29:42 -0700 Subject: [PATCH 6/7] now finally errors on bad formatting! --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 58ca4b1..788b64b 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,6 @@ default: fmt fmt: go fmt ./... goimports -l -w . - go vet ./... docker-build: docker build -t $(IMAGENAME) . @@ -43,6 +42,9 @@ build: ./$(FN) go-get: cd cmd/$(FN) && go get -v -./$(FN): */*.go cmd/*/*.go go-get +vet: go vet ./... + bash -c 'test -z "$$(gofmt .)"' + +./$(FN): */*.go cmd/*/*.go go-get vet cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) . From 1b15abb7f7013a540753f41a85de47147cb0b310 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 21:32:16 -0700 Subject: [PATCH 7/7] should err on lack of vet/fmt now --- .gitignore | 1 + Makefile | 2 +- cmd/sco/main.go | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fb30ca6..6d3322f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ sco +.DS_Store diff --git a/Makefile b/Makefile index 788b64b..5a139cd 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ go-get: vet: go vet ./... - bash -c 'test -z "$$(gofmt .)"' + bash -c 'test -z "$$(gofmt -l .)"' ./$(FN): */*.go cmd/*/*.go go-get vet cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) . diff --git a/cmd/sco/main.go b/cmd/sco/main.go index 63a609f..93b9435 100644 --- a/cmd/sco/main.go +++ b/cmd/sco/main.go @@ -8,6 +8,7 @@ import ( var Version string var Commit string + var Buildarch string func main() {