spiegel/internal/config/toml.go

54 lines
857 B
Go
Raw Normal View History

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) {
2024-05-22 01:45:19 +02:00
var c Config
2024-04-04 01:12:42 +02:00
source, err := os.ReadFile(file)
if err != nil {
2024-05-22 01:45:19 +02:00
return c, errConfigFileNotReadable
2024-04-04 01:12:42 +02:00
}
2024-05-22 01:45:19 +02:00
err = toml.Unmarshal(source, &c)
2024-04-04 01:12:42 +02:00
if err != nil {
panic(err)
}
2024-05-22 01:45:19 +02:00
fillFullPath(&c)
fillFullPath(&c)
2024-05-22 01:45:19 +02:00
return c, nil
2024-04-04 01:12:42 +02:00
}
2024-05-22 01:45:19 +02:00
func fillFullPath(c *Config) {
for i, content := range c.RepoList {
c.RepoList[i].FullPath = c.CloneDirectory + "/" + content.Name
}
}
2024-05-22 01:45:19 +02:00
func (c Config) Verify() error {
2024-04-04 01:12:42 +02:00
allowedValue := []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
found := false
for _, v := range allowedValue {
2024-05-22 01:45:19 +02:00
if v == c.Log.Level {
2024-04-04 01:12:42 +02:00
found = true
}
}
if !found {
return errLogLevel
}
2024-05-22 01:45:19 +02:00
if unix.Access(c.CloneDirectory, unix.W_OK) != nil {
2024-04-04 01:12:42 +02:00
return errCloneDirectoryUnwritable
}
return nil
}