-
Notifications
You must be signed in to change notification settings - Fork 33
/
extension.go
204 lines (169 loc) · 4.28 KB
/
extension.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cmd
import (
"fmt"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/extension"
"github.com/stateful/runme/internal/log"
"github.com/stateful/runme/internal/tui"
"github.com/stateful/runme/internal/tui/prompt"
"go.uber.org/zap"
)
func extensionCmd() *cobra.Command {
var force bool
cmd := &cobra.Command{
Use: "extension",
Short: "Check your Stateful VS Code extension status",
RunE: func(cmd *cobra.Command, args []string) error {
model := tui.NewModel(
newExtensionerModel(force),
tui.MinimalKeyMap,
tui.DefaultStyles,
)
return newProgram(cmd, model).Start()
},
}
cmd.Flags().BoolVarP(&force, "force", "f", false, "updates extension if it's already installed")
return cmd
}
type extCheckMsg struct {
Installed bool
Name string
Err error
}
type prepExtInstallationMsg struct{}
type extUpdateMsg struct {
Updated bool
Name string
Err error
}
type extensionerModel struct {
force bool
loading bool
loadingMsg string
spinner spinner.Model
successMsg string
prompting bool
prompt prompt.QuestionModel
log *zap.Logger
}
func newExtensionerModel(force bool) extensionerModel {
s := spinner.New()
s.Spinner = spinner.Line
return extensionerModel{
force: force,
prompt: prompt.NewQuestionModel("Do you want to install the extension?"),
spinner: s,
loading: true,
loadingMsg: "checking status of the extension...",
log: log.Get().Named("command.extensionerModel"),
}
}
func (m extensionerModel) Init() tea.Cmd {
return tea.Batch(
m.prompt.Init(),
func() tea.Msg {
fullName, installed, err := extensioner.IsInstalled()
return extCheckMsg{
Installed: installed,
Name: fullName,
Err: err,
}
},
)
}
func (m extensionerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case prompt.Confirmed:
m.prompting = false
if msg.Value {
return m, func() tea.Msg { return prepExtInstallationMsg{} }
}
m.successMsg = fmt.Sprintf("You can install the extension manually using: %q", extension.InstallCommand())
return m, tea.Quit
case prepExtInstallationMsg:
m.loading = true
m.loadingMsg = "installing the extension..."
return m, m.installExtension
case extCheckMsg:
m.loading = false
if msg.Err != nil {
return m, tui.Cmd(tui.ErrorMsg{Err: msg.Err, Exit: true})
}
if msg.Installed && !m.force {
m.successMsg = fmt.Sprintf(`It looks like you're set with %s 🙌 `, msg.Name)
return m, tea.Quit
}
if msg.Installed && m.force {
m.loading = true
m.loadingMsg = "updating the extension..."
return m, func() tea.Msg {
if err := extensioner.Update(); err != nil {
return extUpdateMsg{Err: err}
}
updatedFullName, _, err := extensioner.IsInstalled()
if err != nil {
return extUpdateMsg{
Err: err,
}
}
return extUpdateMsg{
Updated: true,
Name: updatedFullName,
}
}
}
if !msg.Installed && !m.force {
m.prompting = true
return m, m.prompt.Focus()
}
if !msg.Installed && m.force {
return m, func() tea.Msg { return prepExtInstallationMsg{} }
}
return m, nil
case extUpdateMsg:
m.log.Debug("received extUpdateMsg", zap.Any("msg", msg))
m.loading = false
m.prompting = false
if msg.Err != nil {
return m, tui.Cmd(tui.ErrorMsg{Err: msg.Err, Exit: true})
}
if msg.Updated {
m.successMsg = fmt.Sprintf(`Successfully updated to %s 🙌 `, msg.Name)
}
return m, tea.Quit
}
var cmd tea.Cmd
m.prompt, cmd = m.prompt.Update(msg)
return m, cmd
}
func (m extensionerModel) installExtension() tea.Msg {
if err := extensioner.Install(); err != nil {
return extUpdateMsg{Err: err}
}
installedFullName, _, err := extensioner.IsInstalled()
if err != nil {
return extUpdateMsg{Err: err}
}
return extUpdateMsg{
Updated: true,
Name: installedFullName,
}
}
func (m extensionerModel) View() string {
var s string
if m.loading {
s += m.spinner.View() + " "
s += m.loadingMsg + "\n"
} else if m.prompting {
s += m.prompt.View() + "\n"
} else {
s += tui.ColorSuccess.Render(m.successMsg) + "\n"
}
return s
}