summaryrefslogtreecommitdiff
path: root/init.go
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 /init.go
parente59f87c92c8df9465952ecd8c76478e4dc351608 (diff)
better error handling
Diffstat (limited to 'init.go')
-rw-r--r--init.go62
1 files changed, 9 insertions, 53 deletions
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
}