-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_update.go
168 lines (146 loc) · 3.25 KB
/
self_update.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package vss
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"path/filepath"
"runtime"
"strings"
bufra "github.com/avvmoto/buf-readerat"
"github.com/minio/selfupdate"
)
const (
latestReleaseUrl = "https://github.com/veltiosoft/go-vss/releases/latest/download"
exe = "vss"
)
type SelfUpdateCommand struct {
Meta
}
func (c *SelfUpdateCommand) Help() string {
return "Usage: vss self update"
}
func (c *SelfUpdateCommand) Synopsis() string {
return "Update vss(self) command"
}
func (c *SelfUpdateCommand) Run(args []string) int {
fmt.Println("Updating to latest ...")
var ext string
if runtime.GOOS == "linux" {
ext = "tar.gz"
} else {
ext = "zip"
}
target := fmt.Sprintf("%s/vss_%s_%s.%s", latestReleaseUrl, runtime.GOOS, runtime.GOARCH, ext)
resp, err := http.Get(target)
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("[ERROR] %s", resp.Status)
return 1
}
var r io.Reader
if ext == "tar.gz" {
r, err = extractTgz(resp)
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
} else {
r, err = extractZip(resp)
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
}
err = updateBinary(r)
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
log.Printf("[INFO] Successfully updated")
cmd := exec.Command(exe, "self", "version")
output, err := cmd.CombinedOutput()
if err != nil {
// when unsupported self version command
cmd := exec.Command(exe, "--version")
output, err = cmd.CombinedOutput()
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
}
fmt.Print(string(output))
return 0
}
func updateBinary(target io.Reader) error {
err := selfupdate.Apply(target, selfupdate.Options{})
if err != nil {
return err
}
return nil
}
// extractTgz extracts the binary from the tar.gz archive
func extractTgz(resp *http.Response) (io.Reader, error) {
// Create a new gzip reader
gr, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer gr.Close()
// Create a tar reader
tr := tar.NewReader(gr)
// Iterate through the files in the archive
for {
header, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
return nil, err
}
// Open the output file
if header.FileInfo().IsDir() {
continue
}
filename := filepath.Join(strings.Split(header.Name, "/")[1:]...)
if filename == binaryName {
return tr, nil
} else {
continue
}
}
return nil, errors.New("vss binary not found")
}
// extractZip extracts the binary from the zip archive
func extractZip(resp *http.Response) (io.Reader, error) {
var buf bytes.Buffer
_, err := io.Copy(&buf, resp.Body)
if err != nil {
return nil, err
}
if resp.ContentLength != int64(buf.Len()) {
return nil, errors.New("content length mismatch")
}
bufr := bufra.NewBufReaderAt(bytes.NewReader(buf.Bytes()), buf.Len())
r, err := zip.NewReader(bufr, int64(buf.Len()))
if err != nil {
return nil, err
}
for _, file := range r.File {
filename := filepath.Join(strings.Split(file.Name, "/")[1:]...)
if filename == binaryName {
return file.Open()
}
}
return nil, errors.New("vss binary not found")
}