repolist/configfile.go

200 lines
4.5 KiB
Go

package repolist
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/log"
)
func (v *RepoList) InitRepoList(cfgfile string) {
v.cfgfile = cfgfile
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) ArgGitPull() bool {
var localonly int
var badmap int
log.Log(REPOWARN, "running git pull everywhere")
var failed int = 0
loop := rl.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
if out, err := repo.Status.GitPull(); err == nil {
log.Log(REPOWARN, "Ran git pull ok", repo.Status.Path(), out)
} else {
failed += 1
repo.Status.DumpTags()
if errors.Is(repostatus.ErrorGitPullOnLocal, err) {
localonly += 1
continue
}
badmap += 1
log.Log(REPOWARN, "bad unknown git error", repo.Status.Path(), out, err)
}
}
log.Log(REPOWARN, "Ran git pull in all repos. failure count =", failed)
log.Log(REPOWARN, "Ran git pull in all repos. bad 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
var count int = 0
loop := rl.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
count += 1
if repo.Status.CheckoutDevel() {
// checkout ok
} else {
failed += 1
}
}
log.Log(REPOWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}
func (rl *RepoList) ArgCheckoutMaster() bool {
log.Log(REPOWARN, "running git checkout master everwhere")
var failed int = 0
var count int = 0
loop := rl.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
count += 1
if repo.Status.CheckoutMaster() {
// checkout ok
} else {
failed += 1
}
}
log.Log(REPOWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}
func (rl *RepoList) ArgCheckoutUser() bool {
log.Log(REPOWARN, "running git checkout master everwhere")
var failed int = 0
var count int = 0
loop := rl.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
count += 1
if repo.Status.CheckoutUser() {
// checkout ok
} else {
failed += 1
}
}
log.Log(REPOWARN, "Ran git checkout in", count, "repos. failure count =", failed)
return true
}
func (rl *RepoList) Cfgfile() string {
return rl.cfgfile
}