(hotfix) make sure app is initialized only when we need it

This commit is contained in:
Robin Olsen
2026-03-02 11:41:57 +01:00
parent 1baf6c7ff4
commit cb10aeff6b
3 changed files with 95 additions and 11 deletions

View File

@@ -65,6 +65,8 @@ func init() {
issues.RootCmd.CompletionOptions.DisableDefaultCmd = true issues.RootCmd.CompletionOptions.DisableDefaultCmd = true
} }
setupLazyInitialization()
RootCmd.AddGroup(&cobra.Group{ID: "issues", Title: "Issue Management"}) RootCmd.AddGroup(&cobra.Group{ID: "issues", Title: "Issue Management"})
issues.CreateCmd.GroupID = "issues" issues.CreateCmd.GroupID = "issues"
issues.GetCmd.GroupID = "issues" issues.GetCmd.GroupID = "issues"
@@ -115,10 +117,74 @@ var replCmd = &cobra.Command{
}, },
} }
func initializeServices(ctx context.Context) (*app.App, func(), error) { func initializeApp(ctx context.Context) (*app.App, func(), error) {
return app.New(ctx, tasks.BaseConfig().WithAutoInit(true)) return app.New(ctx, tasks.BaseConfig().WithAutoInit(true))
} }
func setupLazyInitialization() {
prevPreRun := RootCmd.PersistentPreRun
prevPreRunE := RootCmd.PersistentPreRunE
RootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if commandNeedsApp(cmd) {
if err := ensureAppInitialized(cmd.Context()); err != nil {
return err
}
}
if prevPreRunE != nil {
if err := prevPreRunE(cmd, args); err != nil {
return err
}
}
if prevPreRun != nil {
prevPreRun(cmd, args)
}
return nil
}
}
func commandNeedsApp(cmd *cobra.Command) bool {
name := cmd.Name()
switch name {
case "help", "completion", "__complete", "__completeNoDesc":
return false
}
if cmd == RootCmd || name == "survey" {
return false
}
if parent := cmd.Parent(); parent != nil && parent.Name() == "survey" {
switch name {
case "tasks", "interfaces":
return false
}
}
return true
}
func ensureAppInitialized(ctx context.Context) error {
if App != nil {
return nil
}
application, cleanup, err := initializeApp(ctx)
if err != nil {
return err
}
App = application
appCleanup = cleanup
survey.SetApp(App)
issues.SetApp(App)
return nil
}
func initInterfaces() map[string]task.Interface { func initInterfaces() map[string]task.Interface {
interfaces := make(map[string]task.Interface) interfaces := make(map[string]task.Interface)
for _, name := range task.ListInterfaces() { for _, name := range task.ListInterfaces() {

View File

@@ -5,24 +5,20 @@ import (
"github.com/LazyBachelor/LazyPM/internal/app" "github.com/LazyBachelor/LazyPM/internal/app"
issues "github.com/LazyBachelor/LazyPM/internal/commands/issues" issues "github.com/LazyBachelor/LazyPM/internal/commands/issues"
survey "github.com/LazyBachelor/LazyPM/internal/commands/survey"
"github.com/charmbracelet/fang" "github.com/charmbracelet/fang"
) )
var App *app.App var App *app.App
var appCleanup func()
var RootCmd = issues.RootCmd var RootCmd = issues.RootCmd
func main() { func main() {
ctx := context.Background() ctx := context.Background()
app, cleanup, err := initializeServices(ctx) defer func() {
if err != nil { if appCleanup != nil {
return appCleanup()
} }
defer cleanup() }()
App = app
survey.SetApp(App)
issues.SetApp(App)
if err := fang.Execute(ctx, RootCmd, if err := fang.Execute(ctx, RootCmd,
fang.WithColorSchemeFunc(fang.AnsiColorScheme)); err != nil { fang.WithColorSchemeFunc(fang.AnsiColorScheme)); err != nil {

View File

@@ -15,6 +15,20 @@ import (
func runStartCmd(cmd *cobra.Command, args []string) error { func runStartCmd(cmd *cobra.Command, args []string) error {
app := survey.AppFromContext(cmd.Context()) app := survey.AppFromContext(cmd.Context())
if app == nil {
if err := ensureAppInitialized(cmd.Context()); err != nil {
return fmt.Errorf("failed to initialize services: %w", err)
}
app = survey.AppFromContext(cmd.Context())
if app == nil {
app = App
}
}
if app == nil {
return fmt.Errorf("application services are not available")
}
interfaces := initInterfaces() interfaces := initInterfaces()
surveyTasks := initTasks(app) surveyTasks := initTasks(app)
@@ -53,6 +67,14 @@ func taskLoop(ctx context.Context, surveyTasks map[string]task.Tasker, interface
iNames = append(iNames, name) iNames = append(iNames, name)
} }
if len(iNames) == 0 {
return fmt.Errorf("no interfaces are available")
}
if len(surveyTasks) == 0 {
return fmt.Errorf("no tasks are available")
}
rand.Shuffle(len(iNames), func(i, j int) { rand.Shuffle(len(iNames), func(i, j int) {
iNames[i], iNames[j] = iNames[j], iNames[i] iNames[i], iNames[j] = iNames[j], iNames[i]
}) })