forked from opusb/tpclash2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.go
97 lines (81 loc) · 2.65 KB
/
upgrade.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
95
96
97
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/spf13/cobra"
semver "github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
)
var upgradeCmd = &cobra.Command{
Use: "upgrade [VERSION]",
Short: "upgrade TPClash",
Run: func(cmd *cobra.Command, args []string) {
var err error
var target *semver.Version
if len(args) == 1 {
target, err = semver.NewVersion(args[0])
if err != nil {
logrus.Fatalf("[upgrade] failed to parse version: %v", err)
}
logrus.Infof("[upgrade] upgrade to the specified version: v%s", target)
} else {
logrus.Info("[upgrade] check out the latest version from github...")
resp, err := http.Get(githubLatestApi)
if err != nil {
logrus.Fatalf("[upgrade] failed to request github api: %v", err)
}
defer func() { _ = resp.Close }()
var buf bytes.Buffer
if _, err = io.Copy(&buf, resp.Body); err != nil {
logrus.Fatalf("[upgrade] failed to read response: %v", err)
}
remoteVersion := &struct {
Value string `json:"tag_name"`
}{}
if err = json.Unmarshal(buf.Bytes(), remoteVersion); err != nil {
logrus.Fatalf("[upgrade] failed to unmarshal response: %v", err)
}
target, err = semver.NewVersion(remoteVersion.Value)
if err != nil {
logrus.Fatalf("[upgrade] failed to parse remote version: v%s: %v", remoteVersion, err)
}
logrus.Infof("[upgrade] upgrade to the latest version: v%s", target)
}
currentPath, err := os.Executable()
if err != nil {
logrus.Fatalf("[upgrade] failed to get current executable file path: %v", err)
}
tmpFile, err := os.OpenFile(filepath.Join(conf.ClashHome, fmt.Sprintf("tpclash.v%s", target)), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
if err != nil {
logrus.Fatalf("[upgrade] failed to create temp file: %v", err)
}
defer func() {
_ = tmpFile.Close()
}()
downAddr := fmt.Sprintf(githubUpgradeAddr, target, binName)
if conf.UpgradeWithGhProxy {
downAddr = ghProxyAddr + downAddr
}
logrus.Infof("[upgrade] start downloading file: %s", downAddr)
binResp, err := http.Get(downAddr)
if err != nil {
logrus.Fatalf("[upgrade] failed to download new version: v%s: %v", target, err)
}
defer func() { _ = binResp.Close }()
if _, err = io.Copy(tmpFile, binResp.Body); err != nil {
logrus.Fatalf("[upgrade] failed to write temp file: %v", err)
}
if err = os.Rename(tmpFile.Name(), currentPath); err != nil {
logrus.Fatalf("[upgrade] rename failed: %v", err)
}
fmt.Print(upgradedMessage)
},
}
func init() {
upgradeCmd.PersistentFlags().BoolVar(&conf.UpgradeWithGhProxy, "with-ghproxy", true, "use ghproxy.com to download upgrade files")
}