summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-02 17:37:21 -0500
committerJeff Carr <[email protected]>2025-10-02 17:37:21 -0500
commitec88fb8767771c71885ed3a9eb041a42cdd34119 (patch)
tree31c109128c9a4df1eed0324f6cce32b96f2d5e8a
parente59f87c92c8df9465952ecd8c76478e4dc351608 (diff)
better error handling
-rw-r--r--config.go54
-rw-r--r--forgeConfig.proto17
-rw-r--r--init.go62
3 files changed, 70 insertions, 63 deletions
diff --git a/config.go b/config.go
index 508e3e1..0820a1e 100644
--- a/config.go
+++ b/config.go
@@ -3,7 +3,13 @@
package forgepb
import (
+ "errors"
+ "os"
+ "os/user"
+ "path/filepath"
+
"go.wit.com/lib/config"
+ "go.wit.com/lib/fhelp"
"go.wit.com/lib/gui/prep"
"go.wit.com/log"
)
@@ -65,8 +71,9 @@ func (cfg *ForgeConfigs) DumpENV() {
if cfg.GoWork {
log.Infof("CfgPB.GoWork = %v\n", cfg.GoWork)
}
- log.Infof("CfgPB.Mode = %s\n", cfg.Mode)
- log.Infof("CfgPB.PatchDir = %s #deprecate\n", cfg.PatchDir)
+ if cfg.Mode != ForgeMode_UNKNOWN {
+ log.Infof("CfgPB.Mode = %s\n", cfg.Mode)
+ }
// log.Infof("CfgPB.Hostname=%s\n", cfg.Hostname)
}
@@ -80,3 +87,46 @@ func (cfg *ForgeConfigs) DumpENV() {
f.SetConfigSave(true)
}
*/
+
+func loadStdConfig() *ForgeConfigs {
+ cfg := NewForgeConfigs()
+ err := config.ConfigLoad(cfg, "forge", "forge")
+
+ if err == nil {
+ return cfg
+ }
+
+ if errors.Is(err, os.ErrNotExist) {
+ log.Info("no files existed err=", err)
+ } else if errors.Is(err, config.ErrEmpty) {
+ log.Info("files were blank err=", err)
+ } else if errors.Is(err, config.ErrMarshal) {
+ log.Info("files existed and could not be Marshalled() err=", err)
+ } else {
+ log.Info("some other bad problem err=", err)
+ }
+
+ // this is probably the users first time
+ // TODO: check if ./config/forge exists, then something else went wrong...
+
+ usr, _ := user.Current()
+ cfg.Username = usr.Username
+
+ homeDir, _ := os.UserHomeDir()
+
+ cfgdir := filepath.Join(homeDir, ".config/forge")
+ os.MkdirAll(cfgdir, os.ModePerm)
+
+ cfg.HomeDir = cfgdir
+ cfg.ReposDir = filepath.Join(homeDir, "go/src") // todo: check working directory
+ cfg.ReposPB = filepath.Join(cfgdir, "repos.pb")
+ cfg.PatchPB = filepath.Join(cfgdir, "patches.pb")
+ cfg.ForgeURL = "http://forge.wit.com/"
+
+ cfg.DumpENV()
+ if !fhelp.QuestionUser("This is your first time using forge, use these default values?") {
+ os.Exit(-1)
+ }
+ cfg.ConfigSave()
+ return cfg
+}
diff --git a/forgeConfig.proto b/forgeConfig.proto
index 9733bb0..29b454a 100644
--- a/forgeConfig.proto
+++ b/forgeConfig.proto
@@ -39,11 +39,12 @@ message ForgeConfig { // `autogenpb:nom
}
enum ForgeMode {
- MASTER = 0; // "release mode"
- DEVEL = 1; // "patch mode"
- USER = 2; // "work mode"
- NORMAL = 3; // "normal mode" // use this when you are developing code
- CLEAN = 4; // indicates "clean" was run
+ UNKNOWN = 0; // set as first time user
+ MASTER = 1; // "release mode"
+ DEVEL = 2; // "patch mode"
+ USER = 3; // "work mode"
+ NORMAL = 4; // "normal mode" // use this when you are developing code
+ CLEAN = 5; // indicates "clean" was run
}
message ForgeConfigs { // `autogenpb:marshal` `autogenpb:nomutex`
@@ -57,9 +58,9 @@ message ForgeConfigs { // `autogenpb:mar
ForgeMode mode = 8; // what "mode" forge is in
bool goWork = 9; // true if there is a go.work file
bool pathLock = 10; // the path is locked
- string ReposPB = 11; // where the repos.pb is
- string ReposDir = 12; // where the repos are
- string PatchDir = 13; // patch dir
+ string HomeDir = 11; // the forge config dir home
+ string ReposPB = 12; // where the repos.pb is
+ string ReposDir = 13; // where your repos are (orginally set to the GO default ~/go/src)
string PatchPB = 14; // fullpath to patches file
string ForgeURL = 15; // forge URL
string Filename = 16; // filename of the config file
diff --git a/init.go b/init.go
index bb20842..b09d246 100644
--- a/init.go
+++ b/init.go
@@ -4,11 +4,9 @@ package forgepb
import (
"os"
- "os/user"
"path/filepath"
"go.wit.com/lib/config"
- "go.wit.com/lib/fhelp"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
@@ -25,42 +23,18 @@ func Default(opts ...OptionFunc) *Engine {
*/
func Init() *Forge {
- f := new(Forge)
- cfg := new(ForgeConfigs)
- err := config.ConfigLoad(cfg, "forge", "forge")
- f.Config = cfg
- if err != nil {
- f.setenv()
- if !fhelp.QuestionUser("This is your first time using forge, use these default values?") {
- os.Exit(-1)
- }
- f.Config.ConfigSave()
- f.initFromConfig()
- f.Config.DumpENV()
- return f
- }
- if f.Config.Username == "" {
- usr, _ := user.Current()
- f.Config.Username = usr.Username
- f.Config.ConfigSave()
- }
- f.initFromConfig()
- return f
+ cfg := loadStdConfig() // will also handle new users
+ return initFromConfig(cfg)
}
func InitByAppname(argname string) *Forge {
- cfg := new(ForgeConfigs)
+ cfg := NewForgeConfigs()
err := config.ConfigLoad(cfg, argname, "forge")
if err != nil {
- log.Info("forge has not been configured yet", cfg.Filename)
- log.Info("go install go.wit.com/apps/forge@latest")
+ log.Info("ConfigLoad() error", cfg.Filename, err)
os.Exit(-1)
}
- f := new(Forge)
- f.Config = cfg
- f.initFromConfig()
- log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len())
- return f
+ return initFromConfig(cfg)
}
func InitByFullpath(filename string) *Forge {
@@ -70,25 +44,19 @@ func InitByFullpath(filename string) *Forge {
log.Info("forge load config err", err)
os.Exit(-1)
}
- return InitByConfig(cfg)
+ return initFromConfig(cfg)
}
-func InitByConfig(cfg *ForgeConfigs) *Forge {
+func initFromConfig(cfg *ForgeConfigs) *Forge {
if cfg == nil {
log.Info("forge config was == nil")
os.Exit(-1)
}
f := new(Forge)
f.Config = cfg
- f.initFromConfig()
-
- log.Printf("forge.InitByConfig() %s Repos.len()=%d\n", f.Config.Filename, f.Repos.Len())
- return f
-}
-func (f *Forge) initFromConfig() {
if f.configENV() {
- log.Info("ENV changed config. todo: save config here")
+ log.Info("ENV changed config")
f.Config.ConfigSave()
}
if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" {
@@ -117,6 +85,7 @@ func (f *Forge) initFromConfig() {
if f.Config.RillY == 0 {
f.Config.RillY = 20
}
+ return f
}
func (f *Forge) SetConfigSave(b bool) {
@@ -137,19 +106,6 @@ func (f *Forge) Exit() {
os.Exit(0)
}
-// 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() {
- log.Info("doing setenv()")
- if f.Config == nil {
- log.Info("forge.Config() was nil")
- os.Exit(-1)
- }
- })
-}
-
func (f *Forge) GetForgeURL() string {
return f.Config.ForgeURL
}