2024-11-28 00:02:27 -06:00
|
|
|
package forgepb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-12-05 12:37:07 -06:00
|
|
|
"path/filepath"
|
2024-12-17 00:21:20 -06:00
|
|
|
"time"
|
2024-11-28 00:02:27 -06:00
|
|
|
|
2024-12-17 00:21:20 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-11-28 08:35:39 -06:00
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
2024-11-28 19:56:46 -06:00
|
|
|
"go.wit.com/lib/protobuf/zoopb"
|
2024-11-28 00:02:27 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2024-12-13 02:22:13 -06:00
|
|
|
// todo: use initOnce
|
|
|
|
// cache.go has Do()
|
|
|
|
// f.initOnce.Do(f.initWork)
|
2024-12-01 22:23:02 -06:00
|
|
|
func Init() *Forge {
|
2024-12-14 11:28:15 -06:00
|
|
|
f := InitPB()
|
|
|
|
|
2024-12-17 00:21:20 -06:00
|
|
|
f.Machine = new(zoopb.Machine)
|
|
|
|
|
|
|
|
if err := f.Machine.ConfigLoad(); err != nil {
|
|
|
|
log.Warn("zoopb.ConfigLoad() failed", err)
|
|
|
|
}
|
|
|
|
f.Machine.InitWit()
|
|
|
|
|
|
|
|
now := time.Now()
|
2024-12-14 11:28:15 -06:00
|
|
|
start := f.Repos.Len()
|
|
|
|
f.ScanGoSrc()
|
|
|
|
end := f.Repos.Len()
|
|
|
|
if (end - start) == 0 {
|
2024-12-17 06:37:00 -06:00
|
|
|
log.Info("forgepb.Scan() Scan did not find new git repositories. Total =", end)
|
2024-12-14 11:28:15 -06:00
|
|
|
} else {
|
2024-12-17 06:37:00 -06:00
|
|
|
log.Info("forgepb.Scan() Scan found", end-start, "new git repositories. Total =", end)
|
2024-12-14 11:28:15 -06:00
|
|
|
}
|
|
|
|
|
2024-12-17 20:48:08 -06:00
|
|
|
f.rillUpdate(20, 10)
|
2024-12-17 01:15:17 -06:00
|
|
|
|
|
|
|
if f.configSave {
|
2024-12-17 00:21:20 -06:00
|
|
|
f.ConfigSave()
|
2024-12-17 01:15:17 -06:00
|
|
|
f.configSave = false
|
2024-12-14 11:28:15 -06:00
|
|
|
}
|
2024-12-17 00:21:20 -06:00
|
|
|
log.Info("update() check took", shell.FormatDuration(time.Since(now)))
|
2024-12-14 11:28:15 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// only init's the protobuf. intended to not scan or change anything
|
|
|
|
func InitPB() *Forge {
|
2024-12-01 22:23:02 -06:00
|
|
|
f := new(Forge)
|
|
|
|
|
|
|
|
// TODO: rethink this but it works for now
|
2024-11-28 00:02:27 -06:00
|
|
|
gosrc := os.Getenv("FORGE_GOSRC")
|
2024-12-01 22:23:02 -06:00
|
|
|
if gosrc == "" {
|
|
|
|
goSrcDir, err := f.findGoSrc()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("forge init() findGoSrc()", err)
|
|
|
|
}
|
|
|
|
os.Setenv("FORGE_GOSRC", goSrcDir)
|
2024-11-28 00:02:27 -06:00
|
|
|
}
|
2024-12-01 22:23:02 -06:00
|
|
|
f.goSrc = os.Getenv("FORGE_GOSRC")
|
2024-11-28 23:00:29 -06:00
|
|
|
|
2024-12-05 12:37:07 -06:00
|
|
|
// 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)
|
|
|
|
}
|
2024-12-12 23:50:38 -06:00
|
|
|
|
|
|
|
// check again for go.work // user could have a go.work file in ~/go/src
|
2024-12-10 01:48:29 -06:00
|
|
|
if f.goWorkExists() {
|
|
|
|
f.goWork = true
|
|
|
|
}
|
2024-12-12 23:50:38 -06:00
|
|
|
|
|
|
|
// print out the settings that will be used
|
2024-12-13 02:22:13 -06:00
|
|
|
log.Info("forgepb.Init() FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
|
2024-11-28 18:36:15 -06:00
|
|
|
|
2024-11-28 00:02:27 -06:00
|
|
|
// load the ~/.config/forge/ config
|
2024-12-13 02:22:13 -06:00
|
|
|
f.Config = new(ForgeConfigs)
|
2024-11-28 00:02:27 -06:00
|
|
|
if err := f.Config.ConfigLoad(); err != nil {
|
|
|
|
log.Warn("forgepb.ConfigLoad() failed", err)
|
|
|
|
}
|
2024-11-28 08:35:39 -06:00
|
|
|
|
2024-12-13 02:22:13 -06:00
|
|
|
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)")
|
|
|
|
}
|
2024-11-28 19:56:46 -06:00
|
|
|
f.Repos = new(gitpb.Repos)
|
2024-12-01 10:44:29 -06:00
|
|
|
f.Repos.ConfigLoad()
|
2024-11-28 18:36:15 -06:00
|
|
|
return f
|
2024-11-28 00:02:27 -06:00
|
|
|
}
|
2024-12-17 06:37:00 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|