📝 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 = 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: == Options:
* `clonedirectory`: Directory where mirror is clone * `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" ** `level`: Log level, allowed value is: "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
** `file`: Log file, default (empty) is in stderr ** `file`: Log file, default (empty) is in stderr
** `RepoList`: List of mirrored git repository ** `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 *** `name`: directory name of clone
== Example: == Example:

View file

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

View file

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

13
main.go
View file

@ -13,7 +13,18 @@ import (
) )
func main() { 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 { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }