diff options
| -rw-r--r-- | config.go | 37 | ||||
| -rw-r--r-- | init.go | 10 |
2 files changed, 27 insertions, 20 deletions
@@ -111,18 +111,17 @@ func (cfg *ForgeConfigs) DumpPB() { } */ -func LoadStdConfig() *ForgeConfigs { +func LoadStdConfig() (*ForgeConfigs, error) { cfg := NewForgeConfigs() err := config.ConfigLoad(cfg, "forge", "forge") if cfg.Filename == "" { - panic("wtf filename blank") - } - - if err == nil { - return cfg + panic("lib/config broken. filename is blank") } + // todo: do something here with this error? + // right now, handled in forge + // todo: move the code from forge here if errors.Is(err, os.ErrNotExist) { log.Info("no files existed err=", err) } else if errors.Is(err, config.ErrEmpty) { @@ -133,8 +132,13 @@ func LoadStdConfig() *ForgeConfigs { 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... + return cfg, nil +} + +func MakeDefaultConfig() (*ForgeConfigs, error) { + cfg := NewForgeConfigs() + // Get fullpath to ~/.config/forge/forge.text + cfg.Filename = config.GetConfigFilename("forge", "forge") usr, _ := user.Current() cfg.Username = usr.Username @@ -152,17 +156,22 @@ func LoadStdConfig() *ForgeConfigs { cfg.ReposDir = filepath.Join(homeDir, "go/src") // todo: check working directory os.MkdirAll(cfg.ReposDir, 0755) + // try new idea using lib/config + config.Set("Username", usr.Username) + config.Set("HomeDir", cfgdir) + config.Set("ReposPB", filepath.Join(cfgdir, "repos.pb")) + config.Set("PatchPB", filepath.Join(cfgdir, "patches.pb")) + config.Set("ForgeURL", "https://forge.grid.wit.com/") + config.Set("ReposDir", filepath.Join(homeDir, "go/src")) + cfg.addSampleConfigs() cfg.DumpENV() config.SetChanged("forge", true) - // if !fhelp.QuestionUser("This is your first time using forge, use these default values?") { - // os.Exit(-1) - // } - if err := cfg.ConfigSave(); err != nil { + var err error + if err = cfg.ConfigSave(); err != nil { log.Info("config save error:", err) - os.Exit(-1) } - return cfg + return cfg, err } // first time user. add go.wit.com as an example @@ -25,8 +25,8 @@ func Default(opts ...OptionFunc) *Engine { */ func Init() (*Forge, error) { - cfg := LoadStdConfig() // will also handle new users - return initFromConfig(cfg), nil + cfg, err := LoadStdConfig() // will also handle new users + return initFromConfig(cfg), err } func InitByAppname(argname string) *Forge { @@ -34,7 +34,6 @@ func InitByAppname(argname string) *Forge { err := config.ConfigLoad(cfg, argname, "forge") if err != nil { log.Info("ConfigLoad() error", cfg.Filename, err) - os.Exit(-1) } return initFromConfig(cfg) } @@ -44,7 +43,6 @@ func InitByFullpath(filename string) *Forge { err := config.LoadFile(cfg, filename) if err != nil { log.Info("forge load config err", err) - os.Exit(-1) } return initFromConfig(cfg) } @@ -52,7 +50,6 @@ func InitByFullpath(filename string) *Forge { func initFromConfig(cfg *ForgeConfigs) *Forge { if cfg == nil { log.Info("forge config was == nil") - os.Exit(-1) } f := new(Forge) f.Config = cfg @@ -64,7 +61,6 @@ func initFromConfig(cfg *ForgeConfigs) *Forge { if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" { f.Config.DumpPB() log.Infof("ReposPB invalid filename '%s'\n", f.Config.ReposPB) - os.Exit(-1) } // todo: play with these / determine good values based on user's machine @@ -128,6 +124,7 @@ func (f *Forge) Close() error { return nil } +/* // saves the config if there have been changes func (f *Forge) Exit() { f.ConfigSave() @@ -141,6 +138,7 @@ func (f *Forge) Exit() { // log.Info("forge.Exit() ok") os.Exit(0) } +*/ func (f *Forge) GetForgeURL() string { return f.Config.ForgeURL |
