-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (59 loc) · 1.82 KB
/
main.go
File metadata and controls
69 lines (59 loc) · 1.82 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
// 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/beeper/desktop-api-cli/pkg/cmd"
"github.com/beeper/desktop-api-go"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v3"
)
func main() {
app := cmd.Command
if slices.Contains(os.Args, "__complete") {
prepareForAutocomplete(app)
}
if baseURL, ok := os.LookupEnv("BEEPER_DESKTOP_BASE_URL"); ok {
if err := cmd.ValidateBaseURL(baseURL, "BEEPER_DESKTOP_BASE_URL"); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
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 *beeperdesktopapi.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(os.Stdout, "Error", json, format, 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())
}
}
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)
}
}