-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
57 lines (48 loc) · 1.33 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
/*
Copyright © 2024 Kirill Chernetsky <foxsoft2005@gmail.com>
*/
package internal
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
)
var (
useBeta bool
)
// updateCmd represents the update command
var UpdateCmd = &cobra.Command{
Use: "update",
Short: "updates y360c app",
Long: `Use this command to update y360c application to the actual version.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("checking the latest version...")
wd, _ := os.Getwd()
filename := fmt.Sprintf("y360c-%s-x64.zip", strings.ToLower(runtime.GOOS))
url := fmt.Sprintf("https://github.com/foxsoft2005/y360c/releases/latest/download/%s", filename)
req, _ := http.NewRequest("GET", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalln("unable to get update:", err)
}
defer resp.Body.Close()
f, _ := os.OpenFile(filepath.Join(wd, filename), os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)
fmt.Printf("update downloaded to %v, extract it manually", filepath.Join(wd, filename))
},
}
func init() {
UpdateCmd.Flags().BoolVarP(&useBeta, "beta", "b", false, "use beta version for update")
}