Skip to content

Commit f5402f6

Browse files
committed
refactor: use koanf env provider for HYPEMAN_ env vars
Load HYPEMAN_BASE_URL and HYPEMAN_API_KEY via koanf's env provider in loadCLIConfig() instead of manual os.Getenv checks. This gives consistent precedence (env > config file) in one place. HYPEMAN_BEARER_TOKEN remains as a legacy fallback checked separately.
1 parent 23fb52f commit f5402f6

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
5353
github.com/klauspost/compress v1.18.1 // indirect
5454
github.com/knadh/koanf/maps v0.1.2 // indirect
55+
github.com/knadh/koanf/providers/env v1.1.0 // indirect
5556
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
5657
github.com/mattn/go-isatty v0.0.20 // indirect
5758
github.com/mattn/go-localereader v0.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ github.com/knadh/koanf/maps v0.1.2 h1:RBfmAW5CnZT+PJ1CVc1QSJKf4Xu9kxfQgYVQSu8hpb
8484
github.com/knadh/koanf/maps v0.1.2/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
8585
github.com/knadh/koanf/parsers/yaml v1.1.0 h1:3ltfm9ljprAHt4jxgeYLlFPmUaunuCgu1yILuTXRdM4=
8686
github.com/knadh/koanf/parsers/yaml v1.1.0/go.mod h1:HHmcHXUrp9cOPcuC+2wrr44GTUB0EC+PyfN3HZD9tFg=
87+
github.com/knadh/koanf/providers/env v1.1.0 h1:U2VXPY0f+CsNDkvdsG8GcsnK4ah85WwWyJgef9oQMSc=
88+
github.com/knadh/koanf/providers/env v1.1.0/go.mod h1:QhHHHZ87h9JxJAn2czdEl6pdkNnDh/JS1Vtsyt65hTY=
8789
github.com/knadh/koanf/providers/file v1.2.1 h1:bEWbtQwYrA+W2DtdBrQWyXqJaJSG3KrP3AESOJYp9wM=
8890
github.com/knadh/koanf/providers/file v1.2.1/go.mod h1:bp1PM5f83Q+TOUu10J/0ApLBd9uIzg+n9UgthfY+nRA=
8991
github.com/knadh/koanf/v2 v2.3.2 h1:Ee6tuzQYFwcZXQpc2MiVeC6qHMandf5SMUJJNoFp/c4=

pkg/cmd/config.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package cmd
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67

78
"github.com/knadh/koanf/parsers/yaml"
9+
"github.com/knadh/koanf/providers/env"
810
"github.com/knadh/koanf/providers/file"
911
"github.com/knadh/koanf/v2"
1012
"github.com/urfave/cli/v3"
@@ -26,35 +28,37 @@ func getCLIConfigPath() string {
2628
return filepath.Join(home, ".config", "hypeman", "cli.yaml")
2729
}
2830

29-
// loadCLIConfig loads CLI configuration from the config file.
31+
// loadCLIConfig loads CLI configuration from the config file, then
32+
// overlays HYPEMAN_-prefixed environment variables (highest precedence).
33+
// HYPEMAN_BASE_URL -> base_url, HYPEMAN_API_KEY -> api_key.
3034
// Returns an empty config if the file doesn't exist or can't be parsed.
3135
func loadCLIConfig() *CLIConfig {
3236
cfg := &CLIConfig{}
37+
k := koanf.New(".")
3338

3439
configPath := getCLIConfigPath()
35-
if configPath == "" {
36-
return cfg
40+
if configPath != "" {
41+
_ = k.Load(file.Provider(configPath), yaml.Parser())
3742
}
3843

39-
k := koanf.New(".")
40-
if err := k.Load(file.Provider(configPath), yaml.Parser()); err != nil {
41-
// File doesn't exist or can't be parsed - return empty config
42-
return cfg
43-
}
44+
// Overlay HYPEMAN_-prefixed env vars: HYPEMAN_BASE_URL -> base_url
45+
_ = k.Load(env.ProviderWithValue("HYPEMAN_", ".", func(key string, value string) (string, interface{}) {
46+
if value == "" {
47+
return "", nil
48+
}
49+
return strings.ToLower(strings.TrimPrefix(key, "HYPEMAN_")), value
50+
}), nil)
4451

4552
_ = k.Unmarshal("", cfg)
4653
return cfg
4754
}
4855

4956
// resolveBaseURL returns the effective base URL with precedence:
50-
// CLI flag > env var > config file > default.
57+
// CLI flag > HYPEMAN_BASE_URL env > config file > default.
5158
func resolveBaseURL(cmd *cli.Command) string {
5259
if u := cmd.Root().String("base-url"); u != "" {
5360
return u
5461
}
55-
if u := os.Getenv("HYPEMAN_BASE_URL"); u != "" {
56-
return u
57-
}
5862
cfg := loadCLIConfig()
5963
if cfg.BaseURL != "" {
6064
return cfg.BaseURL
@@ -63,14 +67,14 @@ func resolveBaseURL(cmd *cli.Command) string {
6367
}
6468

6569
// resolveAPIKey returns the effective API key with precedence:
66-
// HYPEMAN_API_KEY env var > HYPEMAN_BEARER_TOKEN env var (legacy) > config file.
70+
// HYPEMAN_API_KEY env > config file > HYPEMAN_BEARER_TOKEN env (legacy).
6771
func resolveAPIKey() string {
68-
if k := os.Getenv("HYPEMAN_API_KEY"); k != "" {
69-
return k
72+
cfg := loadCLIConfig()
73+
if cfg.APIKey != "" {
74+
return cfg.APIKey
7075
}
7176
if k := os.Getenv("HYPEMAN_BEARER_TOKEN"); k != "" {
7277
return k
7378
}
74-
cfg := loadCLIConfig()
75-
return cfg.APIKey
79+
return ""
7680
}

0 commit comments

Comments
 (0)