forgepb/init.go

134 lines
3.0 KiB
Go

package forgepb
import (
"os"
"path/filepath"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
// todo: use initOnce
// cache.go has Do()
// f.initOnce.Do(f.initWork)
func Init() *Forge {
f := InitPB()
f.Machine = new(zoopb.Machine)
if err := f.Machine.ConfigLoad(); err != nil {
log.Warn("zoopb.ConfigLoad() failed", err)
}
f.Machine.InitWit()
now := time.Now()
start := f.Repos.Len()
f.ScanGoSrc()
end := f.Repos.Len()
if (end - start) == 0 {
log.Info("forgepb.Scan() Scan did not find new git repositories. Total =", end)
} else {
log.Info("forgepb.Scan() Scan found", end-start, "new git repositories. Total =", end)
}
f.updateAll()
if f.configSave {
f.ConfigSave()
f.configSave = false
}
log.Info("update() check took", shell.FormatDuration(time.Since(now)))
return f
}
func (f *Forge) updateAll() {
all := f.Repos.SortByFullPath()
for all.Scan() {
repo := all.Next()
if !repo.IsValidDir() {
log.Printf("%10s %-50s", "bad git dir\n", repo.FullPath)
f.Repos.DeleteByFullPath(repo.FullPath)
f.configSave = true
continue
}
if repo.RepoChanged() {
f.configSave = true
log.Info("repo changed", repo.StateChange, repo.FullPath)
repo.Reload()
}
if f.Config.IsReadOnly(repo.GetGoPath()) {
if repo.ReadOnly {
} else {
log.Info("readonly flag on repo is wrong", repo.GetGoPath())
repo.ReadOnly = true
f.configSave = true
}
}
}
}
// only init's the protobuf. intended to not scan or change anything
func InitPB() *Forge {
f := new(Forge)
// TODO: rethink this but it works for now
gosrc := os.Getenv("FORGE_GOSRC")
if gosrc == "" {
goSrcDir, err := f.findGoSrc()
if err != nil {
log.Warn("forge init() findGoSrc()", err)
}
os.Setenv("FORGE_GOSRC", goSrcDir)
}
f.goSrc = os.Getenv("FORGE_GOSRC")
// also rethink this, but maybe this is the right thing to do
if os.Getenv("FORGE_CONFIG") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_CONFIG", fullpath)
}
// check again for go.work // user could have a go.work file in ~/go/src
if f.goWorkExists() {
f.goWork = true
}
// print out the settings that will be used
log.Info("forgepb.Init() FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
// load the ~/.config/forge/ config
f.Config = new(ForgeConfigs)
if err := f.Config.ConfigLoad(); err != nil {
log.Warn("forgepb.ConfigLoad() failed", err)
}
if f.IsGoWork() {
log.Info("forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = true)")
} else {
log.Info("forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = false)")
}
f.Repos = new(gitpb.Repos)
f.Repos.ConfigLoad()
return f
}
func (f *Forge) SetConfigSave(b bool) {
f.configSave = b
}
// saves the config if there have been changes
func (f *Forge) Exit() {
log.Info("forge.configSave =", f.configSave)
if f.configSave {
f.ConfigSave()
}
log.Info("forge.Exit() ok")
os.Exit(0)
}