refactor(forge): expose config path helper
Some checks failed
Test / test (push) Waiting to run
Security Scan / security (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 08:54:24 +00:00
parent 61d2b8440a
commit d331c64fa6
3 changed files with 28 additions and 6 deletions

View file

@ -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:
//

View file

@ -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)
}
}

View file

@ -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:
//