-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add YAML config file support via koanf #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1c7863a
ece2071
206fc2d
5ee2a17
92d0e38
440e938
b959cf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/knadh/koanf/parsers/yaml" | ||
| "github.com/knadh/koanf/providers/env" | ||
| "github.com/knadh/koanf/providers/file" | ||
| "github.com/knadh/koanf/v2" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| // CLIConfig holds CLI configuration loaded from cli.yaml | ||
| type CLIConfig struct { | ||
| BaseURL string `koanf:"base_url"` | ||
| APIKey string `koanf:"api_key"` | ||
| } | ||
|
|
||
| // getCLIConfigPath returns the path to the CLI config file. | ||
| // The CLI uses ~/.config/hypeman/cli.yaml on all platforms. | ||
| func getCLIConfigPath() string { | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return filepath.Join(home, ".config", "hypeman", "cli.yaml") | ||
| } | ||
|
|
||
| // loadCLIConfig loads CLI configuration from the config file, then | ||
| // overlays HYPEMAN_-prefixed environment variables (highest precedence). | ||
| // HYPEMAN_BASE_URL -> base_url, HYPEMAN_API_KEY -> api_key. | ||
| // Returns an empty config if the file doesn't exist or can't be parsed. | ||
| func loadCLIConfig() *CLIConfig { | ||
| cfg := &CLIConfig{} | ||
| k := koanf.New(".") | ||
|
|
||
| configPath := getCLIConfigPath() | ||
| if configPath != "" { | ||
| _ = k.Load(file.Provider(configPath), yaml.Parser()) | ||
| } | ||
|
|
||
| // Overlay HYPEMAN_-prefixed env vars: HYPEMAN_BASE_URL -> base_url | ||
| _ = k.Load(env.ProviderWithValue("HYPEMAN_", ".", func(key string, value string) (string, interface{}) { | ||
| if value == "" { | ||
| return "", nil | ||
| } | ||
| return strings.ToLower(strings.TrimPrefix(key, "HYPEMAN_")), value | ||
| }), nil) | ||
|
|
||
| _ = k.Unmarshal("", cfg) | ||
| return cfg | ||
| } | ||
|
|
||
| // resolveBaseURL returns the effective base URL with precedence: | ||
| // CLI flag > HYPEMAN_BASE_URL env > config file > default. | ||
| func resolveBaseURL(cmd *cli.Command) string { | ||
| if u := cmd.Root().String("base-url"); u != "" { | ||
| return u | ||
| } | ||
| cfg := loadCLIConfig() | ||
| if cfg.BaseURL != "" { | ||
| return cfg.BaseURL | ||
| } | ||
| return "http://localhost:8080" | ||
| } | ||
|
|
||
| // resolveAPIKey returns the effective API key with precedence: | ||
| // HYPEMAN_API_KEY env > config file. | ||
| func resolveAPIKey() string { | ||
| cfg := loadCLIConfig() | ||
| return cfg.APIKey | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,13 +35,7 @@ func handlePush(ctx context.Context, cmd *cli.Command) error { | |
| targetName = args[1] | ||
| } | ||
|
|
||
| baseURL := cmd.String("base-url") | ||
| if baseURL == "" { | ||
| baseURL = os.Getenv("HYPEMAN_BASE_URL") | ||
| } | ||
| if baseURL == "" { | ||
| baseURL = "http://localhost:8080" | ||
| } | ||
| baseURL := resolveBaseURL(cmd) | ||
|
|
||
| parsedURL, err := url.Parse(baseURL) | ||
| if err != nil { | ||
|
|
@@ -71,10 +65,7 @@ func handlePush(ctx context.Context, cmd *cli.Command) error { | |
| return fmt.Errorf("invalid target: %w", err) | ||
| } | ||
|
|
||
| token := os.Getenv("HYPEMAN_BEARER_TOKEN") | ||
| if token == "" { | ||
| token = os.Getenv("HYPEMAN_API_KEY") | ||
| } | ||
| token := resolveAPIKey() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Push drops legacy token env fallbackMedium Severity
|
||
|
|
||
| // Use custom transport that always sends Basic auth header | ||
| transport := &authTransport{ | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.