summaryrefslogtreecommitdiff
path: root/init.go
diff options
context:
space:
mode:
Diffstat (limited to 'init.go')
-rw-r--r--init.go120
1 files changed, 19 insertions, 101 deletions
diff --git a/init.go b/init.go
index 5c83a45..ce7d8eb 100644
--- a/init.go
+++ b/init.go
@@ -3,12 +3,8 @@
package forgepb
import (
- "errors"
- "os"
- "path/filepath"
-
+ "go.wit.com/lib/cobol"
"go.wit.com/lib/config"
- "go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
@@ -25,15 +21,15 @@ func Default(opts ...OptionFunc) *Engine {
*/
func Init() (*Forge, error) {
- cfg, err := LoadStdConfig() // will also handle new users
+ cfg, err := loadStdConfig() // will also handle new users
return initFromConfig(cfg), err
}
func InitByAppname(argname string) *Forge {
cfg := NewForgeConfigs()
- err := config.ConfigLoad(cfg, argname, "forge")
+ err := cfg.loadConfig()
if err != nil {
- log.Info("ConfigLoad() error", cfg.Filename, err)
+ log.Info("ConfigLoad() error. make a default config here?", err)
}
return initFromConfig(cfg)
}
@@ -53,53 +49,27 @@ func initFromConfig(cfg *ForgeConfigs) *Forge {
}
f := new(Forge)
f.Config = cfg
-
- if f.configENV() {
- log.Info("ENV changed config")
- f.Config.ConfigSave()
- }
- if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" {
- f.Config.DumpPB()
- log.Infof("ReposPB invalid filename '%s'\n", f.Config.ReposPB)
- }
+ f.Config.loadConfig()
// todo: play with these / determine good values based on user's machine
- if f.Config.RillX == 0 {
- f.Config.RillX = 10
+ if cobol.Int(config.Get("RillX")) == 0 {
+ config.Set("RillX", "10")
+ config.Save()
}
- if f.Config.RillY == 0 {
- f.Config.RillY = 20
- }
-
- if !shell.Exists(f.Config.ReposPB) {
- // create an initial repos.pb file
- // panic() here? // warning? // (probably not. it's just the repos.pb cache file
- f.Repos = gitpb.NewRepos()
- f.Repos.Filename = f.Config.ReposPB
- f.Repos.Save()
-
- if f.Config.Mode == ForgeMode_NEWUSER {
- // new user. drop back to main() for an introduction
- return f
- }
+ if cobol.Int(config.Get("RillY")) == 0 {
+ config.Set("RillY", "20")
+ config.Save()
}
+ // create an initial repos.pb file
+ // panic() here? // warning? // (probably not. it's just the repos.pb cache file
f.Repos = gitpb.NewRepos()
- f.Repos.ConfigLoad(f.Config.ReposPB)
-
- f.Patchsets = NewSets()
- if f.Config.PatchPB == "" {
- // init default Patchsets?
- // save patchsets here?
+ err := f.Repos.CacheLoad() // loads the file from ~/.cache/forge/repos.pb
+ if err == nil {
+ log.Printf("forge loaded %s file with len(%d) repos\n", f.Repos.Filename, f.Repos.Len())
} else {
- if err := config.LoadFile(f.Patchsets, f.Config.PatchPB); err != nil {
- if errors.Is(err, os.ErrNotExist) {
- log.Info("forge.Init() making new patches file", f.Config.PatchPB)
- f.SavePatchsets()
- } else {
- log.Info("forge.Init() load patches failed", err)
- }
- }
+ log.Printf("forge failed to load %s file with len(%d) repos err=(%v)\n", f.Repos.Filename, f.Repos.Len(), err)
+ panic("failed to load repos.pb")
}
return f
}
@@ -115,7 +85,7 @@ func (f *Forge) Close() error {
}
if f.Repos != nil {
if config.HasChanged("repos") {
- if err := f.Repos.ConfigSave(f.Config.ReposPB); err != nil {
+ if err := f.Repos.Save(); err != nil {
log.Info("forge.Repos.ConfigSave() error", err)
return err
}
@@ -123,55 +93,3 @@ func (f *Forge) Close() error {
}
return nil
}
-
-/*
-// saves the config if there have been changes
-func (f *Forge) Exit() {
- f.ConfigSave()
- if f.Repos != nil {
- if config.HasChanged("repos") {
- if err := f.Repos.ConfigSave(f.Config.ReposPB); err != nil {
- log.Info("forge.Repos.ConfigSave() error", err)
- }
- }
- }
- // log.Info("forge.Exit() ok")
- os.Exit(0)
-}
-*/
-
-func (f *Forge) GetForgeURL() string {
- return f.Config.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.Config.ForgeURL = url
- os.Setenv("FORGE_URL", f.Config.ForgeURL)
- log.Info("Forge URL has been set to", f.Config.ForgeURL)
-}
-
-// 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) configENV() bool {
- var changed bool
- f.once.Do(func() {
- if os.Getenv("FORGE_URL") != "" {
- f.Config.ForgeURL = os.Getenv("FORGE_URL")
- }
- if f.hostname == "" {
- f.hostname, _ = os.Hostname()
- }
- })
- if changed {
- // save config here
- }
- return changed
-}