feat: store config file path in struct

This commit is contained in:
juancwu 2026-01-28 16:23:42 +00:00
commit 4b0ce1df5f

View file

@ -9,6 +9,8 @@ import (
type Config struct {
ForgejoInstanceURL string `json:"forgejo_instance_url"`
filepath string `json:"-"`
}
// Load tries to load a saved configuration.
@ -22,7 +24,7 @@ func Load(path string) (*Config, error) {
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return &Config{}, nil
return &Config{filepath: path}, nil
}
fileData, err := os.ReadFile(path)
@ -35,6 +37,7 @@ func Load(path string) (*Config, error) {
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
}
cfg.filepath = path
return &cfg, nil
}
@ -42,10 +45,7 @@ func Load(path string) (*Config, error) {
func (cfg *Config) Save(path string) error {
var err error
if path == "" {
path, err = getUserConfigDir()
if err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
path = cfg.filepath
}
fileData, err := json.MarshalIndent(cfg, "", " ")