Repository navigation
Expand file tree
/
Copy pathcmdutil.go
More file actions
525 lines (466 loc) · 14.4 KB
/
Copy pathcmdutil.go
File metadata and controls
525 lines (466 loc) · 14.4 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/context-dot-dev/context-dev-cli/internal/jsonview"
"github.com/context-dot-dev/context-go-sdk/v2/option"
"github.com/charmbracelet/x/term"
"github.com/itchyny/json2yaml"
"github.com/muesli/reflow/wrap"
"github.com/tidwall/gjson"
"github.com/tidwall/pretty"
"github.com/urfave/cli/v3"
)
var OutputFormats = []string{"auto", "explore", "json", "jsonl", "pretty", "raw", "yaml"}
// ValidateBaseURL checks that a base URL is correctly prefixed with a protocol scheme and produces a better
// error message than the person would see otherwise if it doesn't.
func ValidateBaseURL(value, source string) error {
if value != "" && !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") {
return fmt.Errorf("%s %q is missing a scheme (expected http:// or https://)", source, value)
}
return nil
}
func getDefaultRequestOptions(cmd *cli.Command) []option.RequestOption {
opts := []option.RequestOption{
option.WithHeader("User-Agent", fmt.Sprintf("ContextDev/CLI %s", Version)),
option.WithHeader("X-Stainless-Lang", "cli"),
option.WithHeader("X-Stainless-Package-Version", Version),
option.WithHeader("X-Stainless-Runtime", "cli"),
option.WithHeader("X-Stainless-CLI-Command", cmd.FullName()),
}
if cmd.IsSet("api-key") {
opts = append(opts, option.WithAPIKey(cmd.String("api-key")))
}
// Override base URL if the --base-url flag is provided
if baseURL := cmd.String("base-url"); baseURL != "" {
opts = append(opts, option.WithBaseURL(baseURL))
}
return opts
}
var debugMiddlewareOption = option.WithMiddleware(
func(r *http.Request, mn option.MiddlewareNext) (*http.Response, error) {
logger := log.Default()
if reqBytes, err := httputil.DumpRequest(r, true); err == nil {
logger.Printf("Request Content:\n%s\n", reqBytes)
}
resp, err := mn(r)
if err != nil {
return resp, err
}
if respBytes, err := httputil.DumpResponse(resp, true); err == nil {
logger.Printf("Response Content:\n%s\n", respBytes)
}
return resp, err
},
)
// isInputPiped tries to check for input being piped into the CLI which tells us that we should try to read
// from stdin. This can be a bit tricky in some cases like when an stdin is connected to a pipe but nothing is
// being piped in (this may happen in some environments like Cursor's integration terminal or CI), which is
// why this function is a little more elaborate than it'd be otherwise.
func isInputPiped() bool {
stat, err := os.Stdin.Stat()
if err != nil {
return false
}
mode := stat.Mode()
// Regular file (redirect like < file.txt) — only if non-empty.
//
// Notably, on Unix the case like `< /dev/null` is handled below because `/dev/null` is not a regular
// file. On Windows, NUL appears as a regular file with size 0, so it's also handled correctly.
if mode.IsRegular() && stat.Size() > 0 {
return true
}
// For pipes/sockets (e.g. `echo foo | stainlesscli`), use an OS-specific check to determine whether
// data is actually available. Some environments like Cursor's integrated terminal connect stdin as a
// pipe even when nothing is being piped.
if mode&(os.ModeNamedPipe|os.ModeSocket) != 0 {
// Defined in either cmdutil_unix.go or cmdutil_windows.go.
return isPipedDataAvailableOSSpecific()
}
return false
}
func isTerminal(w io.Writer) bool {
switch v := w.(type) {
case *os.File:
return term.IsTerminal(v.Fd())
default:
return false
}
}
func streamOutput(label string, generateOutput func(w *os.File) error) error {
// For non-tty output (probably a pipe), write directly to stdout
if !isTerminal(os.Stdout) {
return streamToStdout(generateOutput)
}
// When streaming output on Unix-like systems, there's a special trick involving creating two socket pairs
// that we prefer because it supports small buffer sizes which results in less pagination per buffer. The
// constructs needed to run it don't exist on Windows builds, so we have this function broken up into
// OS-specific files with conditional build comments. Under Windows (and in case our fancy constructs fail
// on Unix), we fall back to using pipes (`streamToPagerWithPipe`), which are OS agnostic.
//
// Defined in either cmdutil_unix.go or cmdutil_windows.go.
return streamOutputOSSpecific(label, generateOutput)
}
func streamToPagerWithPipe(label string, generateOutput func(w *os.File) error) error {
r, w, err := os.Pipe()
if err != nil {
return err
}
defer r.Close()
defer w.Close()
pagerProgram := os.Getenv("PAGER")
if pagerProgram == "" {
pagerProgram = "less"
}
if _, err := exec.LookPath(pagerProgram); err != nil {
return err
}
cmd := exec.Command(pagerProgram)
cmd.Stdin = r
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"LESS=-X -r -P "+label,
"MORE=-r -P "+label,
)
if err := cmd.Start(); err != nil {
return err
}
if err := r.Close(); err != nil {
return err
}
// If we would be streaming to a terminal and aren't forcing color one way
// or the other, we should configure things to use color so the pager gets
// colorized input.
if isTerminal(os.Stdout) && os.Getenv("FORCE_COLOR") == "" {
os.Setenv("FORCE_COLOR", "1")
}
if err := generateOutput(w); err != nil && !strings.Contains(err.Error(), "broken pipe") {
return err
}
w.Close()
return cmd.Wait()
}
func streamToStdout(generateOutput func(w *os.File) error) error {
signal.Ignore(syscall.SIGPIPE)
err := generateOutput(os.Stdout)
if err != nil && strings.Contains(err.Error(), "broken pipe") {
return nil
}
return err
}
// writeBinaryResponse writes a binary response to stdout or a file.
//
// Takes in a stdout reference so we can test this function without overriding os.Stdout in tests.
func writeBinaryResponse(response *http.Response, stdout io.Writer, outfile string) (string, error) {
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
switch outfile {
case "-", "/dev/stdout":
_, err := stdout.Write(body)
return "", err
case "":
// If output file is unspecified, then print to stdout for plain text or
// if stdout is not a terminal:
if !isTerminal(os.Stdout) || isUTF8TextFile(body) {
_, err := stdout.Write(body)
return "", err
}
// If response has a suggested filename in the content-disposition
// header, then use that (with an optional suffix to ensure uniqueness):
file, err := createDownloadFile(response, body)
if err != nil {
return "", err
}
defer file.Close()
if _, err := file.Write(body); err != nil {
return "", err
}
return fmt.Sprintf("Wrote output to: %s", file.Name()), nil
default:
if err := os.WriteFile(outfile, body, 0644); err != nil {
return "", err
}
return fmt.Sprintf("Wrote output to: %s", outfile), nil
}
}
// Return a writable file handle to a new file, which attempts to choose a good filename
// based on the Content-Disposition header or sniffing the MIME filetype of the response.
func createDownloadFile(response *http.Response, data []byte) (*os.File, error) {
filename := "file"
// If the header provided an output filename, use that
disp := response.Header.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(disp)
if err == nil {
if dispFilename, ok := params["filename"]; ok {
// Only use the last path component to prevent directory traversal
filename = filepath.Base(dispFilename)
// Try to create the file with exclusive flag to avoid race conditions
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err == nil {
return file, nil
}
}
}
// If file already exists, create a unique filename using CreateTemp
ext := filepath.Ext(filename)
if ext == "" {
ext = guessExtension(data)
}
base := strings.TrimSuffix(filename, ext)
return os.CreateTemp(".", base+"-*"+ext)
}
func guessExtension(data []byte) string {
ct := http.DetectContentType(data)
// Prefer common extensions over obscure ones
switch ct {
case "application/gzip":
return ".gz"
case "application/pdf":
return ".pdf"
case "application/zip":
return ".zip"
case "audio/mpeg":
return ".mp3"
case "image/bmp":
return ".bmp"
case "image/gif":
return ".gif"
case "image/jpeg":
return ".jpg"
case "image/png":
return ".png"
case "image/webp":
return ".webp"
case "video/mp4":
return ".mp4"
}
exts, err := mime.ExtensionsByType(ct)
if err == nil && len(exts) > 0 {
return exts[0]
} else if isUTF8TextFile(data) {
return ".txt"
} else {
return ".bin"
}
}
func shouldUseColors(w io.Writer) bool {
force, ok := os.LookupEnv("FORCE_COLOR")
if ok {
if force == "1" {
return true
}
if force == "0" {
return false
}
}
return isTerminal(w)
}
func formatJSON(res gjson.Result, opts ShowJSONOpts) ([]byte, error) {
if opts.Transform != "" {
transformed := res.Get(opts.Transform)
if transformed.Exists() {
res = transformed
}
}
// Modeled after `jq -r` (`--raw-output`): if the result is a string, print it without JSON quotes so that
// it's easier to pipe into other programs.
if opts.RawOutput && res.Type == gjson.String {
return []byte(res.Str + "\n"), nil
}
switch strings.ToLower(opts.Format) {
case "auto":
autoOpts := opts
autoOpts.Format = "json"
autoOpts.Transform = ""
return formatJSON(res, autoOpts)
case "pretty":
return []byte(jsonview.RenderJSON(opts.Title, res) + "\n"), nil
case "json":
prettyJSON := pretty.Pretty([]byte(res.Raw))
if shouldUseColors(opts.Stdout) {
return pretty.Color(prettyJSON, pretty.TerminalStyle), nil
} else {
return prettyJSON, nil
}
case "jsonl":
// @ugly is gjson syntax for "no whitespace", so it fits on one line
oneLineJSON := res.Get("@ugly").Raw
if shouldUseColors(opts.Stdout) {
bytes := append(pretty.Color([]byte(oneLineJSON), pretty.TerminalStyle), '\n')
return bytes, nil
} else {
return []byte(oneLineJSON + "\n"), nil
}
case "raw":
return []byte(res.Raw + "\n"), nil
case "yaml":
input := strings.NewReader(res.Raw)
var yaml strings.Builder
if err := json2yaml.Convert(&yaml, input); err != nil {
return nil, err
}
_, err := opts.Stdout.Write([]byte(yaml.String()))
return nil, err
default:
return nil, fmt.Errorf("Invalid format: %s, valid formats are: %s", opts.Format, strings.Join(OutputFormats, ", "))
}
}
const warningExploreNotSupported = "Warning: Output format 'explore' not supported for non-terminal output; falling back to 'json'\n"
// ShowJSONOpts configures how JSON output is displayed.
type ShowJSONOpts struct {
ExplicitFormat bool // true if the user explicitly passed --format
Format string // output format (auto, explore, json, jsonl, pretty, raw, yaml)
RawOutput bool // like jq -r: print strings without JSON quotes
Stderr io.Writer // stderr for warnings; injectable for testing; defaults to os.Stderr
Stdout *os.File // stdout (or pager); injectable for testing; defaults to os.Stdout
Title string // display title
Transform string // GJSON path to extract before displaying
}
func (o *ShowJSONOpts) setDefaults() {
if o.Stderr == nil {
o.Stderr = os.Stderr
}
if o.Stdout == nil {
o.Stdout = os.Stdout
}
}
// ShowJSON displays a single JSON result to the user.
func ShowJSON(res gjson.Result, opts ShowJSONOpts) error {
opts.setDefaults()
switch strings.ToLower(opts.Format) {
case "auto":
autoOpts := opts
autoOpts.Format = "json"
return ShowJSON(res, autoOpts)
case "explore":
if !isTerminal(opts.Stdout) {
if opts.ExplicitFormat {
fmt.Fprint(opts.Stderr, warningExploreNotSupported)
}
jsonOpts := opts
jsonOpts.Format = "json"
return ShowJSON(res, jsonOpts)
}
if opts.Transform != "" {
transformed := res.Get(opts.Transform)
if transformed.Exists() {
res = transformed
}
}
return jsonview.ExploreJSON(opts.Title, res)
default:
bytes, err := formatJSON(res, opts)
if err != nil {
return err
}
_, err = opts.Stdout.Write(bytes)
return err
}
}
// Get the number of lines that would be output by writing the data to the terminal
func countTerminalLines(data []byte, terminalWidth int) int {
return bytes.Count([]byte(wrap.String(string(data), terminalWidth)), []byte("\n"))
}
type hasRawJSON interface {
RawJSON() string
}
// ShowJSONIterator displays an iterator of values to the user. Use itemsToDisplay = -1 for no limit.
func ShowJSONIterator[T any](iter jsonview.Iterator[T], itemsToDisplay int64, opts ShowJSONOpts) error {
opts.setDefaults()
if opts.Format == "explore" {
if isTerminal(opts.Stdout) {
return jsonview.ExploreJSONStream(opts.Title, iter)
}
if opts.ExplicitFormat {
fmt.Fprint(opts.Stderr, warningExploreNotSupported)
}
opts.Format = "json"
}
terminalWidth, terminalHeight, err := term.GetSize(os.Stdout.Fd())
if err != nil {
terminalWidth = 100
terminalHeight = 40
}
// Decide whether or not to use a pager based on whether it's a short output or a long output
usePager := false
output := []byte{}
numberOfNewlines := 0
// -1 is used to signal no limit of items to display
for itemsToDisplay != 0 && iter.Next() {
item := iter.Current()
var obj gjson.Result
if hasRaw, ok := any(item).(hasRawJSON); ok {
obj = gjson.Parse(hasRaw.RawJSON())
} else {
jsonData, err := json.Marshal(item)
if err != nil {
return err
}
obj = gjson.ParseBytes(jsonData)
}
json, err := formatJSON(obj, opts)
if err != nil {
return err
}
output = append(output, json...)
itemsToDisplay -= 1
numberOfNewlines += countTerminalLines(json, terminalWidth)
// If the output won't fit in the terminal window, stream it to a pager
if numberOfNewlines >= terminalHeight-3 {
usePager = true
break
}
}
if !usePager {
_, err := opts.Stdout.Write(output)
if err != nil {
return err
}
return iter.Err()
}
return streamOutput(opts.Title, func(pager *os.File) error {
_, err := pager.Write(output)
if err != nil {
return err
}
pagerOpts := opts
pagerOpts.Stdout = pager
for iter.Next() {
if itemsToDisplay == 0 {
break
}
item := iter.Current()
var obj gjson.Result
if hasRaw, ok := any(item).(hasRawJSON); ok {
obj = gjson.Parse(hasRaw.RawJSON())
} else {
jsonData, err := json.Marshal(item)
if err != nil {
return err
}
obj = gjson.ParseBytes(jsonData)
}
if err := ShowJSON(obj, pagerOpts); err != nil {
return err
}
itemsToDisplay -= 1
}
return iter.Err()
})
}