/
cmd.go
84 lines (71 loc) 路 2.21 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package update
import (
"context"
"fmt"
"net/http"
"runtime"
"strings"
"github.com/google/go-github/github"
"github.com/inconshreveable/go-update"
"github.com/ovh/venom/cli"
"github.com/spf13/cobra"
)
var urlGitubReleases = "https://github.com/ovh/venom/releases"
// Cmd update
var Cmd = &cobra.Command{
Use: "update",
Short: "Update venom to the latest release version: venom update",
Long: `venom update`,
Run: func(cmd *cobra.Command, args []string) {
doUpdate()
},
}
func getURLArtifactFromGithub() string {
client := github.NewClient(nil)
release, resp, err := client.Repositories.GetLatestRelease(context.TODO(), "ovh", "venom")
if err != nil {
cli.Exit("Repositories.GetLatestRelease returned error: %v\n%v", err, resp.Body)
}
if len(release.Assets) > 0 {
for _, asset := range release.Assets {
assetName := strings.Replace(*asset.Name, ".", "-", -1)
current := fmt.Sprintf("venom-%s-%s", runtime.GOOS, runtime.GOARCH)
if assetName == current {
return *asset.BrowserDownloadURL
}
}
}
text := "Invalid Artifacts on latest release. Please try again in few minutes.\n"
text += "If the problem persists, please open an issue on https://github.com/ovh/venom/issues\n"
cli.Exit(text)
return ""
}
func getContentType(resp *http.Response) string {
for k, v := range resp.Header {
if k == "Content-Type" && len(v) >= 1 {
return v[0]
}
}
return ""
}
func doUpdate() {
url := getURLArtifactFromGithub()
fmt.Printf("Url to update venom: %s\n", url)
resp, err := http.Get(url)
if err != nil {
cli.Exit("Error when downloading venom from url %s: %v\n", url, err)
}
if contentType := getContentType(resp); contentType != "application/octet-stream" {
fmt.Printf("Url: %s\n", url)
cli.Exit("Invalid Binary (Content-Type: %s). Please try again or download it manually from %s\n", contentType, urlGitubReleases)
}
if resp.StatusCode != 200 {
cli.Exit("Error http code: %d, url called: %s\n", resp.StatusCode, url)
}
fmt.Printf("Getting latest release from: %s ...\n", url)
defer resp.Body.Close()
if err = update.Apply(resp.Body, update.Options{}); err != nil {
cli.Exit("Error when updating venom from url: %s err:%s\n", url, err.Error())
}
fmt.Println("Update done.")
}