// Copyright 2025 WIT.COM Inc Licensed GPL 3.0 package forgepb import ( "os" "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 { debugPrintWARNINGDefault() engine := New() engine.Use(Logger(), Recovery()) return engine.With(opts...) } */ func Init() *Forge { cfg := new(ForgeConfigs) err := config.ConfigLoad(cfg, "forge", "forge") if err != nil { log.Info("forge has not been configured yet filename =", cfg.Filename) log.Info("go install go.wit.com/apps/forge@latest") os.Exit(-1) } f := initFromConfig(cfg) if f.Config.Mode != ForgeMode_NORMAL { log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len()) fhelp.DumpENV("finit:") 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 := initFromConfig(cfg) log.Printf("forge.Init() %s len()=%d\n", f.Config.Filename, f.Repos.Len()) return f } func initFromConfig(cfg *ForgeConfigs) *Forge { f := new(Forge) f.Config = cfg if f.configENV() { log.Info("ENV changed config. todo: save config here") f.Config.ConfigSave() } f.Repos = gitpb.NewRepos() f.Repos.ConfigLoad(f.Config.ReposPB) // init the Patchsets f.Patchsets = NewPatchsets() // todo: play with these / determine good values based on user's machine f.rillX = 10 f.rillY = 20 return f } /* func (f *Forge) InitMachine() { if f.Config.Username == "" { usr, _ := user.Current() f.Config.Username = usr.Username } f.hostname, _ = os.Hostname() // log.Info(hostname, err) } */ func (f *Forge) SetConfigSave(b bool) { f.configSave = b } // saves the config if there have been changes func (f *Forge) Exit() { // log.Info("forge.configSave =", f.configSave) if f.configSave { f.ConfigSave() } // 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() { if err := fhelp.ConfigureENV(); err != nil { log.Info("forge ConfigureENV() failed", err) os.Exit(-1) } if f.Config == nil { log.Info("forge.Config() was nil") os.Exit(-1) } // f.configDir = os.Getenv("FORGE_CONFIG") // f.forgeURL = os.Getenv("FORGE_URL") f.hostname = os.Getenv("HOSTNAME") if os.Getenv("FORGE_GOWORK") == "true" { f.goWork = true } f.Config.ReposPB = os.Getenv("FORGE_REPOPB") f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR") f.Config.ForgeURL = os.Getenv("FORGE_URL") }) } 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 err := fhelp.ConfigureENV(); err != nil { log.Info("forge ConfigureENV() failed", err) os.Exit(-1) } if os.Getenv("FORGE_REPOPB") != "" && f.Config.ReposPB != os.Getenv("FORGE_REPOPB") { log.Info("ENV: updating FORGE_REPOSPB from", f.Config.ReposPB, "to", os.Getenv("FORGE_REPOPB")) f.Config.ReposPB = os.Getenv("FORGE_REPOPB") changed = true } if os.Getenv("FORGE_GOSRC") != "" && f.Config.ReposDir != os.Getenv("FORGE_GOSRC") { log.Info("ENV: updating FORGE_GOSRC from", f.Config.ReposDir, "to", os.Getenv("FORGE_GOSRC")) f.Config.ReposDir = os.Getenv("FORGE_GOSRC") changed = true } if os.Getenv("FORGE_PATCHDIR") != "" && f.Config.PatchDir != os.Getenv("FORGE_PATCHDIR") { log.Info("ENV: updating FORGE_PATCHDIR from", f.Config.PatchDir, "to", os.Getenv("FORGE_PATCHDIRC")) f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR") changed = true } f.Config.ForgeURL = os.Getenv("FORGE_URL") f.hostname = os.Getenv("HOSTNAME") if os.Getenv("FORGE_GOWORK") == "true" { f.goWork = true } }) if changed { // save config here } return changed }