-
Notifications
You must be signed in to change notification settings - Fork 0
/
verUpgrade.go
58 lines (49 loc) · 1.13 KB
/
verUpgrade.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
package cmd
import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"github.com/spf13/cobra"
)
var downloadURL = "https://github.com/nuage-lab/aws-tgw-network-mgr/raw/master/get.sh"
// upgradeCmd represents the version command
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "upgrade awsnuagenetwmgr to latest available version",
Run: func(cmd *cobra.Command, args []string) {
f, err := ioutil.TempFile("", "awsnuagenetwmgr")
defer os.Remove(f.Name())
if err != nil {
log.Fatalf("Failed to create temp file %s\n", err)
}
downloadFile(downloadURL, f)
c := exec.Command("bash", f.Name())
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
log.Fatalf("Upgrade failed: %s\n", err)
}
},
}
// downloadFile will download a file from a URL and write its content to a file
func downloadFile(url string, file *os.File) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
func init() {
versionCmd.AddCommand(upgradeCmd)
}