repolist/configfile.go

211 lines
5.3 KiB
Go

package repolist
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
func (v *RepoList) InitRepoList(cfgfile string) {
lines := parsecfg(cfgfile)
for _, line := range lines {
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")
readonly = true
} else {
log.Log(REPOWARN, repodir, "setting ReadOnly to false")
readonly = 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]
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)
}
}
}
func parsecfg(f string) []string {
homeDir, _ := os.UserHomeDir()
cfgfile := filepath.Join(homeDir, f)
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")
return lines
}
func (rl *RepoList) ArgCheckoutUser() bool {
log.Log(REPOWARN, "running git checkout devel everwhere")
var failed int = 0
for _, repo := range rl.AllRepos() {
if repo.Status.ReadOnly() {
// log.Log(REPOWARN,"skipping read-only", repo.Name())
continue
}
if repo.Status.CheckDirty() {
log.Log(REPOWARN, "skipping dirty repo", repo.Name())
continue
}
branch := repo.Status.GetUserBranchName()
if branch == repo.Status.GetCurrentBranchName() {
// already on user branch
continue
}
cmd := []string{"git", "checkout", branch}
log.Log(REPOWARN, "Running:", cmd, "in", repo.Name())
err, output := repo.RunCmd(cmd)
if err == nil {
log.Log(REPOWARN, "git checkout worked", output)
} else {
failed += 1
log.Log(REPOWARN, "git checkout failed")
log.Log(REPOWARN, "Something went wrong. Got err", err)
log.Log(REPOWARN, "output =", output)
// return false
}
}
log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed)
return true
}
func (rl *RepoList) ArgGitPull() bool {
var localonly int
var badmap int
log.Log(REPOWARN, "running git pull everywhere")
var failed int = 0
for _, repo := range rl.AllRepos() {
log.Log(REPOWARN, "Running:", repo.Status.Path())
if err := repo.Status.GitPull(); err == nil {
} else {
failed += 1
repo.Status.DumpTags()
name := repo.Status.GetCurrentBranchName()
if repo.Status.IsOnlyLocalTag(name) {
log.Log(REPOWARN, repo.Status.Path(), "can not git pull on local only branch", name, err)
} else {
log.Log(REPOWARN, repo.Status.Path(), "this branch should have worked", name, err)
}
if fmt.Sprint(err) == "git config error" {
badmap += 1
continue
}
if fmt.Sprint(err) == "local only" {
localonly += 1
continue
}
return false
}
}
log.Log(REPOWARN, "Ran git pull in all repos. failure count =", failed)
log.Log(REPOWARN, "Ran git pull in all repos. git config map errors =", badmap)
if localonly != 0 {
log.Log(REPOWARN, "Ran git pull in all repos. ignored local only branches =", localonly)
}
return true
}
func (rl *RepoList) ArgCheckoutDevel() bool {
log.Log(REPOWARN, "running git checkout devel everwhere")
var failed int = 0
for _, repo := range rl.AllRepos() {
if repo.Status.ReadOnly() {
// log.Log(REPOWARN,"skipping read-only", repo.Name())
continue
}
if repo.CheckDirty() {
log.Log(REPOWARN, "skipping dirty repo", repo.Name())
continue
}
branch := repo.Status.GetDevelBranchName()
cmd := []string{"git", "checkout", branch}
log.Log(REPOWARN, "Running:", cmd, "in", repo.Name())
err, output := repo.RunCmd(cmd)
if err == nil {
log.Log(REPOWARN, "git checkout worked", output)
} else {
failed += 1
log.Log(REPOWARN, "git checkout failed")
log.Log(REPOWARN, "Something went wrong. Got err", err)
log.Log(REPOWARN, "output =", output)
// return false
}
}
log.Log(REPOWARN, "Ran git checkout in all repos. failure count =", failed)
return true
}