/
update.go
94 lines (79 loc) · 2.13 KB
/
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
package update
import (
"fmt"
"net/http"
"os"
"os/exec"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"
"github.com/ovh/cds/cli/cds/internal"
"github.com/ovh/cds/sdk"
)
// used by CI to inject at build time
var urlUpdateRelease string
// Cmd update
var Cmd = &cobra.Command{
Use: "update",
Short: "Update cds to the latest release version: cds update",
Long: `cds update`,
Aliases: []string{"up"},
Run: func(cmd *cobra.Command, args []string) {
doUpdate(urlUpdateRelease, internal.Architecture)
},
}
func getContentType(resp *http.Response) string {
for k, v := range resp.Header {
if k == "Content-Type" && len(v) >= 1 {
return v[0]
}
}
return ""
}
func guessArchitecture() string {
cmd := exec.Command("uname", "-m")
line, err := cmd.Output()
if err != nil {
fmt.Printf("Cannot guess architecture: %s\n", err)
return ""
}
return string(line)
}
func doUpdate(url, architecture string) {
if architecture == "" {
fmt.Printf("You seem to have a custom build of cds\n")
guess := guessArchitecture()
if guess == "" {
sdk.Exit("Please download latest release on %s\n", url)
}
fmt.Printf("Assuming architecture being %s\n", guess)
architecture = guess
url = fmt.Sprintf("%s/download/cli/%s", sdk.Host, architecture)
}
if internal.Verbose {
fmt.Printf("Url to update cds: %s\n", url)
}
resp, err := http.Get(url)
if err != nil {
sdk.Exit("Error when downloading cds: %s\n", err.Error())
fmt.Printf("Url: %s\n", url)
os.Exit(1)
}
contentType := getContentType(resp)
if contentType != "application/octet-stream" {
sdk.Exit("Invalid Binary (Content-Type: %s). Please try again or download it manually from %s\n", contentType, url)
fmt.Printf("Url: %s\n", url)
os.Exit(1)
}
if resp.StatusCode != 200 {
sdk.Exit("Error http code: %d, url called: %s\n", resp.StatusCode, url)
os.Exit(1)
}
fmt.Printf("Getting latest release from : %s ...\n", url)
defer resp.Body.Close()
if err = update.Apply(resp.Body, update.Options{}); err != nil {
sdk.Exit("Error when updating cds: %s\n", err.Error())
sdk.Exit("Url: %s\n", url)
os.Exit(1)
}
fmt.Println("Update done.")
}