Skip to content

Commit 8d976d4

Browse files
feat: better error message if scheme forgotten in CLI *_BASE_URL/--base-url
1 parent ed64986 commit 8d976d4

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

cmd/beeper-desktop-cli/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ func main() {
2323
prepareForAutocomplete(app)
2424
}
2525

26+
if baseURL, ok := os.LookupEnv("BEEPER_DESKTOP_BASE_URL"); ok {
27+
if err := cmd.ValidateBaseURL(baseURL, "BEEPER_DESKTOP_BASE_URL"); err != nil {
28+
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
29+
os.Exit(1)
30+
}
31+
}
32+
2633
if err := app.Run(context.Background(), os.Args); err != nil {
2734
exitCode := 1
2835

pkg/cmd/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func init() {
3939
Name: "base-url",
4040
DefaultText: "url",
4141
Usage: "Override the base URL for API requests",
42+
Validator: func(baseURL string) error {
43+
return ValidateBaseURL(baseURL, "--base-url")
44+
},
4245
},
4346
&cli.StringFlag{
4447
Name: "format",

pkg/cmd/cmdutil.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ import (
2929

3030
var OutputFormats = []string{"auto", "explore", "json", "jsonl", "pretty", "raw", "yaml"}
3131

32+
// ValidateBaseURL checks that a base URL is correctly prefixed with a protocol scheme and produces a better
33+
// error message than the person would see otherwise if it doesn't.
34+
func ValidateBaseURL(value, source string) error {
35+
if value != "" && !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") {
36+
return fmt.Errorf("%s %q is missing a scheme (expected http:// or https://)", source, value)
37+
}
38+
return nil
39+
}
40+
3241
func getDefaultRequestOptions(cmd *cli.Command) []option.RequestOption {
3342
opts := []option.RequestOption{
3443
option.WithHeader("User-Agent", fmt.Sprintf("BeeperDesktop/CLI %s", Version)),

pkg/cmd/cmdutil_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,42 @@ func TestCreateDownloadFile(t *testing.T) {
125125
assert.Equal(t, "passwd", filepath.Base(file.Name()))
126126
})
127127
}
128+
129+
func TestValidateBaseURL(t *testing.T) {
130+
t.Parallel()
131+
132+
t.Run("ValidHTTPS", func(t *testing.T) {
133+
t.Parallel()
134+
135+
require.NoError(t, ValidateBaseURL("https://api.example.com", "--base-url"))
136+
})
137+
138+
t.Run("ValidHTTP", func(t *testing.T) {
139+
t.Parallel()
140+
141+
require.NoError(t, ValidateBaseURL("http://localhost:8080", "--base-url"))
142+
})
143+
144+
t.Run("Empty", func(t *testing.T) {
145+
t.Parallel()
146+
147+
require.NoError(t, ValidateBaseURL("", "MY_BASE_URL"))
148+
})
149+
150+
t.Run("MissingScheme", func(t *testing.T) {
151+
t.Parallel()
152+
153+
err := ValidateBaseURL("localhost:8080", "MY_BASE_URL")
154+
require.Error(t, err)
155+
assert.Contains(t, err.Error(), "MY_BASE_URL")
156+
assert.Contains(t, err.Error(), "missing a scheme")
157+
})
158+
159+
t.Run("HostOnly", func(t *testing.T) {
160+
t.Parallel()
161+
162+
err := ValidateBaseURL("api.example.com", "--base-url")
163+
require.Error(t, err)
164+
assert.Contains(t, err.Error(), "--base-url")
165+
})
166+
}

0 commit comments

Comments
 (0)