📝 Clean configuration doc & code

This commit is contained in:
Ada 2024-05-22 01:45:19 +02:00
parent ecbd550f13
commit ed50f0982f
Signed by: ada
GPG key ID: 6A7F898157C6DE6E
4 changed files with 38 additions and 25 deletions

View file

@ -1,6 +1,6 @@
= Configuration
File format used is https://toml.io/en/[toml]
File format used is https://toml.io/en/[toml]. You can specify config file in first argument. (ex: `./spiegel different.toml)`). Default path is `config.toml`
== Options:
* `clonedirectory`: Directory where mirror is clone
@ -9,7 +9,7 @@ File format used is https://toml.io/en/[toml]
** `level`: Log level, allowed value is: "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
** `file`: Log file, default (empty) is in stderr
** `RepoList`: List of mirrored git repository
*** `URL`: Source URL
*** `URL`: Source URL. You can use `https://<user>:<password>@git.example/repo.git` if you want http basic auth.
*** `name`: directory name of clone
== Example:

View file

@ -8,34 +8,35 @@ import (
)
func LoadToml(file string) (Config, error) {
var config Config
var c Config
source, err := os.ReadFile(file)
if err != nil {
return config, errConfigFileNotReadable
return c, errConfigFileNotReadable
}
err = toml.Unmarshal(source, &config)
err = toml.Unmarshal(source, &c)
if err != nil {
panic(err)
}
fillFullPath(&config)
fillFullPath(&c)
fillFullPath(&c)
return config, nil
return c, nil
}
func fillFullPath(config *Config) {
for i, content := range config.RepoList {
config.RepoList[i].FullPath = config.CloneDirectory + "/" + content.Name
func fillFullPath(c *Config) {
for i, content := range c.RepoList {
c.RepoList[i].FullPath = c.CloneDirectory + "/" + content.Name
}
}
func (config Config) Verify() error {
func (c Config) Verify() error {
allowedValue := []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
found := false
for _, v := range allowedValue {
if v == config.Log.Level {
if v == c.Log.Level {
found = true
}
}
@ -44,7 +45,7 @@ func (config Config) Verify() error {
return errLogLevel
}
if unix.Access(config.CloneDirectory, unix.W_OK) != nil {
if unix.Access(c.CloneDirectory, unix.W_OK) != nil {
return errCloneDirectoryUnwritable
}

View file

@ -3,7 +3,6 @@ package git
import (
"errors"
"io"
"log"
"os"
goGit "github.com/go-git/go-git/v5"
@ -20,8 +19,8 @@ func StartClone(repoList []RepoConfig) {
}
}
func (config RepoConfig) fullClone() {
logrus.Debug("Clone ", config.Name, "...")
func (c RepoConfig) fullClone() {
logrus.Debug("Clone ", c.Name, "...")
logger := logrus.New()
w := logger.Writer()
defer func(w *io.PipeWriter) {
@ -31,25 +30,27 @@ func (config RepoConfig) fullClone() {
}
}(w)
_, err := goGit.PlainClone(config.FullPath, true, &goGit.CloneOptions{
URL: config.URL,
repoConfig := &goGit.CloneOptions{
URL: c.URL,
Progress: w,
Mirror: true,
})
}
_, err := goGit.PlainClone(c.FullPath, true, repoConfig)
if err != nil {
log.Panic(err)
logrus.Error(err)
}
}
func (config RepoConfig) Update() {
repo, err := goGit.PlainOpen(config.FullPath)
func (c RepoConfig) Update() {
repo, err := goGit.PlainOpen(c.FullPath)
if err != nil {
logrus.Error(err)
return
}
logrus.Debug("Clone ", config.Name, "...")
logrus.Debug("Clone ", c.Name, "...")
logger := logrus.New()
w := logger.Writer()
defer func(w *io.PipeWriter) {
@ -64,7 +65,7 @@ func (config RepoConfig) Update() {
})
if err != nil {
if errors.Is(err, goGit.NoErrAlreadyUpToDate) {
logrus.Info(config.Name, " is already up-to-date")
logrus.Info(c.Name, " is already up-to-date")
} else {
logrus.Error(err)
}

13
main.go
View file

@ -13,7 +13,18 @@ import (
)
func main() {
initConfig, err := config.LoadToml("config.example.toml")
var configPath string
switch len(os.Args) {
case 2: //nolint:mnd
configPath = os.Args[1]
case 1:
configPath = "config.toml"
default:
logrus.Fatal("Max 1 argument is valid.")
}
initConfig, err := config.LoadToml(configPath)
if err != nil {
logrus.Fatal(err)
}