-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
98 lines (91 loc) · 2.25 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// Config holds configuration values.
type Config struct {
Branch string `yaml:"branch"`
Update bool `yaml:"update"`
Include []string `yaml:"include"`
Exclude []string `yaml:"exclude"`
Force bool `yaml:"force"`
RemoteName string `yaml:"remote_name"`
}
// loadConfig reads the configuration file.
func loadConfig(configPath string) (Config, error) {
var (
config Config
err error
)
// Read configuration file
file, err := os.ReadFile(configPath)
if err != nil {
return config, err
}
// Handle empty config file.
if len(file) == 0 {
return config, nil
}
// Deserialize data into struct.
err = yaml.Unmarshal(file, &config)
if err != nil {
return config, err
}
// Validate configuration keys.
validKeys := map[string]bool{
"branch": true,
"update": true,
"include": true,
"exclude": true,
"force": true,
"remote_name": true,
}
// Deserialize data into convenient map for key checking.
var rawConfig map[string]any
if err = yaml.Unmarshal(file, &rawConfig); err != nil {
return config, err
}
// Validate.
if err = validateKeys(rawConfig, validKeys); err != nil {
return config, err
}
return config, nil
}
// validateKeys validates the yaml config keys.
func validateKeys(config map[string]any, validKeys map[string]bool) error {
for key := range config {
if !validKeys[key] {
return fmt.Errorf("%sError unsupported key in config file: %s%s", LightRed, key, Reset)
}
}
return nil
}
// findConfigFile looks for the .rprc file in the current and parent directories.
func findConfigFile(currentDir string) (string, error) {
configPath := filepath.Join(currentDir, ".rprc")
if _, err := os.Stat(configPath); err == nil {
return configPath, nil
}
parentDir := filepath.Dir(currentDir)
if parentDir != currentDir {
return findConfigFile(parentDir)
}
return "", nil
}
// isIncluded checks if a repository is included based on the include and exclude lists.
func isIncluded(repoName string, include, exclude []string) bool {
for _, inc := range include {
if inc == repoName {
return true
}
}
for _, exc := range exclude {
if exc == repoName {
return false
}
}
return len(include) == 0
}