Refactor
This commit is contained in:
parent
f3afd5762f
commit
05adcdfcd3
2 changed files with 45 additions and 33 deletions
40
internal/config/config.go
Normal file
40
internal/config/config.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Directories []string `yaml:"directories"`
|
||||
}
|
||||
|
||||
func getConfigPath() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, ".config", "git-repo-updater", "config.yaml"), nil
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
path, err := getConfigPath()
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("read config: %w", err)
|
||||
}
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return Config{}, fmt.Errorf("parse config: %w", err)
|
||||
}
|
||||
if len(cfg.Directories) == 0 {
|
||||
return Config{}, fmt.Errorf("no directories configured")
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue