-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfeedback.go
More file actions
269 lines (246 loc) · 7.25 KB
/
feedback.go
File metadata and controls
269 lines (246 loc) · 7.25 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cmd
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/localstack/lstk/internal/auth"
"github.com/localstack/lstk/internal/config"
"github.com/localstack/lstk/internal/env"
"github.com/localstack/lstk/internal/feedback"
"github.com/localstack/lstk/internal/log"
"github.com/localstack/lstk/internal/output"
"github.com/localstack/lstk/internal/telemetry"
"github.com/localstack/lstk/internal/ui"
"github.com/localstack/lstk/internal/ui/styles"
"github.com/localstack/lstk/internal/version"
"github.com/spf13/cobra"
"golang.org/x/term"
)
func newFeedbackCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "feedback",
Short: "Send feedback",
Long: "Send feedback directly to the LocalStack team.",
RunE: commandWithTelemetry("feedback", tel, func(cmd *cobra.Command, args []string) error {
sink := output.NewPlainSink(cmd.OutOrStdout())
if !isInteractiveMode(cfg) {
return fmt.Errorf("feedback requires an interactive terminal")
}
message, confirmed, err := collectFeedbackInteractively(cmd, sink, cfg)
if err != nil {
return err
}
if !confirmed {
return nil
}
if strings.TrimSpace(cfg.AuthToken) == "" {
return fmt.Errorf("feedback requires authentication")
}
client := feedback.NewClient(cfg.APIEndpoint)
submit := func(ctx context.Context, submitSink output.Sink) error {
output.EmitSpinnerStart(submitSink, "Submitting feedback")
err := client.Submit(ctx, feedback.SubmitInput{
Message: message,
AuthToken: cfg.AuthToken,
Context: buildFeedbackContext(cfg),
})
output.EmitSpinnerStop(submitSink)
if err != nil {
return err
}
output.EmitInfo(submitSink, styles.Success.Render(output.SuccessMarker())+" Thank you for your feedback!")
return nil
}
err = ui.RunFeedback(cmd.Context(), submit)
if err != nil {
return err
}
return nil
}),
}
return cmd
}
func collectFeedbackInteractively(cmd *cobra.Command, sink output.Sink, cfg *env.Env) (string, bool, error) {
file, ok := cmd.InOrStdin().(*os.File)
if !ok {
return "", false, fmt.Errorf("interactive feedback requires a terminal")
}
output.EmitInfo(sink, "What's your feedback?")
output.EmitSecondary(sink, styles.Secondary.Render("> Press enter to submit or esc to cancel"))
message, cancelled, err := readInteractiveLine(file, cmd.OutOrStdout())
if err != nil {
return "", false, err
}
if cancelled {
output.EmitSecondary(sink, styles.Secondary.Render("Cancelled feedback submission"))
return "", false, nil
}
if strings.TrimSpace(message) == "" {
return "", false, fmt.Errorf("feedback message cannot be empty")
}
ctx := buildFeedbackContext(cfg)
output.EmitInfo(sink, "")
output.EmitInfo(sink, "This report will include:")
output.EmitInfo(sink, "- Feedback: "+styles.Secondary.Render(message))
output.EmitInfo(sink, "- Version (lstk): "+styles.Secondary.Render(version.Version()))
output.EmitInfo(sink, "- OS (arch): "+styles.Secondary.Render(fmt.Sprintf("%s (%s)", runtime.GOOS, runtime.GOARCH)))
output.EmitInfo(sink, "- Installation: "+styles.Secondary.Render(orUnknown(ctx.InstallMethod)))
output.EmitInfo(sink, "- Shell: "+styles.Secondary.Render(orUnknown(ctx.Shell)))
output.EmitInfo(sink, "- Container runtime: "+styles.Secondary.Render(orUnknown(ctx.ContainerRuntime)))
output.EmitInfo(sink, "- Auth: "+styles.Secondary.Render(authStatus(ctx.AuthConfigured)))
output.EmitInfo(sink, "- Config: "+styles.Secondary.Render(orUnknown(ctx.ConfigPath)))
output.EmitInfo(sink, "")
output.EmitInfo(sink, renderConfirmationPrompt("Confirm submitting this feedback?"))
submit, err := readConfirmation(file, cmd.OutOrStdout())
if err != nil {
return "", false, err
}
if !submit {
output.EmitSecondary(sink, styles.Secondary.Render("Cancelled feedback submission"))
return "", false, nil
}
return message, true, nil
}
func buildFeedbackContext(cfg *env.Env) feedback.Context {
configPath, _ := config.ConfigFilePath()
authConfigured := strings.TrimSpace(cfg.AuthToken) != ""
if !authConfigured {
if tokenStorage, err := auth.NewTokenStorage(cfg.ForceFileKeyring, log.Nop()); err == nil {
if token, err := tokenStorage.GetAuthToken(); err == nil && strings.TrimSpace(token) != "" {
authConfigured = true
}
}
}
return feedback.Context{
AuthConfigured: authConfigured,
InstallMethod: feedback.DetectInstallMethod(),
Shell: detectShell(),
ContainerRuntime: detectContainerRuntime(cfg),
ConfigPath: configPath,
}
}
func detectShell() string {
shellPath := strings.TrimSpace(os.Getenv("SHELL"))
if shellPath == "" {
return "unknown"
}
return filepath.Base(shellPath)
}
func authStatus(v bool) string {
if v {
return "Configured"
}
return "Not Configured"
}
func detectContainerRuntime(cfg *env.Env) string {
if strings.TrimSpace(cfg.DockerHost) != "" {
return "docker"
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "docker"
}
switch {
case fileExists(filepath.Join(homeDir, ".orbstack", "run", "docker.sock")):
return "orbstack"
case fileExists(filepath.Join(homeDir, ".colima", "default", "docker.sock")),
fileExists(filepath.Join(homeDir, ".colima", "docker.sock")):
return "colima"
default:
return "docker"
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func orUnknown(v string) string {
if strings.TrimSpace(v) == "" {
return "unknown"
}
return v
}
func renderConfirmationPrompt(question string) string {
return styles.Secondary.Render("? ") +
styles.Message.Render(question) +
styles.Secondary.Render(" [Y/n]")
}
func readInteractiveLine(in *os.File, out io.Writer) (string, bool, error) {
state, err := term.MakeRaw(int(in.Fd()))
if err != nil {
return "", false, err
}
defer func() { _ = term.Restore(int(in.Fd()), state) }()
var buf []byte
scratch := make([]byte, 1)
for {
if _, err := in.Read(scratch); err != nil {
return "", false, err
}
switch scratch[0] {
case '\r', '\n':
_, _ = io.WriteString(out, "\r\n")
return strings.TrimSpace(string(buf)), false, nil
case 27:
cancelled, err := readEscapeSequence(in)
if err != nil {
return "", false, err
}
if !cancelled {
continue
}
_, _ = io.WriteString(out, "\r\n")
return "", true, nil
case 3:
_, _ = io.WriteString(out, "\r\n")
return "", true, nil
case 127, 8:
if len(buf) == 0 {
continue
}
buf = buf[:len(buf)-1]
_, _ = io.WriteString(out, "\b \b")
default:
if scratch[0] < 32 {
continue
}
buf = append(buf, scratch[0])
_, _ = out.Write(scratch)
}
}
}
func readConfirmation(in *os.File, out io.Writer) (bool, error) {
state, err := term.MakeRaw(int(in.Fd()))
if err != nil {
return false, err
}
defer func() { _ = term.Restore(int(in.Fd()), state) }()
scratch := make([]byte, 1)
for {
if _, err := in.Read(scratch); err != nil {
return false, err
}
switch scratch[0] {
case '\r', '\n', 'y', 'Y':
_, _ = io.WriteString(out, "\r\n")
return true, nil
case 27:
cancelled, err := readEscapeSequence(in)
if err != nil {
return false, err
}
if !cancelled {
continue
}
_, _ = io.WriteString(out, "\r\n")
return false, nil
case 3, 'n', 'N':
_, _ = io.WriteString(out, "\r\n")
return false, nil
}
}
}