2024-04-04 01:12:42 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadToml(file string) (Config, error) {
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
source, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return config, errConfigFileNotReadable
|
|
|
|
}
|
|
|
|
|
|
|
|
err = toml.Unmarshal(source, &config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-04-04 20:53:35 +02:00
|
|
|
fillFullPath(&config)
|
|
|
|
|
2024-04-04 01:12:42 +02:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2024-04-04 20:53:35 +02:00
|
|
|
func fillFullPath(config *Config) {
|
|
|
|
for i, content := range config.RepoList {
|
|
|
|
config.RepoList[i].FullPath = config.CloneDirectory + "/" + content.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 18:02:04 +02:00
|
|
|
func (config Config) Verify() error {
|
2024-04-04 01:12:42 +02:00
|
|
|
allowedValue := []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
|
|
|
|
found := false
|
|
|
|
for _, v := range allowedValue {
|
|
|
|
if v == config.Log.Level {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errLogLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
if unix.Access(config.CloneDirectory, unix.W_OK) != nil {
|
|
|
|
return errCloneDirectoryUnwritable
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: verify RepoList not redundant
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|