-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathself-update.go
78 lines (62 loc) · 2.2 KB
/
self-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
package commands
import (
"fmt"
"kool-dev/kool/services/updater"
"strings"
"github.com/blang/semver"
"github.com/spf13/cobra"
)
// KoolSelfUpdate holds handlers and functions to implement the self-update command logic
type KoolSelfUpdate struct {
DefaultKoolService
updater updater.Updater
}
func AddKoolSelfUpdate(root *cobra.Command) {
var (
selfUpdate = NewKoolSelfUpdate()
selfUpdateCmd = NewSelfUpdateCommand(selfUpdate)
)
root.AddCommand(selfUpdateCmd)
}
// NewKoolSelfUpdate creates a new handler for self-update logic with default dependencies
func NewKoolSelfUpdate() *KoolSelfUpdate {
return &KoolSelfUpdate{
*newDefaultKoolService(),
&updater.DefaultUpdater{RootCommand: rootCmd},
}
}
// Execute runs the self-update logic with incoming arguments.
func (s *KoolSelfUpdate) Execute(args []string) (err error) {
if err = s.updater.CheckPermission(); err != nil {
return
}
var currentVersion, latestVersion semver.Version
currentVersion = s.updater.GetCurrentVersion()
if latestVersion, err = s.updater.Update(currentVersion); err != nil {
if strings.Contains(strings.ToLower(err.Error()), "permission denied") {
s.Shell().Warning("Error: %s", err)
return fmt.Errorf("kool self-update failed: (maybe try again with sudo)")
}
return fmt.Errorf("kool self-update failed: %v", err)
}
if latestVersion.Equals(currentVersion) {
s.Shell().Warning("You already have the latest version ", currentVersion.String())
return
}
s.Shell().Success("Successfully updated to version ", latestVersion.String())
s.Shell().Println("Please run 'kool info' now to validate your local environment.")
return
}
// NewSelfUpdateCommand initializes new kool self-update command
func NewSelfUpdateCommand(selfUpdate *KoolSelfUpdate) *cobra.Command {
selfUpdateTask := NewKoolTask("Updating kool version", selfUpdate)
selfUpdateTask.SetFrameOutput(false)
return &cobra.Command{
Use: "self-update",
Short: "Update kool to the latest version",
Long: "Checks the latest release of Kool in GitHub Releases, and downloads and replaces the local binary if a newer version is available.",
Args: cobra.NoArgs,
RunE: LongTaskCommandRunFunction(selfUpdateTask),
DisableFlagsInUseLine: true,
}
}