From 80d424722be9538d75764afbcf728a8489b081b8 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Sun, 2 Jun 2019 13:34:38 +0800 Subject: [PATCH] fix panics --- backends/cachet/backend.go | 22 +++++++++++++++++----- cli/main.go | 5 ----- cli/root.go | 4 +--- monitors/http.go | 4 +++- 4 files changed, 21 insertions(+), 14 deletions(-) delete mode 100644 cli/main.go diff --git a/backends/cachet/backend.go b/backends/cachet/backend.go index 52daa0d..b08cfc7 100644 --- a/backends/cachet/backend.go +++ b/backends/cachet/backend.go @@ -103,9 +103,7 @@ func (api CachetBackend) NewRequest(requestType, url string, reqBody []byte) (*h defer res.Body.Close() defer req.Body.Close() - var body struct { - Data json.RawMessage `json:"data"` - } + var body CachetResponse err = json.NewDecoder(res.Body).Decode(&body) return res, body, err @@ -248,7 +246,21 @@ func (api CachetBackend) Tick(monitor monitors.MonitorInterface, status monitors } func (api CachetBackend) getComponent(componentID int) (*Component, error) { - return nil, nil + resp, body, err := api.NewRequest("GET", "/components/"+strconv.Itoa(componentID), nil) + if err != nil { + return nil, err + } + + var data *Component + if err := json.Unmarshal(body.(CachetResponse).Data, &data); err != nil { + return nil, fmt.Errorf("Cannot decode component: %v", err) + } + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Could not get component! %v", err) + } + + return data, nil } func (api CachetBackend) findIncident(componentID int) (*Incident, error) { @@ -270,7 +282,7 @@ func (api CachetBackend) findIncident(componentID int) (*Incident, error) { } func (api CachetBackend) findIncidents(componentID int, status int) ([]*Incident, error) { - resp, body, err := api.NewRequest("GET", "/incidents?component_id="+strconv.Itoa(componentID)+"&status="+strconv.Itoa(status), nil) + resp, body, err := api.NewRequest("GET", "/incidents?component_Id="+strconv.Itoa(componentID)+"&status="+strconv.Itoa(status), nil) if err != nil { return nil, err } diff --git a/cli/main.go b/cli/main.go deleted file mode 100644 index 736ef31..0000000 --- a/cli/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -func main() { - Execute() -} diff --git a/cli/root.go b/cli/root.go index 648a0a4..d88cc66 100644 --- a/cli/root.go +++ b/cli/root.go @@ -24,9 +24,7 @@ var rootCmd = &cobra.Command{ }, } -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { +func main() { if err := rootCmd.Execute(); err != nil { panic(err) } diff --git a/monitors/http.go b/monitors/http.go index 8b7ee37..e079451 100644 --- a/monitors/http.go +++ b/monitors/http.go @@ -49,7 +49,9 @@ func (monitor *HTTPMonitor) test() (bool, []error) { } transport := http.DefaultTransport.(*http.Transport) - transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: monitor.Strict == false} + transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: monitor.Strict == false, + } client := &http.Client{ Timeout: time.Duration(monitor.Timeout * time.Second), Transport: transport,