// Copyright 2025 WIT.COM Inc Licensed GPL 3.0 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" ) /* better syntax from gin Default returns an Engine instance with the Logger and Recovery middleware already attached. func Default(opts ...OptionFunc) *Engine { engine := New() engine.Use(Logger(), Recovery()) return engine.With(opts...) } */ 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() if f.Config.Mode == ForgeMode_MASTER { log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len()) f.Config.DumpENV() } return f } func InitByAppname(argname string) *Forge { cfg := new(ForgeConfigs) 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") 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 } func InitByFullpath(filename string) *Forge { cfg := NewForgeConfigs() err := config.LoadFile(cfg, filename) if err != nil { log.Info("forge load config err", err) os.Exit(-1) } return InitByConfig(cfg) } func InitByConfig(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") f.Config.ConfigSave() } if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" { f.Config.DumpENV() log.Infof("ReposPB invalid filename '%s'\n", f.Config.ReposPB) os.Exit(-1) } f.Repos = gitpb.NewRepos() f.Repos.ConfigLoad(f.Config.ReposPB) f.Patchsets = NewSets() if f.Config.PatchPB == "" { // init default Patchsets? // save patchsets here? } else { if err := config.LoadFile(f.Patchsets, f.Config.PatchPB); err != nil { log.Info("forge.Init() load patches failed", err) } } // todo: play with these / determine good values based on user's machine if f.Config.RillX == 0 { f.Config.RillX = 10 } if f.Config.RillY == 0 { f.Config.RillY = 20 } } func (f *Forge) SetConfigSave(b bool) { config.SetChanged("forge", b) } // 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) } // 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 } // 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 }