forked from thedevsaddam/docgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
126 lines (109 loc) · 2.78 KB
/
github.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package update
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/kardianos/osext"
)
type (
// ReleaseInfo represents github latest release info
ReleaseInfo struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
}
Asset struct {
Name string `json:"name"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
BrowserDownloadURL string `json:"browser_download_url"`
}
)
func (r *ReleaseInfo) getDownloadURL(name string) string {
for _, a := range r.Assets {
if a.Name == name {
return a.BrowserDownloadURL
}
}
return ""
}
// fetchReleaseInfo return githublatest release info
func fetchReleaseInfo(ctx context.Context) (*ReleaseInfo, error) {
url := "https://api.github.com/repos/cksidharthan/docgen/releases/latest"
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("update: failed to fetch latest release")
}
rf := ReleaseInfo{}
if err := json.NewDecoder(resp.Body).Decode(&rf); err != nil {
return nil, err
}
return &rf, nil
}
// updateBinary download the binary in current binary and replace the old one
func updateBinary(ctx context.Context, url string) error {
if url == "" {
return errors.New("update: empty download url")
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("update: failed to fetch binary file")
}
dir, err := osext.ExecutableFolder()
if err != nil {
return err
}
fileName := filepath.Join(dir, filepath.Base(os.Args[0]))
tmpFile := fileName + ".tmp"
if err := os.Chmod(fileName, 0777); err != nil {
return err
}
if err := os.Rename(fileName, tmpFile); err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
return err
}
if err := os.Chmod(fileName, 0777); err != nil {
return err
}
_, err = io.Copy(f, resp.Body)
if err != nil {
if err := os.Rename(tmpFile, fileName); err != nil {
return err
}
return err
}
return os.Remove(tmpFile)
}