package cli import ( "context" "fmt" "git.eeqj.de/sneak/vaultik/internal/config" "git.eeqj.de/sneak/vaultik/internal/database" "git.eeqj.de/sneak/vaultik/internal/globals" "go.uber.org/fx" ) // AppOptions contains common options for creating the fx application type AppOptions struct { ConfigPath string Modules []fx.Option Invokes []fx.Option } // NewApp creates a new fx application with common modules func NewApp(opts AppOptions) *fx.App { baseModules := []fx.Option{ fx.Supply(config.ConfigPath(opts.ConfigPath)), fx.Provide(globals.New), config.Module, database.Module, fx.NopLogger, } allOptions := append(baseModules, opts.Modules...) allOptions = append(allOptions, opts.Invokes...) return fx.New(allOptions...) } // RunApp starts and stops the fx application within the given context func RunApp(ctx context.Context, app *fx.App) error { if err := app.Start(ctx); err != nil { return fmt.Errorf("failed to start app: %w", err) } defer func() { if err := app.Stop(ctx); err != nil { fmt.Printf("error stopping app: %v\n", err) } }() // Wait for context cancellation <-ctx.Done() return nil } // RunWithApp is a helper that creates and runs an fx app with the given options func RunWithApp(ctx context.Context, opts AppOptions) error { app := NewApp(opts) return RunApp(ctx, app) }