-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
121 lines (98 loc) · 2.81 KB
/
plugin.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Package plasmactlupdate implements an update launchr plugin
package plasmactlupdate
import (
"context"
_ "embed"
"fmt"
"github.com/launchrctl/keyring"
"github.com/launchrctl/launchr"
"github.com/launchrctl/launchr/pkg/action"
)
func init() {
launchr.RegisterPlugin(&Plugin{})
}
//go:embed action.yaml
var actionYaml []byte
// Plugin is [launchr.Plugin] providing update action.
type Plugin struct {
k keyring.Keyring
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: 20,
}
}
// OnAppInit implements [launchr.OnAppInitPlugin] interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.k)
return nil
}
// DiscoverActions implements [launchr.ActionDiscoveryPlugin] interface.
func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) {
a := action.NewFromYAML("update", actionYaml)
a.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
input := a.Input()
ci := keyring.CredentialsItem{
URL: baseURL,
Username: input.Opt("username").(string),
Password: input.Opt("password").(string),
}
u, err := createUpdateAction(p.k, ci)
if err != nil {
return err
}
return runUpdate(u)
}))
return []*action.Action{a}, nil
}
// runUpdate command entrypoint.
func runUpdate(u *updateAction) error {
// Wrapper to conclude errors.
if err := runCommands(u); err != nil {
u.exitWithError()
return err
}
return nil
}
// runCommands run commands one by one.
func runCommands(u *updateAction) error {
version := launchr.Version()
launchr.Term().Info().Printfln("Starting %s installation...", version.Name)
currOS, arch, err := u.initVars()
if err != nil {
return err
}
// Check the validity of the credentials.
if err = u.validateCredentials(); err != nil {
return err
}
// Get value of Stable Release.
stableRelease, err := u.getStableRelease()
if err != nil {
return err
}
if isUpToDate(stableRelease) {
launchr.Term().Printfln("Current version of %s is up to date.", version.Name)
return nil
}
// Format the URL with the determined 'os', 'arch' and 'extension' values.
u.c.URL = fmt.Sprintf(binPathMask, baseURL, stableRelease, currOS, arch, u.ext)
launchr.Term().Printfln("Downloading file: %s", u.c.URL)
// Download file to the temp folder.
if err = u.downloadFile(); err != nil {
return err
}
launchr.Log().Debug("binary path", "path", u.fPath)
if err = u.installFile(u.fDir); err != nil {
return err
}
// Outro.
launchr.Term().Success().Printfln("%s has been installed successfully.", u.fName)
return nil
}
// isUpToDate check is current installed version of plasmactl is not up-to-date.
func isUpToDate(stableRelease string) bool {
version := launchr.Version()
return version.Version == stableRelease
}