lots of Init() cleanups

This commit is contained in:
Jeff Carr 2025-09-03 13:54:36 -05:00
parent 888442708c
commit 98cc06ab8f
8 changed files with 74 additions and 120 deletions

View File

@ -19,7 +19,7 @@ import (
func (f *Forge) ConfigSave() error {
var err error
// backup the current config files
if e := backupConfig(); e != nil {
if e := f.backupConfig(); e != nil {
log.Info("forge.BackupConfig() error", e)
err = e
// continue here? notsure. could be bad either way
@ -45,7 +45,7 @@ func (f *Forge) ConfigSave() error {
return err
}
// write to ~/.config/forge/ unless ENV{FORGE_CONFIG} is set
// write to ~/.config/forge/
func (f *ForgeConfigs) ConfigSave() error {
data, err := f.Marshal()
if err != nil {
@ -64,12 +64,7 @@ func (f *ForgeConfigs) ConfigSave() error {
}
// load the ~/.config/forge/ files
func (c *ForgeConfigs) ConfigLoad() error {
if os.Getenv("FORGE_CONFIG") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_CONFIG", fullpath)
}
func (c *ForgeConfigs) ConfigLoad(fullpath string) error {
// var data []byte
// var err error
if c == nil {

View File

@ -12,10 +12,10 @@ import (
"time"
)
func backupConfig() error {
func (f *Forge) backupConfig() error {
// make a new dir to backup the files
srcDir := filepath.Join(os.Getenv("FORGE_CONFIG"))
destDir := filepath.Join(os.Getenv("FORGE_CONFIG"), "backup")
srcDir := filepath.Join(f.configDir)
destDir := filepath.Join(f.configDir, "backup")
return backupFiles(srcDir, destDir)
}

View File

@ -330,7 +330,7 @@ func (psets *Patchsets) PrintTable() {
log.DaemonMode(true)
// print the header
args := []string{"commit hash", "what", "age", "cId", "partId", "", "", "", "", ""}
args := []string{"commit hash", "", "", "name", "Repo Namespace", "", "", "", "", ""}
sizes := []int{36, 3, 3, 40, 80, 2, 2, 2, 2, 2}
log.Info(cobol.StandardTableSize10(sizes, args))

158
init.go
View File

@ -5,9 +5,9 @@ package forgepb
import (
"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"
@ -84,87 +84,26 @@ func Init() *Forge {
return f
}
func DetermineGoPath() *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.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)
}
f.configDir = os.Getenv("FORGE_CONFIG")
// 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.Log(INFO, "forgepb.Init() FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
return f
}
func (f *Forge) InitPB() {
f.setenv()
// load the ~/.config/forge/ config
f.Config = new(ForgeConfigs)
if err := f.Config.ConfigLoad(); err != nil {
if err := f.Config.ConfigLoad(f.configDir); err != nil {
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
}
if f.IsGoWork() {
log.Log(INFO, "forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = true)")
} else {
log.Log(INFO, "forgepb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "(go.work = false)")
}
f.Repos = gitpb.NewRepos()
f.Repos.ConfigLoad()
if f.Repos.HasFullScan {
f.hasFullScan = true
}
f.forgeURL = "https://forge.wit.com/"
if os.Getenv("FORGE_URL") != "" {
f.forgeURL = os.Getenv("FORGE_URL")
log.Info("got forge url", f.forgeURL)
}
// todo: play with these / determine good values based on user's machine
f.rillX = 10
f.rillY = 20
}
// 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
log.Info("Forge URL has been set to", f.forgeURL)
}
func (f *Forge) GetForgeURL() string {
return f.forgeURL
}
func (f *Forge) InitMachine() {
if f.Config.Username == "" {
usr, _ := user.Current()
@ -176,7 +115,8 @@ func (f *Forge) InitMachine() {
// only init's the protobuf. intended to not scan or change anything
func InitPB() *Forge {
f := DetermineGoPath()
f := new(Forge)
f.setenv()
f.InitPB()
return f
}
@ -197,55 +137,65 @@ func (f *Forge) Exit() {
func RawInitPB() *Forge {
f := new(Forge)
f.RawInitPB()
return f
}
homeDir, _ := os.UserHomeDir()
// todo: check or exit if err?
// TODO: rethink this but it works for now
if os.Getenv("FORGE_GOSRC") == "" {
fullpath := filepath.Join(homeDir, ".cache/forge")
err := os.MkdirAll(fullpath, os.ModePerm)
if err != nil {
log.Log(WARN, "mkdir failed", fullpath, err)
}
os.Setenv("FORGE_GOSRC", fullpath)
}
f.goSrc = os.Getenv("FORGE_GOSRC")
// also rethink this, but maybe this is the right thing to do
if os.Getenv("FORGE_CONFIG") == "" {
fullpath := filepath.Join(homeDir, ".config/forge")
os.MkdirAll(fullpath, os.ModePerm)
os.Setenv("FORGE_CONFIG", fullpath)
}
f.configDir = os.Getenv("FORGE_CONFIG")
func (f *Forge) RawInitPB() {
f.setenv()
// load the ~/.config/forge/ config
f.Config = new(ForgeConfigs)
if err := f.Config.ConfigLoad(); err != nil {
if err := f.Config.ConfigLoad(f.configDir); err != nil {
log.Log(WARN, "forgepb.ConfigLoad() failed", err)
}
f.Repos = gitpb.NewRepos()
f.Repos.ConfigLoad()
f.forgeURL = "https://forge.wit.com/"
if os.Getenv("FORGE_URL") != "" {
f.forgeURL = os.Getenv("FORGE_URL")
log.Info("got forge url", f.forgeURL)
}
// where patches are stored
f.patchDir = f.goSrc
if os.Getenv("FORGE_PATCHDIR") != "" {
f.patchDir = os.Getenv("FORGE_PATCHDIR")
}
// todo: play with these / determine good values based on user's machine
f.rillX = 10
f.rillY = 20
return f
}
// 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() {
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")
if os.Getenv("FORGE_GOWORK") == "true" {
f.goWork = true
}
log.Printf("FORGE_CONFIG = %s\n", f.configDir)
log.Printf("FORGE_GOSRC = %s\n", f.goSrc)
log.Printf("FORGE_GOWORK = %v\n", f.goWork)
log.Printf("FORGE_REPOPB = %s\n", f.repoPB)
log.Printf("FORGE_URL = %s\n", f.forgeURL)
log.Printf("FORGE_PATCHDIR = %s\n", f.patchDir)
log.Printf("HOSTNAME = %s\n", os.Getenv("HOSTNAME"))
}
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)
}

View File

@ -44,6 +44,15 @@ func (f *Forge) loadUpstreamPatchsets(psets *Patchsets) {
log.Info("new patchset", pset.Name, pset.Uuid)
pset.State = "new"
foundnew = true
if pset == nil {
log.Warn("OH NO! pset == nil")
continue
}
if f.Patchsets == nil {
log.Warn("OH NO! f.Patchsets == nil")
continue
}
f.Patchsets.Append(pset)
} else {
log.Info("patchset already on disk", found.Name, found.State)
pset.State = found.State
@ -53,7 +62,6 @@ func (f *Forge) loadUpstreamPatchsets(psets *Patchsets) {
}
}
if foundnew {
log.Info("should save these here")
f.SavePatchsets()
}
}

View File

@ -27,7 +27,7 @@ func (f *Forge) LoadPatchsets() error {
log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
return err
}
log.Infof("LoadPatchsets() worked ok %d\n", f.Patchsets.Len())
log.Infof("LoadPatchsets() found %d patches.\n", f.Patchsets.Len())
return nil
}
@ -54,7 +54,7 @@ func (f *Forge) SavePatchsets() error {
log.Infof("SavePatchset() proto.Marshal() error %v\n", err)
return err
}
log.Infof("SavePatchset() worked (%d) bytes\n", len(data))
log.Infof("SavePatchset() worked (%d) bytes on %d patches\n", len(data), f.Patchsets.Len())
regfile.Write(data)
return nil
}

View File

@ -82,7 +82,7 @@ message Patchset { // `autogenpb:mars
string hostname = 14; //
}
message Patchsets { // `autogenpb:marshal` `autogenpb:gui`
message Patchsets { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
string version = 2; // `autogenpb:version:v0.0.45`
repeated Patchset Patchsets = 3;

View File

@ -13,6 +13,7 @@ type Forge struct {
initOnce sync.Once
initErr error // init error, if any
goSrc string // the path to go/src
repoPB string // the path to the repos.pb cache file
configDir string // normally ~/.config/forge
goWork bool // means the user is currently using a go.work file
Config *ForgeConfigs // config repos for readonly, private, etc