Debian Codegraph @

modules/update/update.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package update
2
3 import (
4 "context"
5 "math"
6 "strconv"
7 "strings"
8
9 "github.com/bettercap/bettercap/core"
10 "github.com/bettercap/bettercap/session"
11
12 "github.com/google/go-github/github"
13
14 "github.com/evilsocket/islazy/tui"
15 )
16
17 type UpdateModule struct {
18 session.SessionModule
19 client *github.Client
20 }
21
22 func NewUpdateModule(s *session.Session) *UpdateModule {
23 mod := &UpdateModule{
24 SessionModule: session.NewSessionModule("update", s),
25 client: github.NewClient(nil),
26 }
27
28 mod.AddHandler(session.NewModuleHandler("update.check on", "",
29 "Check latest available stable version and compare it with the one being used.",
30 func(args []string) error {
31 return mod.Start()
32 }))
33
34 return mod
35 }
36
37 func (mod *UpdateModule) Name() string {
38 return "update"
39 }
40
41 func (mod *UpdateModule) Description() string {
42 return "A module to check for bettercap's updates."
43 }
44
45 func (mod *UpdateModule) Author() string {
46 return "Simone Margaritelli <[email protected]>"
47 }
48
49 func (mod *UpdateModule) Configure() error {
50 return nil
51 }
52
53 func (mod *UpdateModule) Stop() error {
54 return nil
55 }
56
57 func (mod *UpdateModule) versionToNum(ver string) float64 {
58 if ver[0] == 'v' {
59 ver = ver[1:]
60 }
61
62 n := 0.0
63 parts := strings.Split(ver, ".")
64 nparts := len(parts)
65
66 // reverse
67 for i := nparts/2 - 1; i >= 0; i-- {
68 opp := nparts - 1 - i
69 parts[i], parts[opp] = parts[opp], parts[i]
70 }
71
72 for i, e := range parts {
73 ev, _ := strconv.Atoi(e)
74 n += float64(ev) * math.Pow10(i)
75 }
76
77 return n
78 }
79
80 func (mod *UpdateModule) Start() error {
81 return mod.SetRunning(true, func() {
82 defer mod.SetRunning(false, nil)
83
84 mod.Info("checking latest stable release ...")
85
86 if releases, _, err := mod.client.Repositories.ListReleases(context.Background(), "bettercap", "bettercap", nil); err == nil {
87 latest := releases[0]
88 if mod.versionToNum(core.Version) < mod.versionToNum(*latest.TagName) {
89 mod.Session.Events.Add("update.available", latest)
90 } else {
91 mod.Info("you are running %s which is the latest stable version.", tui.Bold(core.Version))
92 }
93 } else {
94 mod.Error("error while fetching latest release info from GitHub: %s", err)
95 }
96 })
97 }