feat: accept DISCOURSE_KEY as env alias for the API key

NewFromEnv now falls back to DISCOURSE_KEY when DISCOURSE_API_KEY
is unset (the explicit name still wins), matching the shorter
DISCOURSE_URL/DISCOURSE_KEY convention used across the MOPAC fleet
scripts. Behavioral test proves both the alias and the precedence
against the fake server.

Part of Redmine 507 (Discourse Go client).
This commit is contained in:
2026-08-29 16:47:17 -05:00
parent 5bbb307b12
commit 5b95ac3c7e
2 changed files with 36 additions and 4 deletions
+9 -4
View File
@@ -117,11 +117,16 @@ func New(baseURL, apiKey, apiUsername string) (*Client, error) {
}, nil
}
// NewFromEnv builds a client from DISCOURSE_URL, DISCOURSE_API_KEY and
// DISCOURSE_API_USERNAME (default "system"). The intended source is a
// 0600 env file (see env.example), sourced before the process starts.
// NewFromEnv builds a client from DISCOURSE_URL, the API key
// (DISCOURSE_API_KEY, falling back to the shorter DISCOURSE_KEY alias)
// and DISCOURSE_API_USERNAME (default "system"). The intended source is
// a 0600 env file (see env.example), sourced before the process starts.
func NewFromEnv() (*Client, error) {
return New(os.Getenv("DISCOURSE_URL"), os.Getenv("DISCOURSE_API_KEY"), os.Getenv("DISCOURSE_API_USERNAME"))
key := os.Getenv("DISCOURSE_API_KEY")
if key == "" {
key = os.Getenv("DISCOURSE_KEY")
}
return New(os.Getenv("DISCOURSE_URL"), key, os.Getenv("DISCOURSE_API_USERNAME"))
}
// String renders the client for logs: base url + acting username only.