-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (73 loc) · 2.22 KB
/
Copy pathmain.go
File metadata and controls
85 lines (73 loc) · 2.22 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"slices"
"github.com/stainless-api/stainless-api-cli/pkg/cmd"
"github.com/stainless-api/stainless-api-cli/pkg/console"
"github.com/stainless-api/stainless-api-go"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v3"
)
func main() {
updateCheck := cmd.CheckForUpdate()
app := cmd.Command
if slices.Contains(os.Args, "__complete") {
prepareForAutocomplete(app)
}
if baseURL, ok := os.LookupEnv("STAINLESS_BASE_URL"); ok {
if err := cmd.ValidateBaseURL(baseURL, "STAINLESS_BASE_URL"); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
checkVersionUpdate(updateCheck)
if err := app.Run(context.Background(), os.Args); err != nil {
exitCode := 1
// Check if error has a custom exit code
if exitErr, ok := err.(cli.ExitCoder); ok {
exitCode = exitErr.ExitCode()
}
var apierr *stainless.Error
if errors.As(err, &apierr) {
fmt.Fprintf(os.Stderr, "%s %q: %d %s\n", apierr.Request.Method, apierr.Request.URL, apierr.Response.StatusCode, http.StatusText(apierr.Response.StatusCode))
format := app.String("format-error")
json := gjson.Parse(apierr.RawJSON())
show_err := cmd.ShowJSON(json, cmd.ShowJSONOpts{
ExplicitFormat: app.IsSet("format-error"),
Format: format,
Title: "Error",
Transform: app.String("transform-error"),
})
if show_err != nil {
// Just print the original error:
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
} else {
if cmd.CommandErrorBuffer.Len() > 0 {
os.Stderr.Write(cmd.CommandErrorBuffer.Bytes())
} else {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
}
checkVersionUpdate(updateCheck)
os.Exit(exitCode)
}
}
func prepareForAutocomplete(cmd *cli.Command) {
// urfave/cli does not handle flag completions and will print an error if we inspect a command with invalid flags.
// This skips that sort of validation
cmd.SkipFlagParsing = true
for _, child := range cmd.Commands {
prepareForAutocomplete(child)
}
}
func checkVersionUpdate(updateCheck <-chan string) {
if msg := <-updateCheck; msg != "" {
console.Warn("%s", msg)
}
}