-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.go
241 lines (192 loc) · 5.21 KB
/
cli.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
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
package cli
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/cli/browser"
"github.com/google/go-github/v54/github"
"github.com/mcuadros/go-defaults"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/anchordotdev/cli/models"
"github.com/anchordotdev/cli/stacktrace"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
)
type Clipboard interface {
ReadAll() (string, error)
WriteAll(text string) error
}
var Executable string
var Version = struct {
Version, Commit, Date string
Os, Arch string
}{
Version: "dev",
Commit: "none",
Date: "unknown",
Os: runtime.GOOS,
Arch: runtime.GOARCH,
}
var LatestRelease *Release
type Release = github.RepositoryRelease
var SkipReleaseCheck = false
func IsDevVersion() bool {
return Version.Version == "dev"
}
func IsFreshLatestRelease(ctx context.Context) (bool, error) {
release, err := getLatestRelease(ctx)
if err != nil {
return true, err
}
return release.PublishedAt != nil && time.Since(release.PublishedAt.Time).Hours() < 24, nil
}
func IsUpgradeable(ctx context.Context) (bool, error) {
release, err := getLatestRelease(ctx)
if err != nil {
return false, err
}
return release.TagName != nil && *release.TagName != ReleaseTagName(), nil
}
func getLatestRelease(ctx context.Context) (*Release, error) {
if LatestRelease != nil {
return LatestRelease, nil
}
release, _, err := github.NewClient(nil).Repositories.GetLatestRelease(ctx, "anchordotdev", "cli")
if err != nil {
return nil, err
}
LatestRelease = &Release{
PublishedAt: release.PublishedAt,
TagName: release.TagName,
}
return LatestRelease, nil
}
func UserAgent() string {
return "Anchor CLI " + VersionString()
}
func ReleaseTagName() string {
return fmt.Sprintf("v%s", Version.Version)
}
func VersionString() string {
return fmt.Sprintf("%s (%s/%s) Commit: %s BuildDate: %s", Version.Version, Version.Os, Version.Arch, Version.Commit, Version.Date)
}
var Defaults = defaultConfig()
func defaultConfig() *Config {
var cfg Config
defaults.SetDefaults(&cfg)
return &cfg
}
type UI struct {
RunTUI func(context.Context, *ui.Driver) error
}
type contextKey int
const (
configKey contextKey = iota
calledAsKey
)
func CalledAsFromContext(ctx context.Context) string {
if calledAs, ok := ctx.Value(calledAsKey).(string); ok {
return calledAs
}
return ""
}
func ConfigFromContext(ctx context.Context) *Config {
if v := ctx.Value(configKey); v != nil {
return v.(*Config)
}
return nil
}
func ConfigFromCmd(cmd *cobra.Command) *Config {
return ConfigFromContext(cmd.Context())
}
func ContextWithCalledAs(ctx context.Context, calledAs string) context.Context {
return context.WithValue(ctx, calledAsKey, calledAs)
}
func ContextWithConfig(ctx context.Context, cfg *Config) context.Context {
return context.WithValue(ctx, configKey, cfg)
}
func ReportError(ctx context.Context, err error, drv *ui.Driver, cmd *cobra.Command, args []string) {
cfg := ConfigFromContext(ctx)
var flags []string
cmd.Flags().Visit(func(flag *pflag.Flag) {
flags = append(flags, flag.Name)
})
q := url.Values{}
q.Add("title", fmt.Sprintf("Error: %s", err.Error()))
var body strings.Builder
fmt.Fprintf(&body, "**Are there any additional details you would like to share?**\n")
fmt.Fprintf(&body, "\n")
fmt.Fprintf(&body, "---\n")
fmt.Fprintf(&body, "\n")
fmt.Fprintf(&body, "**Command:** `%s`\n", cmd.CommandPath())
var executable string
if Executable != "" {
executable = Executable
} else {
executable, _ = os.Executable()
}
if executable != "" {
fmt.Fprintf(&body, "**Executable:** `%s`\n", executable)
}
fmt.Fprintf(&body, "**Version:** `%s`\n", VersionString())
fmt.Fprintf(&body, "**Arguments:** `[%s]`\n", strings.Join(args, ", "))
fmt.Fprintf(&body, "**Flags:** `[%s]`\n", strings.Join(flags, ", "))
fmt.Fprintf(&body, "**Timestamp:** `%s`\n", cfg.Timestamp().Format(time.RFC3339Nano))
var stackerr stacktrace.Error
if errors.As(err, &stackerr) {
fmt.Fprintf(&body, "**Stack:**\n```\n%s\n```\n", stackerr.Stack)
}
fmt.Fprintf(&body, "**Stdout:**\n```\n%s\n```\n", strings.TrimRight(drv.ErrorView(), "\n"))
q.Add("body", body.String())
reportErrorConfirmCh := make(chan struct{})
drv.Activate(ctx, &models.ReportError{
ConfirmCh: reportErrorConfirmCh,
Cmd: cmd,
Args: args,
Msg: err.Error(),
})
if !cfg.NonInteractive {
<-reportErrorConfirmCh
}
newIssueURL := fmt.Sprintf("https://github.com/anchordotdev/cli/issues/new?%s", q.Encode())
// FIXME: ? remove config val, switch to mocking this to always err in tests
if cfg.Test.Browserless {
drv.Activate(ctx, &models.Browserless{Url: newIssueURL})
} else {
if err := browser.OpenURL(newIssueURL); err != nil {
drv.Activate(ctx, &models.Browserless{Url: newIssueURL})
}
}
}
type UserError struct {
Err error
}
func (u UserError) Error() string { return u.Err.Error() }
func isReportable(err error) bool {
switch err.(type) {
case UserError:
return false
case ui.Error:
return false
}
var terr truststore.Error
if errors.As(err, &terr) {
return false
}
eerr := new(exec.ExitError)
if errors.As(err, &eerr) {
return false
}
switch err {
case context.Canceled:
return false
}
return true
}