make forge work for a new user

This commit is contained in:
Jeff Carr 2025-09-21 20:49:32 -05:00
parent e953b998ae
commit c752d40839
5 changed files with 90 additions and 72 deletions

View File

@ -3,6 +3,8 @@
package forgepb package forgepb
import ( import (
"os"
"go.wit.com/lib/config" "go.wit.com/lib/config"
"go.wit.com/lib/gui/prep" "go.wit.com/lib/gui/prep"
"go.wit.com/log" "go.wit.com/log"
@ -32,3 +34,59 @@ func (f *Forge) ConfigSave() error {
} }
return err return err
} }
// functions to import and export the protobuf
// data to and from config files
// write to ~/.config/forge/
func (cfg *ForgeConfigs) ConfigSave() error {
var header string
header += "\n"
header += "# the forge config file\n"
header += "# You can customize things like:\n"
header += "#\n"
header += "# * which repos you have write access to\n"
header += "# * custom branch names for 'master', 'devel' and 'user'\n"
header += "# * 'favorites' so you can remember which things you like\n"
header += "# * sometimes protobuf TEXT can fail so as a backup this also creates a .json file\n"
header += "#\n"
header += "\n"
return config.ConfigSaveWithHeader(cfg, header)
}
func (cfg *ForgeConfigs) DumpENV() {
log.Infof("CfgPB.Filename = %s\n", cfg.Filename)
log.Infof("CfgPB.ReposPB = %s\n", cfg.ReposPB)
log.Infof("CfgPB.ReposDir = %s\n", cfg.ReposDir)
log.Infof("CfgPB.PatchDir = %s\n", cfg.PatchDir)
log.Infof("CfgPB.ForgeURL = %s\n", cfg.ForgeURL)
if cfg.GoWork {
log.Infof("CfgPB.GoWork = %v\n", cfg.GoWork)
}
log.Infof("CfgPB.Mode = %s\n", cfg.Mode)
// log.Infof("CfgPB.Hostname=%s\n", cfg.Hostname)
if cfg.ReposPB != os.Getenv("FORGE_REPOSPB") {
log.Infof("CfgPB file problem: cfg.ReposPB=%s != FORGE_REPOSPB=%s\n", cfg.ReposPB, os.Getenv("FORGE_REPOSPB"))
}
}
/*
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)
}
*/
func (cfg *ForgeConfigs) InitDefaults() {
}

View File

@ -1,56 +0,0 @@
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0
package forgepb
import (
"os"
"go.wit.com/lib/config"
"go.wit.com/log"
)
// functions to import and export the protobuf
// data to and from config files
// write to ~/.config/forge/
func (cfg *ForgeConfigs) ConfigSave() error {
var header string
header += "\n"
header += "# the forge config file\n"
header += "# You can customize things like:\n"
header += "#\n"
header += "# * which repos you have write access to\n"
header += "# * custom branch names for 'master', 'devel' and 'user'\n"
header += "# * 'favorites' so you can remember which things you like\n"
header += "#\n"
header += "\n"
return config.ConfigSaveWithHeader(cfg, header)
}
func (cfg *ForgeConfigs) DumpENV() {
if cfg.ReposPB != os.Getenv("FORGE_REPOSPB") {
log.Infof("RepoPB file problem: cfg.ReposPB=%s != FORGE_REPOSPB=%s\n", cfg.ReposPB, os.Getenv("FORGE_REPOSPB"))
}
}
// load the ~/.config/forge/ files
func (c *ForgeConfigs) ConfigLoad(fullpath string) error {
return nil
}
/*
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)
}
*/

View File

@ -55,7 +55,7 @@ message ForgeConfigs { // `autogenpb:mar
repeated string xtermArgv = 6; // the argv line for xterm repeated string xtermArgv = 6; // the argv line for xterm
string defaultGui = 7; // default GUI plugin to use string defaultGui = 7; // default GUI plugin to use
ForgeMode mode = 8; // what "mode" forge is in ForgeMode mode = 8; // what "mode" forge is in
string goSrc = 9; // is ~/go/src unless a go.work file is found bool goWork = 9; // true if there is a go.work file
bool pathLock = 10; // the path is locked bool pathLock = 10; // the path is locked
string ReposPB = 11; // where the repos.pb is string ReposPB = 11; // where the repos.pb is
string ReposDir = 12; // where the repos are string ReposDir = 12; // where the repos are

40
init.go
View File

@ -24,17 +24,26 @@ func Default(opts ...OptionFunc) *Engine {
*/ */
func Init() *Forge { func Init() *Forge {
f := new(Forge)
cfg := new(ForgeConfigs) cfg := new(ForgeConfigs)
err := config.ConfigLoad(cfg, "forge", "forge") err := config.ConfigLoad(cfg, "forge", "forge")
f.Config = cfg
if err != nil { if err != nil {
log.Info("forge has not been configured yet filename =", cfg.Filename) // fhelp.DumpENV("finit:")
log.Info("go install go.wit.com/apps/forge@latest") f.setenv()
if !fhelp.QuestionUser("This is your first time using forge, use these default values?") {
os.Exit(-1) os.Exit(-1)
} }
f := initFromConfig(cfg) f.Config.InitDefaults()
f.Config.ConfigSave()
f.initFromConfig()
f.Config.DumpENV()
return f
}
f.initFromConfig()
if f.Config.Mode == ForgeMode_MASTER { if f.Config.Mode == ForgeMode_MASTER {
log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len()) log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len())
fhelp.DumpENV("finit:") // fhelp.DumpENV("finit:")
f.Config.DumpENV() f.Config.DumpENV()
} }
return f return f
@ -48,14 +57,14 @@ func InitByAppname(argname string) *Forge {
log.Info("go install go.wit.com/apps/forge@latest") log.Info("go install go.wit.com/apps/forge@latest")
os.Exit(-1) os.Exit(-1)
} }
f := initFromConfig(cfg) f := new(Forge)
f.Config = cfg
f.initFromConfig()
log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len()) log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len())
return f return f
} }
func initFromConfig(cfg *ForgeConfigs) *Forge { func (f *Forge) initFromConfig() {
f := new(Forge)
f.Config = cfg
if f.configENV() { if f.configENV() {
log.Info("ENV changed config. todo: save config here") log.Info("ENV changed config. todo: save config here")
f.Config.ConfigSave() f.Config.ConfigSave()
@ -80,9 +89,12 @@ func initFromConfig(cfg *ForgeConfigs) *Forge {
f.Patchsets = NewPatchsets() f.Patchsets = NewPatchsets()
// todo: play with these / determine good values based on user's machine // todo: play with these / determine good values based on user's machine
if f.Config.RillX == 0 {
f.Config.RillX = 10 f.Config.RillX = 10
}
if f.Config.RillY == 0 {
f.Config.RillY = 20 f.Config.RillY = 20
return f }
} }
func (f *Forge) SetConfigSave(b bool) { func (f *Forge) SetConfigSave(b bool) {
@ -93,8 +105,9 @@ func (f *Forge) SetConfigSave(b bool) {
func (f *Forge) Exit() { func (f *Forge) Exit() {
// log.Info("forge.configSave =", f.configSave) // log.Info("forge.configSave =", f.configSave)
if f.Config.Mode == ForgeMode_MASTER { if f.Config.Mode == ForgeMode_MASTER {
fhelp.DumpENV("forge:") // fhelp.DumpENV("forge:")
f.Config.DumpENV() // f.Config.DumpENV()
// todo: tell the user to switch to NORMAL mode
} }
f.ConfigSave() f.ConfigSave()
@ -114,6 +127,7 @@ func (f *Forge) Exit() {
// all initial ENV settings should be stored in the forge struct // all initial ENV settings should be stored in the forge struct
func (f *Forge) setenv() { func (f *Forge) setenv() {
f.once.Do(func() { f.once.Do(func() {
log.Info("doing setenv()")
if err := fhelp.ConfigureENV(); err != nil { if err := fhelp.ConfigureENV(); err != nil {
log.Info("forge ConfigureENV() failed", err) log.Info("forge ConfigureENV() failed", err)
os.Exit(-1) os.Exit(-1)
@ -128,9 +142,11 @@ func (f *Forge) setenv() {
f.goWork = true f.goWork = true
} }
f.Config.ReposPB = os.Getenv("FORGE_REPOPB") f.Config.ReposPB = os.Getenv("FORGE_REPOSPB")
f.Config.ReposDir = os.Getenv("FORGE_REPOSDIR")
f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR") f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR")
f.Config.ForgeURL = os.Getenv("FORGE_URL") f.Config.ForgeURL = os.Getenv("FORGE_URL")
fhelp.DumpENV("setenv end()")
}) })
} }