From 6b7ab8a752e97f855d2f4201959162356e3ba388 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 23 Feb 2024 11:01:23 -0600 Subject: [PATCH] parse config file options --- configfile.go | 126 +++++++++++++++++++++++++++++++++++++++----------- newRepo.go | 6 +++ 2 files changed, 105 insertions(+), 27 deletions(-) diff --git a/configfile.go b/configfile.go index 931f232..771c163 100644 --- a/configfile.go +++ b/configfile.go @@ -12,15 +12,83 @@ import ( func (v *RepoList) InitRepoList(cfgfile string) { lines := parsecfg(cfgfile) for _, line := range lines { - log.Verbose("repo =", line) + var repo *RepoRow + var err error line = strings.TrimSpace(line) + if line == "" { + // skip empty lines + continue + } if strings.HasPrefix(line, "#") { + // skip config file comment lines continue } parts := strings.Split(line, " ") + var private bool = false + var readonly bool = false + var userdir bool = false + if len(parts) > 1 { + repodir := parts[0] + for _, s := range parts[1:] { + option := strings.Split(s, "=") + if len(option) != 2 { + log.Log(REPOWARN, "can not parse cfgfile line:", line) + continue + } + switch option[0] { + case "private": + if option[1] == "true" { + log.Log(REPOWARN, repodir, "setting private to true") + private = true + } else { + log.Log(REPOWARN, repodir, "setting private to false") + private = false + } + case "readonly": + if option[1] == "true" { + log.Log(REPOWARN, repodir, "setting ReadOnly to true") + private = true + } else { + log.Log(REPOWARN, repodir, "setting ReadOnly to false") + private = false + } + case "userdir": + if option[1] == "true" { + log.Log(REPOWARN, repodir, "setting this directory as Writable()") + userdir = true + } else { + userdir = false + } + default: + log.Log(REPOWARN, "unknown config file option", s) + } + } + } + if userdir { + log.Log(REPOWARN, "add this as a writable dir", line) + continue + } if len(parts) > 0 { path := parts[0] - v.NewRepo(path) + repo, err = v.NewRepo(path) + } + if err != nil { + log.Log(REPOWARN, line, "repo could not be added", err) + continue + } + if repo == nil { + // there is not a new repo + continue + } + if private { + repo.Status.SetPrivate(true) + } else { + repo.Status.SetPrivate(false) + } + if readonly { + repo.Status.SetReadOnly(true) + } else { + repo.Status.SetReadOnly(false) } } } @@ -28,7 +96,11 @@ func (v *RepoList) InitRepoList(cfgfile string) { func parsecfg(f string) []string { homeDir, _ := os.UserHomeDir() cfgfile := filepath.Join(homeDir, f) - content, _ := ioutil.ReadFile(cfgfile) + content, err := ioutil.ReadFile(cfgfile) + if err != nil { + log.Log(REPOWARN, "read cfgfile error", err) + return nil + } out := string(content) out = strings.TrimSpace(out) lines := strings.Split(out, "\n") @@ -36,15 +108,15 @@ func parsecfg(f string) []string { } func (rl *RepoList) ArgCheckoutUser() bool { - log.Info("running git checkout devel everwhere") + log.Log(REPOWARN, "running git checkout devel everwhere") var failed int = 0 for _, repo := range rl.AllRepos() { if repo.Status.ReadOnly() { - // log.Info("skipping read-only", repo.Name()) + // log.Log(REPOWARN,"skipping read-only", repo.Name()) continue } if repo.Status.CheckDirty() { - log.Info("skipping dirty repo", repo.Name()) + log.Log(REPOWARN, "skipping dirty repo", repo.Name()) continue } branch := repo.Status.GetUserBranchName() @@ -53,68 +125,68 @@ func (rl *RepoList) ArgCheckoutUser() bool { continue } cmd := []string{"git", "checkout", branch} - log.Info("Running:", cmd, "in", repo.Name()) + log.Log(REPOWARN, "Running:", cmd, "in", repo.Name()) err, output := repo.RunCmd(cmd) if err == nil { - log.Info("git checkout worked", output) + log.Log(REPOWARN, "git checkout worked", output) } else { failed += 1 - log.Info("git checkout failed") - log.Info("Something went wrong. Got err", err) - log.Info("output =", output) + log.Log(REPOWARN, "git checkout failed") + log.Log(REPOWARN, "Something went wrong. Got err", err) + log.Log(REPOWARN, "output =", output) // return false } } - log.Info("Ran git checkout in all repos. failure count =", failed) + log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed) return true } func (rl *RepoList) ArgGitPull() bool { - log.Info("running git pull everywhere") + log.Log(REPOWARN, "running git pull everywhere") cmd := []string{"git", "pull"} var failed int = 0 for _, repo := range rl.AllRepos() { - log.Info("Running:", repo.Status.Path(), cmd) + log.Log(REPOWARN, "Running:", repo.Status.Path(), cmd) err, output := repo.RunCmd(cmd) if err == nil { - log.Info(output) + log.Log(REPOWARN, output) } else { failed += 1 - log.Info("Something went wrong. Got err", err) - log.Info("output =", output) + log.Log(REPOWARN, "Something went wrong. Got err", err) + log.Log(REPOWARN, "output =", output) return false } } - log.Info("Ran git pull in all repos. failure count =", failed) + log.Log(REPOWARN, "Ran git pull in all repos. failure count =", failed) return true } func (rl *RepoList) ArgCheckoutDevel() bool { - log.Info("running git checkout devel everwhere") + log.Log(REPOWARN, "running git checkout devel everwhere") var failed int = 0 for _, repo := range rl.AllRepos() { if repo.Status.ReadOnly() { - // log.Info("skipping read-only", repo.Name()) + // log.Log(REPOWARN,"skipping read-only", repo.Name()) continue } if repo.CheckDirty() { - log.Info("skipping dirty repo", repo.Name()) + log.Log(REPOWARN, "skipping dirty repo", repo.Name()) continue } branch := repo.Status.GetDevelBranchName() cmd := []string{"git", "checkout", branch} - log.Info("Running:", cmd, "in", repo.Name()) + log.Log(REPOWARN, "Running:", cmd, "in", repo.Name()) err, output := repo.RunCmd(cmd) if err == nil { - log.Info("git checkout worked", output) + log.Log(REPOWARN, "git checkout worked", output) } else { failed += 1 - log.Info("git checkout failed") - log.Info("Something went wrong. Got err", err) - log.Info("output =", output) + log.Log(REPOWARN, "git checkout failed") + log.Log(REPOWARN, "Something went wrong. Got err", err) + log.Log(REPOWARN, "output =", output) // return false } } - log.Info("Ran git checkout in all repos. failure count =", failed) + log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed) return true } diff --git a/newRepo.go b/newRepo.go index 79796ad..828ba60 100644 --- a/newRepo.go +++ b/newRepo.go @@ -50,6 +50,12 @@ func (r *RepoRow) Show() { } func (r *RepoList) NewRepo(path string) (*RepoRow, error) { + test, ok := r.allrepos[path] + if ok { + // this repo already exists + return test, nil + } + status, err := repostatus.New(path) if err != nil { return nil, err