feat: store config file path in struct
This commit is contained in:
parent
155672abe2
commit
4b0ce1df5f
1 changed files with 5 additions and 5 deletions
|
|
@ -9,6 +9,8 @@ import (
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ForgejoInstanceURL string `json:"forgejo_instance_url"`
|
ForgejoInstanceURL string `json:"forgejo_instance_url"`
|
||||||
|
|
||||||
|
filepath string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load tries to load a saved configuration.
|
// 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) {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
return &Config{}, nil
|
return &Config{filepath: path}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fileData, err := os.ReadFile(path)
|
fileData, err := os.ReadFile(path)
|
||||||
|
|
@ -35,6 +37,7 @@ func Load(path string) (*Config, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
|
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
|
||||||
}
|
}
|
||||||
|
cfg.filepath = path
|
||||||
|
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
@ -42,10 +45,7 @@ func Load(path string) (*Config, error) {
|
||||||
func (cfg *Config) Save(path string) error {
|
func (cfg *Config) Save(path string) error {
|
||||||
var err error
|
var err error
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path, err = getUserConfigDir()
|
path = cfg.filepath
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to save config: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileData, err := json.MarshalIndent(cfg, "", " ")
|
fileData, err := json.MarshalIndent(cfg, "", " ")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue