Repository navigation
Expand file tree
/
Copy pathoutput.go
More file actions
254 lines (246 loc) · 6.74 KB
/
Copy pathoutput.go
File metadata and controls
254 lines (246 loc) · 6.74 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
package output
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/diffscope/diffscope-package-manager/internal/download"
"github.com/diffscope/diffscope-package-manager/internal/netpolicy"
"io"
"regexp"
"strings"
"time"
"github.com/diffscope/diffscope-package-manager/internal/packagemanager"
repo "github.com/diffscope/diffscope-package-manager/packagerepository"
)
type ReportedError struct{ error }
func (e *ReportedError) Unwrap() error { return e.error }
type Renderer struct {
Command string
JSON bool
Language string
PackagesDir string
DryRun bool
Out, Err io.Writer
Mutation bool
ProgressMode string
downloadProgressAt map[string]time.Time
writeError error
diagnostics []string
style *textStyles
errStyle *textStyles
packFiles int
packBytes int64
remoteInstall bool
live *liveProgress
progressInitialized bool
lastProgress packagemanager.Progress
progressAt time.Time
restoreTerminal []func() error
}
func (r *Renderer) write(v any) {
if r.writeError == nil {
// Redact presentation copies only. Internal manifests and cache keys keep
// their original bytes and exact URLs for verification and requests.
b, err := json.Marshal(v)
if err != nil {
r.writeError = err
return
}
var presentation any
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()
if err = decoder.Decode(&presentation); err != nil {
r.writeError = err
return
}
r.writeError = json.NewEncoder(r.Out).Encode(redactValues(presentation))
}
}
var outputURL = regexp.MustCompile(`https?://[^\s"<>]+`)
func redactValues(v any) any {
switch x := v.(type) {
case string:
return outputURL.ReplaceAllStringFunc(x, netpolicy.Redact)
case map[string]any:
for k, value := range x {
x[k] = redactValues(value)
}
case []any:
for i, value := range x {
x[i] = redactValues(value)
}
}
return v
}
func (r *Renderer) Observer(e packagemanager.Event) {
if e.Event == "progress" {
if p, ok := e.Data.(download.Progress); ok {
r.DownloadProgress(p)
return
}
if p, ok := e.Data.(packagemanager.Progress); ok {
r.Progress(p)
}
return
}
if r.JSON {
r.write(map[string]any{"command": r.Command, "event": e.Event, "data": e.Data})
return
}
switch e.Event {
case "inspect_start", "inspect_ok", "staged":
default:
r.pauseProgress()
}
r.textEvent(e)
}
func (r *Renderer) Diagnostics(diagnostics []string) {
r.diagnostics = diagnostics
if r.JSON {
return
}
if len(diagnostics) > 0 {
r.pauseProgress()
}
for _, d := range diagnostics {
fmt.Fprintln(r.Err, r.errorStyles().warning.Render("!")+" "+outputURL.ReplaceAllStringFunc(safeText(d), netpolicy.Redact))
}
}
func (r *Renderer) Finish(err error) error {
if err != nil && r.live != nil && r.lastProgress.Phase == "inspect" && r.lastProgress.State != "done" {
r.live.failInspection(r.lastProgress.Target)
}
r.stopProgress()
defer r.Close()
if err == nil {
err = r.writeError
}
if err == nil {
return nil
}
code := "IO_ERROR"
details := any(nil)
var e *packagemanager.Error
if errors.As(err, &e) {
code = e.Code
details = e.Details
}
if errors.Is(err, context.Canceled) {
code = "CANCELLED"
}
payload := map[string]any{"code": code, "message": err.Error()}
if details != nil {
payload["details"] = details
}
if r.JSON {
if r.Mutation {
result := map[string]any{"command": r.Command, "event": "error", "error": payload, "diagnostics": r.diagnostics}
if e != nil && e.DiagnosticDetails != nil {
result["diagnosticDetails"] = e.DiagnosticDetails
}
r.write(result)
} else {
r.write(map[string]any{"ok": false, "command": r.Command, "error": payload, "diagnostics": r.diagnostics})
}
} else {
fmt.Fprintln(r.Err, r.errorStyles().failure.Render("✗ Error: "+outputURL.ReplaceAllStringFunc(safeText(err.Error()), netpolicy.Redact)))
}
return &ReportedError{err}
}
func (r *Renderer) Query(data any, diagnostics []string) error {
if r.JSON {
result := map[string]any{"ok": true, "command": r.Command, "data": data, "diagnostics": diagnostics}
if info, ok := data.(packagemanager.RemoteInfo); ok {
result["diagnosticDetails"] = info.DiagnosticDetails
}
r.write(result)
} else {
r.Diagnostics(diagnostics)
switch v := data.(type) {
case packagemanager.Info:
r.info(v)
case repo.VersionsIndex:
r.section("Available versions")
for _, entry := range v.Versions {
suffix := ""
if entry.Deprecated {
suffix = " [deprecated]"
}
r.printf(" %s%s\n", r.styles().identity.Render(v.ID+"="+entry.Version.String()), suffix)
}
case packagemanager.RemoteInfo:
r.remoteInfo(v)
case []packagemanager.InstalledPackage:
r.List(v, []string{"id", "version"})
default:
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
_, err = fmt.Fprintln(r.Out, string(b))
}
r.writeError = err
}
}
return r.Finish(nil)
}
func ParseColumns(s string) ([]string, error) {
if strings.TrimSpace(s) == "" {
s = "id,version"
}
columns := strings.Split(s, ",")
allowed := map[string]bool{"id": true, "version": true, "name": true, "vendor": true, "description": true, "installed-at": true, "hash": true, "directory": true, "compat-version": true}
for i, c := range columns {
c = strings.ToLower(strings.TrimSpace(c))
switch c {
case "installed_at", "installedat":
c = "installed-at"
case "compat_version", "compatversion":
c = "compat-version"
}
if !allowed[c] {
return nil, fmt.Errorf("The list column %q is unknown.", c)
}
columns[i] = c
}
return columns, nil
}
func (r *Renderer) WriteError() error {
if r.writeError == nil && r.live != nil {
return r.live.outputError()
}
return r.writeError
}
func (r *Renderer) DownloadProgress(p download.Progress) {
if r.JSON {
r.write(map[string]any{"command": r.Command, "event": "progress", "data": p})
return
}
if r.ProgressMode == "none" {
return
}
r.ensureLiveProgress()
if r.live != nil {
r.live.download(p)
return
}
if r.downloadProgressAt == nil {
r.downloadProgressAt = map[string]time.Time{}
}
if p.State == "update" && time.Since(r.downloadProgressAt[p.TaskID]) < 2*time.Second {
return
}
r.downloadProgressAt[p.TaskID] = time.Now()
total := "unknown"
if p.TotalKnown {
total = fmt.Sprintf("%d", p.Total)
}
line := fmt.Sprintf("%s %s %d/%s bytes %.1f KiB/s [%s]", p.Target, p.Status, p.Current, total, p.BytesPerSecond/1024, p.CacheStatus)
if p.State == "source-switch" {
line += " source " + p.PreviousSourceID[:min(8, len(p.PreviousSourceID))] + " -> " + p.SourceID[:min(8, len(p.SourceID))]
}
_, err := fmt.Fprintln(r.Err, line)
if err != nil && r.writeError == nil {
r.writeError = err
}
}