Skip to content

Commit 58888d5

Browse files
authored
Merge pull request #12 from wherobots/fix/help-tree-without-api-key
Defer API key check so --help and --tree work without a key
2 parents f7651e1 + cb85e29 commit 58888d5

4 files changed

Lines changed: 40 additions & 18 deletions

File tree

internal/config/config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ func Load() (Config, error) {
4141
return Config{}, err
4242
}
4343
apiKey := strings.TrimSpace(os.Getenv(envWherobotsAPIKey))
44-
if apiKey == "" {
45-
return Config{}, fmt.Errorf(
46-
"%s is required\n\nTo create an API key, visit: %s\nThen export it:\n\n export %s='<your-api-key>'",
47-
envWherobotsAPIKey, apiKeyURL(openAPIURL), envWherobotsAPIKey,
48-
)
49-
}
5044

5145
cacheRoot, err := os.UserCacheDir()
5246
if err != nil {
@@ -78,6 +72,18 @@ func Load() (Config, error) {
7872
}, nil
7973
}
8074

75+
// RequireAPIKey returns an error with setup instructions when the API key is
76+
// empty, or nil when a key is present.
77+
func (c Config) RequireAPIKey() error {
78+
if c.APIKey != "" {
79+
return nil
80+
}
81+
return fmt.Errorf(
82+
"%s is required\n\nTo create an API key, visit: %s\nThen export it:\n\n export %s='<your-api-key>'",
83+
envWherobotsAPIKey, apiKeyURL(c.OpenAPIURL), envWherobotsAPIKey,
84+
)
85+
}
86+
8187
func resolveOpenAPISpecURL(baseURL string) (string, error) {
8288
raw := strings.TrimSpace(baseURL)
8389
if raw == "" {

internal/config/config_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,48 @@ func TestLoadBuildsSpecURLFromWherobotsAPIURL(t *testing.T) {
4141
}
4242
}
4343

44-
func TestLoadRequiresWherobotsAPIKey(t *testing.T) {
44+
func TestLoadSucceedsWithoutAPIKey(t *testing.T) {
4545
t.Setenv("WHEROBOTS_API_URL", "")
4646
t.Setenv("WHEROBOTS_API_KEY", "")
4747

48-
_, err := Load()
48+
cfg, err := Load()
49+
if err != nil {
50+
t.Fatalf("Load() error = %v", err)
51+
}
52+
if cfg.APIKey != "" {
53+
t.Fatalf("APIKey = %q, want empty", cfg.APIKey)
54+
}
55+
}
56+
57+
func TestRequireAPIKeyErrorsWithDefaultURL(t *testing.T) {
58+
cfg := Config{OpenAPIURL: "https://api.cloud.wherobots.com/openapi.json"}
59+
err := cfg.RequireAPIKey()
4960
if err == nil {
50-
t.Fatalf("expected Load() error")
61+
t.Fatalf("expected error")
5162
}
5263
if !strings.Contains(err.Error(), "https://cloud.wherobots.com/settings#api-keys") {
5364
t.Fatalf("error should contain default API key URL, got: %v", err)
5465
}
5566
}
5667

57-
func TestLoadMissingKeyUsesCustomAPIHost(t *testing.T) {
58-
t.Setenv("WHEROBOTS_API_URL", "https://api.staging.wherobots.com")
59-
t.Setenv("WHEROBOTS_API_KEY", "")
60-
61-
_, err := Load()
68+
func TestRequireAPIKeyErrorsWithCustomURL(t *testing.T) {
69+
cfg := Config{OpenAPIURL: "https://api.staging.wherobots.com/openapi.json"}
70+
err := cfg.RequireAPIKey()
6271
if err == nil {
63-
t.Fatalf("expected Load() error")
72+
t.Fatalf("expected error")
6473
}
6574
if !strings.Contains(err.Error(), "https://staging.wherobots.com/settings#api-keys") {
6675
t.Fatalf("error should contain custom API key URL, got: %v", err)
6776
}
6877
}
6978

79+
func TestRequireAPIKeySucceedsWithKey(t *testing.T) {
80+
cfg := Config{APIKey: "key-1"}
81+
if err := cfg.RequireAPIKey(); err != nil {
82+
t.Fatalf("unexpected error: %v", err)
83+
}
84+
}
85+
7086
func TestLoadReadsUploadPathConfig(t *testing.T) {
7187
t.Setenv("WHEROBOTS_API_URL", "")
7288
t.Setenv("WHEROBOTS_API_KEY", "key-1")

internal/executor/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func BuildRequest(
4646
if runtimeSpec.BaseURL == "" {
4747
return nil, fmt.Errorf("missing base URL (no OpenAPI servers and WHEROBOTS_API_URL has no resolvable host)")
4848
}
49-
if cfg.APIKey == "" {
50-
return nil, fmt.Errorf("WHEROBOTS_API_KEY is required")
49+
if err := cfg.RequireAPIKey(); err != nil {
50+
return nil, err
5151
}
5252
if len(pathArgs) != len(op.PathParamOrder) {
5353
return nil, fmt.Errorf("expected %d path arguments, got %d", len(op.PathParamOrder), len(pathArgs))

internal/executor/request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestBuildRequestMissingAPIKeyReturnsError(t *testing.T) {
7575
op := &spec.Operation{Method: "GET", Path: "/users"}
7676

7777
_, err := BuildRequest(context.Background(), cfg, runtimeSpec, op, nil, nil, "")
78-
if err == nil || !strings.Contains(err.Error(), "WHEROBOTS_API_KEY is required") {
78+
if err == nil || !strings.Contains(err.Error(), "WHEROBOTS_API_KEY") {
7979
t.Fatalf("expected API key error, got %v", err)
8080
}
8181
}

0 commit comments

Comments
 (0)