(hotfix) make sure app is initialized only when we need it
This commit is contained in:
@@ -65,6 +65,8 @@ func init() {
|
||||
issues.RootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
}
|
||||
|
||||
setupLazyInitialization()
|
||||
|
||||
RootCmd.AddGroup(&cobra.Group{ID: "issues", Title: "Issue Management"})
|
||||
issues.CreateCmd.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))
|
||||
}
|
||||
|
||||
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 {
|
||||
interfaces := make(map[string]task.Interface)
|
||||
for _, name := range task.ListInterfaces() {
|
||||
|
||||
@@ -5,24 +5,20 @@ import (
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/app"
|
||||
issues "github.com/LazyBachelor/LazyPM/internal/commands/issues"
|
||||
survey "github.com/LazyBachelor/LazyPM/internal/commands/survey"
|
||||
"github.com/charmbracelet/fang"
|
||||
)
|
||||
|
||||
var App *app.App
|
||||
var appCleanup func()
|
||||
var RootCmd = issues.RootCmd
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
app, cleanup, err := initializeServices(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
defer func() {
|
||||
if appCleanup != nil {
|
||||
appCleanup()
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
App = app
|
||||
survey.SetApp(App)
|
||||
issues.SetApp(App)
|
||||
}()
|
||||
|
||||
if err := fang.Execute(ctx, RootCmd,
|
||||
fang.WithColorSchemeFunc(fang.AnsiColorScheme)); err != nil {
|
||||
|
||||
@@ -15,6 +15,20 @@ import (
|
||||
|
||||
func runStartCmd(cmd *cobra.Command, args []string) error {
|
||||
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()
|
||||
surveyTasks := initTasks(app)
|
||||
|
||||
@@ -53,6 +67,14 @@ func taskLoop(ctx context.Context, surveyTasks map[string]task.Tasker, interface
|
||||
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) {
|
||||
iNames[i], iNames[j] = iNames[j], iNames[i]
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user