repolist/configfile.go

193 lines
4.7 KiB
Go
Raw Normal View History

2024-02-22 21:12:35 -06:00
package repolist
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
func (v *RepoList) InitRepoList(cfgfile string) {
lines := parsecfg(cfgfile)
for _, line := range lines {
2024-02-23 11:01:23 -06:00
var repo *RepoRow
var err error
2024-02-22 21:12:35 -06:00
line = strings.TrimSpace(line)
2024-02-23 11:01:23 -06:00
if line == "" {
// skip empty lines
continue
}
2024-02-22 21:12:35 -06:00
if strings.HasPrefix(line, "#") {
2024-02-23 11:01:23 -06:00
// skip config file comment lines
2024-02-22 21:12:35 -06:00
continue
}
parts := strings.Split(line, " ")
2024-02-23 11:01:23 -06:00
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
}
2024-02-22 21:12:35 -06:00
if len(parts) > 0 {
path := parts[0]
2024-02-23 11:01:23 -06:00
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)
2024-02-22 21:12:35 -06:00
}
}
}
func parsecfg(f string) []string {
homeDir, _ := os.UserHomeDir()
cfgfile := filepath.Join(homeDir, f)
2024-02-23 11:01:23 -06:00
content, err := ioutil.ReadFile(cfgfile)
if err != nil {
log.Log(REPOWARN, "read cfgfile error", err)
return nil
}
2024-02-22 21:12:35 -06:00
out := string(content)
out = strings.TrimSpace(out)
lines := strings.Split(out, "\n")
return lines
}
2024-02-23 07:02:31 -06:00
func (rl *RepoList) ArgCheckoutUser() bool {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "running git checkout devel everwhere")
2024-02-23 07:02:31 -06:00
var failed int = 0
for _, repo := range rl.AllRepos() {
if repo.Status.ReadOnly() {
2024-02-23 11:01:23 -06:00
// log.Log(REPOWARN,"skipping read-only", repo.Name())
2024-02-23 07:02:31 -06:00
continue
}
if repo.Status.CheckDirty() {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "skipping dirty repo", repo.Name())
2024-02-23 07:02:31 -06:00
continue
}
branch := repo.Status.GetUserBranchName()
if branch == repo.Status.GetCurrentBranchName() {
// already on user branch
continue
}
cmd := []string{"git", "checkout", branch}
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Running:", cmd, "in", repo.Name())
2024-02-23 07:02:31 -06:00
err, output := repo.RunCmd(cmd)
if err == nil {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "git checkout worked", output)
2024-02-23 07:02:31 -06:00
} else {
failed += 1
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "git checkout failed")
log.Log(REPOWARN, "Something went wrong. Got err", err)
log.Log(REPOWARN, "output =", output)
2024-02-23 07:02:31 -06:00
// return false
}
}
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed)
2024-02-23 07:02:31 -06:00
return true
}
func (rl *RepoList) ArgGitPull() bool {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "running git pull everywhere")
2024-02-23 07:02:31 -06:00
cmd := []string{"git", "pull"}
var failed int = 0
for _, repo := range rl.AllRepos() {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Running:", repo.Status.Path(), cmd)
2024-02-23 07:02:31 -06:00
err, output := repo.RunCmd(cmd)
if err == nil {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, output)
2024-02-23 07:02:31 -06:00
} else {
failed += 1
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Something went wrong. Got err", err)
log.Log(REPOWARN, "output =", output)
2024-02-23 07:02:31 -06:00
return false
}
}
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Ran git pull in all repos. failure count =", failed)
2024-02-23 07:02:31 -06:00
return true
}
func (rl *RepoList) ArgCheckoutDevel() bool {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "running git checkout devel everwhere")
2024-02-23 07:02:31 -06:00
var failed int = 0
for _, repo := range rl.AllRepos() {
if repo.Status.ReadOnly() {
2024-02-23 11:01:23 -06:00
// log.Log(REPOWARN,"skipping read-only", repo.Name())
2024-02-23 07:02:31 -06:00
continue
}
if repo.CheckDirty() {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "skipping dirty repo", repo.Name())
2024-02-23 07:02:31 -06:00
continue
}
branch := repo.Status.GetDevelBranchName()
cmd := []string{"git", "checkout", branch}
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Running:", cmd, "in", repo.Name())
2024-02-23 07:02:31 -06:00
err, output := repo.RunCmd(cmd)
if err == nil {
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "git checkout worked", output)
2024-02-23 07:02:31 -06:00
} else {
failed += 1
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "git checkout failed")
log.Log(REPOWARN, "Something went wrong. Got err", err)
log.Log(REPOWARN, "output =", output)
2024-02-23 07:02:31 -06:00
// return false
}
}
2024-02-23 11:01:23 -06:00
log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed)
2024-02-23 07:02:31 -06:00
return true
}