-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcmd_update.go
72 lines (58 loc) · 2.01 KB
/
cmd_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
package main
import (
"fmt"
"net/http"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)
func init() {
updateCmd.Flags().BoolVar(&flagUpdateFromGithub, "from-github", false, "Update binary from latest github release")
updateCmd.Flags().StringVar(&flagUpdateURLAPI, "api", "", "Update binary from a CDS Engine API")
}
var (
flagUpdateFromGithub bool
flagUpdateURLAPI string
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update engine binary",
Example: "engine update --from-github",
Run: func(cmd *cobra.Command, args []string) {
if !flagUpdateFromGithub && flagUpdateURLAPI == "" {
sdk.Exit(`You have to use "./engine update --from-github" or "./engine update --api http://intance/of/your/cds/api"`)
}
var urlBinary string
conf := cdsclient.Config{Host: flagUpdateURLAPI}
client := cdsclient.New(conf)
fmt.Println(sdk.VersionString())
if flagUpdateFromGithub {
// no need to have apiEndpoint here
var errGH error
urlBinary, errGH = sdk.DownloadURLFromGithub(sdk.BinaryFilename("engine", sdk.GOOS, sdk.GOARCH, ""), "latest")
if errGH != nil {
sdk.Exit("Error while getting URL from Github url:%s err:%s\n", urlBinary, errGH)
}
fmt.Printf("Updating binary from Github on %s...\n", urlBinary)
} else {
urlBinary = client.DownloadURLFromAPI("engine", sdk.GOOS, sdk.GOARCH, "")
fmt.Printf("Updating binary from CDS API on %s...\n", urlBinary)
}
resp, err := http.Get(urlBinary)
if err != nil {
sdk.Exit("Error while getting binary from: %s err:%s\n", urlBinary, err)
}
defer resp.Body.Close()
if err := sdk.CheckContentTypeBinary(resp); err != nil {
sdk.Exit(err.Error())
}
if resp.StatusCode != 200 {
sdk.Exit("Error http code: %d, url called: %s\n", resp.StatusCode, urlBinary)
}
if err := update.Apply(resp.Body, update.Options{}); err != nil {
sdk.Exit("Error while updating binary from CDS API: %s\n", err)
}
fmt.Println("Update engine done.")
},
}