From d331c64fa65fb4ea4f4cd6d16134cd4daa8c1f11 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 08:54:24 +0000 Subject: [PATCH] refactor(forge): expose config path helper Co-Authored-By: Virgil --- config.go | 18 +++++++++++++----- config_test.go | 14 ++++++++++++++ forge.go | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 00e24d6..6da3f02 100644 --- a/config.go +++ b/config.go @@ -26,16 +26,23 @@ type configFile struct { Token string `json:"token"` } -func configPath() (string, error) { +// ConfigPath returns the default config file path used by SaveConfig and +// ResolveConfig. +// +// Usage: +// +// path, err := forge.ConfigPath() +// _ = path +func ConfigPath() (string, error) { home, err := os.UserHomeDir() if err != nil { - return "", core.E("configPath", "forge: resolve home directory", err) + return "", core.E("ConfigPath", "forge: resolve home directory", err) } return filepath.Join(home, defaultConfigPath), nil } func readConfigFile() (url, token string, err error) { - path, err := configPath() + path, err := ConfigPath() if err != nil { return "", "", err } @@ -62,7 +69,7 @@ func readConfigFile() (url, token string, err error) { // // _ = forge.SaveConfig("https://forge.example.com", "token") func SaveConfig(url, token string) error { - path, err := configPath() + path, err := ConfigPath() if err != nil { return err } @@ -132,7 +139,8 @@ func NewFromConfig(flagURL, flagToken string, opts ...Option) (*Forge, error) { } // NewForgeFromConfig creates a new Forge client using resolved configuration. -// It returns an error if no API token is available from flags or environment. +// It returns an error if no API token is available from flags, environment, +// or the saved config file. // // Usage: // diff --git a/config_test.go b/config_test.go index 1263e42..5d35f47 100644 --- a/config_test.go +++ b/config_test.go @@ -189,3 +189,17 @@ func TestSaveConfig_Good(t *testing.T) { t.Errorf("got token=%q", cfg["token"]) } } + +func TestConfigPath_Good(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + + got, err := ConfigPath() + if err != nil { + t.Fatal(err) + } + want := filepath.Join(home, ".config", "forge", "config.json") + if got != want { + t.Fatalf("got path=%q, want %q", got, want) + } +} diff --git a/forge.go b/forge.go index 0f7eaf8..22e08fe 100644 --- a/forge.go +++ b/forge.go @@ -67,7 +67,7 @@ func NewForge(url, token string, opts ...Option) *Forge { return f } -// Client returns the underlying HTTP client. +// Client returns the underlying Forge client. // // Usage: //