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
|
||||
}
|
||||
38
main.go
38
main.go
|
|
@ -2,45 +2,17 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"log"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"git-repo-updater/internal/config"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Directories []string `yaml:"directories"`
|
||||
}
|
||||
|
||||
func getConfigPath() string {
|
||||
home, err := os.UserHomeDir()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return filepath.Join(home, ".config", "git-repo-updater", "config.yaml")
|
||||
}
|
||||
|
||||
func main() {
|
||||
configPath := getConfigPath()
|
||||
data, err := os.ReadFile(configPath)
|
||||
cfg, err := config.Load()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
config := parseConfig(data)
|
||||
|
||||
fmt.Println("Directories:", config.Directories)
|
||||
}
|
||||
|
||||
func parseConfig(data []byte) Config {
|
||||
var config Config
|
||||
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return config
|
||||
fmt.Println("Directories:", cfg.Directories)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue