forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
59 lines (49 loc) · 1.35 KB
/
remote.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package sources
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
)
// RemoteUpdateSource finds releases/updates from custom url feed (used primarily for testing)
type RemoteUpdateSource struct {
libkb.Contextified
}
func NewRemoteUpdateSource(g *libkb.GlobalContext) RemoteUpdateSource {
return RemoteUpdateSource{
Contextified: libkb.NewContextified(g),
}
}
func (k RemoteUpdateSource) FindUpdate(config keybase1.UpdateConfig) (update *keybase1.Update, err error) {
if config.URL == "" {
err = fmt.Errorf("No source URL for remote")
return
}
req, err := http.NewRequest("GET", config.URL, nil)
client := &http.Client{}
k.G().Log.Info("Request %#v", config.URL)
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Updater remote returned bad status %v", resp.Status)
return
}
var r io.Reader = resp.Body
var obj keybase1.Update
if err = json.NewDecoder(r).Decode(&obj); err != nil {
err = fmt.Errorf("Bad updater remote response %s", err)
return
}
update = &obj
k.G().Log.Debug("Received update %#v", update)
return
}