Skip to content

Commit f7651e1

Browse files
committed
Show API key settings link when WHEROBOTS_API_KEY is missing
When the user hasn't set WHEROBOTS_API_KEY, the error now includes a direct link to the console settings page (/settings#api-keys) and the export command to configure it. The link derives its host from WHEROBOTS_API_URL so it works with custom API hosts (e.g. api.staging.wherobots.com → staging.wherobots.com/settings#api-keys). Ref: EWT-4554
1 parent 92c8fad commit f7651e1

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

internal/config/config.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func Load() (Config, error) {
4343
apiKey := strings.TrimSpace(os.Getenv(envWherobotsAPIKey))
4444
if apiKey == "" {
4545
return Config{}, fmt.Errorf(
46-
"%s is required\n\nTo create an API key, visit: https://cloud.wherobots.com/apiKey\nThen export it:\n\n export %s='<your-api-key>'",
47-
envWherobotsAPIKey, envWherobotsAPIKey,
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,
4848
)
4949
}
5050

@@ -125,6 +125,19 @@ func parseDuration(raw string, fallback time.Duration) (time.Duration, error) {
125125
return d, nil
126126
}
127127

128+
// apiKeyURL derives the console settings URL from the resolved OpenAPI spec URL.
129+
// It strips the "api." prefix from the host (e.g. api.cloud.wherobots.com → cloud.wherobots.com)
130+
// and appends /settings#api-keys.
131+
func apiKeyURL(openAPISpecURL string) string {
132+
parsed, err := url.Parse(openAPISpecURL)
133+
if err != nil {
134+
return "https://cloud.wherobots.com/settings#api-keys"
135+
}
136+
host := parsed.Hostname()
137+
host = strings.TrimPrefix(host, "api.")
138+
return fmt.Sprintf("%s://%s/settings#api-keys", parsed.Scheme, host)
139+
}
140+
128141
func getenvDefault(key, fallback string) string {
129142
if value := os.Getenv(key); value != "" {
130143
return value

internal/config/config_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,28 @@ func TestLoadBuildsSpecURLFromWherobotsAPIURL(t *testing.T) {
4242
}
4343

4444
func TestLoadRequiresWherobotsAPIKey(t *testing.T) {
45-
t.Setenv("WHEROBOTS_API_URL", "https://api.example.com")
45+
t.Setenv("WHEROBOTS_API_URL", "")
46+
t.Setenv("WHEROBOTS_API_KEY", "")
47+
48+
_, err := Load()
49+
if err == nil {
50+
t.Fatalf("expected Load() error")
51+
}
52+
if !strings.Contains(err.Error(), "https://cloud.wherobots.com/settings#api-keys") {
53+
t.Fatalf("error should contain default API key URL, got: %v", err)
54+
}
55+
}
56+
57+
func TestLoadMissingKeyUsesCustomAPIHost(t *testing.T) {
58+
t.Setenv("WHEROBOTS_API_URL", "https://api.staging.wherobots.com")
4659
t.Setenv("WHEROBOTS_API_KEY", "")
4760

4861
_, err := Load()
4962
if err == nil {
5063
t.Fatalf("expected Load() error")
5164
}
52-
if !strings.Contains(err.Error(), "https://cloud.wherobots.com/apiKey") {
53-
t.Fatalf("error should contain API key URL, got: %v", err)
65+
if !strings.Contains(err.Error(), "https://staging.wherobots.com/settings#api-keys") {
66+
t.Fatalf("error should contain custom API key URL, got: %v", err)
5467
}
5568
}
5669

0 commit comments

Comments
 (0)