refactor: enhance NewClient to support environment variable fallback and add tests

This commit is contained in:
2026-05-10 11:02:34 +02:00
parent 9b040dfc7b
commit f2a15c69aa
4 changed files with 148 additions and 29 deletions
+10 -21
View File
@@ -8,6 +8,7 @@ import (
miab "gitea.koszewscy.waw.pl/koszewscy/mailinabox-go"
)
func usage() {
fmt.Fprintf(os.Stderr, `Usage: miab <command> [options]
@@ -17,33 +18,21 @@ Commands:
delete Delete a DNS record
add Add a DNS record
Credentials are read from environment variables:
MIAB_HOST Mail-in-a-Box hostname (e.g. box.example.com)
MIAB_USERNAME Admin email address
MIAB_PASSWORD Admin password
Credentials: any empty parameter falls back to environment variables.
MIAB_HOST / MAILINABOX_BASE_URL Mail-in-a-Box hostname (MIAB_HOST takes precedence;
hostname is parsed from MAILINABOX_BASE_URL if set)
MIAB_USERNAME / MAILINABOX_EMAIL Admin email address
MIAB_PASSWORD / MAILINABOX_PASSWORD Admin password
`)
}
func mustClient() *miab.Client {
host := os.Getenv("MIAB_HOST")
username := os.Getenv("MIAB_USERNAME")
password := os.Getenv("MIAB_PASSWORD")
var missing []string
if host == "" {
missing = append(missing, "MIAB_HOST")
}
if username == "" {
missing = append(missing, "MIAB_USERNAME")
}
if password == "" {
missing = append(missing, "MIAB_PASSWORD")
}
if len(missing) > 0 {
fmt.Fprintf(os.Stderr, "miab: missing required environment variables: %v\n", missing)
c, err := miab.NewClient("", "", "")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return miab.NewClient(host, username, password)
return c
}
func main() {