From ed50f0982f54bb59f4021f317a3b24da3d0cebea Mon Sep 17 00:00:00 2001 From: Ada Date: Wed, 22 May 2024 01:45:19 +0200 Subject: [PATCH] :memo: Clean configuration doc & code --- docs/config/README.adoc | 4 ++-- internal/config/toml.go | 23 ++++++++++++----------- internal/git/clone.go | 23 ++++++++++++----------- main.go | 13 ++++++++++++- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/docs/config/README.adoc b/docs/config/README.adoc index 1ea145f..aaa4804 100644 --- a/docs/config/README.adoc +++ b/docs/config/README.adoc @@ -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://:@git.example/repo.git` if you want http basic auth. *** `name`: directory name of clone == Example: diff --git a/internal/config/toml.go b/internal/config/toml.go index 2f6271e..649acc1 100644 --- a/internal/config/toml.go +++ b/internal/config/toml.go @@ -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 } diff --git a/internal/git/clone.go b/internal/git/clone.go index fd8a567..56a9389 100644 --- a/internal/git/clone.go +++ b/internal/git/clone.go @@ -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) } diff --git a/main.go b/main.go index 1e7ada0..f30ee16 100644 --- a/main.go +++ b/main.go @@ -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) }