/
checkupdates_cmd.go
67 lines (54 loc) · 1.47 KB
/
checkupdates_cmd.go
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
package server
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/harfangapps/regis-companion/resp"
)
type doer interface {
Do(req *http.Request) (*http.Response, error)
}
type checkUpdatesCmd struct {
client doer
}
const githubEndpoint = `https://api.github.com/repos/harfangapps/homebrew-harfangapps/releases/latest`
// CHECKUPDATES
func (c checkUpdatesCmd) Execute(cmdName string, req []string, s *Server) (interface{}, error) {
if len(req) != 1 {
return resp.Error(fmt.Sprintf("ERR wrong number of arguments for %v", cmdName)), nil
}
hreq, err := http.NewRequest("GET", githubEndpoint, nil)
if err != nil {
return resp.Error(fmt.Sprintf("ERR failed to create request: %v", err)), nil
}
res, err := c.client.Do(hreq)
if err != nil {
return resp.Error(fmt.Sprintf("ERR request failed: %v", err)), nil
}
defer res.Body.Close()
v, err := readRelease(res.Body)
if err != nil {
return resp.Error(fmt.Sprintf("ERR failed to read version: %v", err)), nil
}
// return true if the release is different than the current version
// (ideally, should be later than, but in practice different is enough)
return v != Version && v != "", nil
}
func readRelease(r io.Reader) (string, error) {
var obj struct {
TagName string `json:"tag_name"`
}
data, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
if len(data) == 0 {
return "", nil
}
if err := json.Unmarshal(data, &obj); err != nil {
return "", err
}
return obj.TagName, nil
}