-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.go
91 lines (86 loc) · 2.05 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
package go_utils
import (
"fmt"
"github.com/apex/log"
"github.com/hktalent/go-update"
"github.com/hktalent/go-update/progress"
githubUpdateStore "github.com/hktalent/go-update/stores/github"
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
"os/exec"
"path/filepath"
"runtime"
"time"
)
func GetDate(arg ...string) string {
var currentTime = time.Now()
if 1 < len(arg) {
t1, err1 := time.Parse(arg[0], arg[1])
if nil == err1 {
currentTime = t1
}
}
l, err := time.LoadLocation("Asia/Shanghai")
if nil == err {
currentTime = time.Now().In(l)
}
return currentTime.Format("2006-01-02 15:04:05")
}
// 更新到最新版本
func UpdateScan4allVersionToLatest(verbose bool, u, t, dir string) error {
if verbose {
log.SetLevel(log.DebugLevel)
}
var command string
switch runtime.GOOS {
case "windows":
command = t + ".exe"
default:
command = t
}
m := &update.Manager{
Command: command,
Store: &githubUpdateStore.Store{
Owner: u,
Repo: t,
Version: `99.99.99`,
},
}
releases, err := m.LatestReleases()
if err != nil {
return errors.Wrap(err, "could not fetch latest release")
}
if len(releases) == 0 {
gologger.Info().Msgf("No new updates found for scan4all engine!")
return nil
}
latest := releases[0]
var currentOS string
switch runtime.GOOS {
case "darwin":
currentOS = "macOS"
default:
currentOS = runtime.GOOS
}
final := latest.FindZip(currentOS, runtime.GOARCH)
if final == nil {
return fmt.Errorf("no compatible binary found for %s/%s", currentOS, runtime.GOARCH)
}
szLstVer := final.Name
tarball, err := final.DownloadProxy(progress.Reader)
if err != nil {
return errors.Wrap(err, "could not download latest release")
}
if "" == dir {
bin, err := exec.LookPath(m.Command)
if err != nil {
return errors.Wrapf(err, "looking up path of %q", m.Command)
}
dir = filepath.Dir(bin)
}
if err := m.InstallTo(tarball, dir); err != nil {
return errors.Wrap(err, "could not install latest release")
}
gologger.Info().Msgf("Successfully updated to %s\n", szLstVer)
return nil
}