forgepb/init.go

227 lines
4.8 KiB
Go

// Copyright 2025 WIT.COM Inc Licensed GPL 3.0
package forgepb
import (
"errors"
"os"
"os/user"
"path/filepath"
"time"
"go.wit.com/lib/fhelp"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
/* better syntax from gin
Default returns an Engine instance with the Logger and Recovery middleware already attached.
func Default(opts ...OptionFunc) *Engine {
debugPrintWARNINGDefault()
engine := New()
engine.Use(Logger(), Recovery())
return engine.With(opts...)
}
*/
func Init() *Forge {
f := InitPB()
/*
f.Machine = new(zoopb.Machine)
if err := f.Machine.ConfigLoad(); err != nil {
log.Log(WARN, "zoopb.ConfigLoad() failed", err)
}
*/
if f.Config.Username == "" {
usr, _ := user.Current()
f.Config.Username = usr.Username
f.SetConfigSave(true)
}
if f.Config.Xterm == "" {
f.Config.Xterm = "xterm"
f.Config.XtermArgv = append(f.Config.XtermArgv, "-bg")
f.Config.XtermArgv = append(f.Config.XtermArgv, "black")
f.Config.XtermArgv = append(f.Config.XtermArgv, "-fg")
f.Config.XtermArgv = append(f.Config.XtermArgv, "white")
f.SetConfigSave(true)
}
// f.Machine.InitWit()
if f.hasFullScan {
// duplicate time checking below. which one to keep?
if f.FullScanAge() > time.Minute {
log.Log(INFO, "forgepb.Scan() skipping scan. been run a minute ago", f.FullScanAge())
return f
}
}
now := time.Now()
start := f.Repos.Len()
f.ScanGoSrc()
f.FullScanRan()
end := f.Repos.Len()
if (end - start) == 0 {
log.Log(INFO, "forgepb.Scan() Scan did not find new git repositories. Total =", end)
if f.FullScanAge() > time.Minute {
f.rillUpdate(20, 10)
}
} else {
log.Log(INFO, "forgepb.Scan() Scan found", end-start, "new git repositories. Total =", end)
f.rillUpdate(20, 10)
}
if f.configSave {
// taking this out to debug Marshal() panic
f.ConfigSave()
f.configSave = false
}
log.Log(INFO, "update() check took", shell.FormatDuration(time.Since(now)))
return f
}
func FirstTimeUser() bool {
if checkenv() {
return false
}
// setup the env
f := new(Forge)
f.setenv()
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), "forge.text")
_, err := os.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {
return true
}
return false
}
func (f *Forge) InitPB() {
f.setenv()
// load the ~/.config/forge/ config
f.Config = new(ForgeConfigs)
if err := f.Config.ConfigLoad(f.configDir); err != nil {
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
}
f.Repos = gitpb.NewRepos()
f.Repos.ConfigLoad()
if f.Repos.HasFullScan {
f.hasFullScan = true
}
// init the Patchsets
f.Patchsets = NewPatchsets()
// todo: play with these / determine good values based on user's machine
f.rillX = 10
f.rillY = 20
}
func (f *Forge) InitMachine() {
if f.Config.Username == "" {
usr, _ := user.Current()
f.Config.Username = usr.Username
}
f.hostname, _ = os.Hostname()
// log.Info(hostname, err)
}
// only init's the protobuf. intended to not scan or change anything
func InitPB() *Forge {
f := new(Forge)
f.setenv()
f.InitPB()
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)
}
func RawInitPB() *Forge {
f := new(Forge)
f.RawInitPB()
return f
}
func (f *Forge) RawInitPB() {
f.InitPB()
}
// the first thing done is process any ENV settings
// try to NOT use the ENV settings anywhere but here
// all initial ENV settings should be stored in the forge struct
func (f *Forge) setenv() {
f.once.Do(func() {
if err := fhelp.ConfigureENV(); err != nil {
log.Info("forge ConfigureENV() failed", err)
os.Exit(-1)
}
f.configDir = os.Getenv("FORGE_CONFIG")
f.goSrc = os.Getenv("FORGE_GOSRC")
f.repoPB = os.Getenv("FORGE_REPOPB")
f.forgeURL = os.Getenv("FORGE_URL")
f.patchDir = os.Getenv("FORGE_PATCHDIR")
f.hostname = os.Getenv("HOSTNAME")
if os.Getenv("FORGE_GOWORK") == "true" {
f.goWork = true
}
})
}
// if the env vars are set, this is probably not a new user
func checkenv() bool {
if os.Getenv("FORGE_CONFIG") != "" {
return true
}
if os.Getenv("FORGE_GOSRC") != "" {
return true
}
if os.Getenv("FORGE_REPOPB") != "" {
return true
}
if os.Getenv("FORGE_URL") != "" {
return true
}
if os.Getenv("FORGE_PATCHDIR") != "" {
return true
}
if os.Getenv("FORGE_VERBOSE") != "" {
return true
}
return false
}
func (f *Forge) GetForgeURL() string {
return f.forgeURL
}
// set the URL for forge otherwise fallback to ENV or to forge.wit.com
func (f *Forge) SetForgeURL(url string) {
if url == "" {
url = os.Getenv("FORGE_URL")
}
if url == "" {
url = "https://forge.wit.com/"
}
f.forgeURL = url
os.Setenv("FORGE_URL", f.forgeURL)
log.Info("Forge URL has been set to", f.forgeURL)
}