use new interactive db connection in runner

This commit is contained in:
Robin Olsen
2026-03-10 12:08:42 +01:00
parent 5c6afc9b69
commit 053f126e19

View File

@@ -31,27 +31,36 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("application services are not available") return fmt.Errorf("application services are not available")
} }
if len(args) == 0 { if !cmd.Flags().Changed("dev") {
var noSubmit bool var mongoStorage *storage.MongoStorage
cmd.Println("No MongoDB password provided, survey responses will not be submitted.") var continueWithoutSubmitting bool
cmd.Println("you can manually submit later with `pm survey submit <mongo-password>`.")
huh.NewConfirm().Title("Are you sure you want to continue without submiting data?"). for {
Value(&noSubmit).RunAccessible(cmd.OutOrStdout(), cmd.InOrStdin()) db, err := storage.NewMongoStorageInteractive(app.Config.MongoURI)
if err == nil {
if !noSubmit { mongoStorage = db
cmd.Println("Exiting. Please run the command again with a MongoDB password to submit your data.") break
return nil
} }
} else {
mongoPass := args[0] cmd.Println("Failed to connect to database, survey responses will not be submitted.")
mongoStorage, err := storage.NewMongoStorage(app.Config.MongoURI, "participant", mongoPass)
if err != nil { if err := huh.NewConfirm().
cmd.Println("Failed to connect to MongoDB, survey responses will not be submitted.") Title("Do you want to continue without submitting your responses?").
cmd.Println("You can manually submit later with `pm survey submit <password>`.") Description("You can fix your database connection and submit your responses later with the submit command.").
return nil Value(&continueWithoutSubmitting).
} else { WithTheme(huh.ThemeBase16()).
cmd.Println("Connected to MongoDB successfully.") RunAccessible(cmd.OutOrStdout(), cmd.InOrStdin()); err != nil {
return fmt.Errorf("failed to read user input: %w", err)
}
if continueWithoutSubmitting {
break
}
}
if mongoStorage != nil {
cmd.Println("Connected to Database Successfully. Starting survey...")
time.Sleep(2 * time.Second)
defer mongoStorage.Close() defer mongoStorage.Close()
@@ -76,6 +85,9 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
if err := mongoStorage.SubmitSurveyResponsesCmd(ctx); err != nil { if err := mongoStorage.SubmitSurveyResponsesCmd(ctx); err != nil {
cmd.Printf("Failed to submit survey responses: %v\n", err) cmd.Printf("Failed to submit survey responses: %v\n", err)
} }
} else {
cmd.Println("Starting survey without database connection. Your responses will not be submitted...")
time.Sleep(2 * time.Second)
} }
} }
@@ -100,11 +112,11 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
survey.Task: surveyTasks[survey.Task], survey.Task: surveyTasks[survey.Task],
} }
} }
if !cmd.Flag("dev").Changed {
if err := newIntroModel().Run(); err != nil { if err := newIntroModel().Run(); err != nil {
return returnIfUserQuit(err, "failed to run intro") return returnIfUserQuit(err, "failed to run intro")
} }
}
if err := taskLoop(cmd.Context(), app, surveyTasks, interfaces); err != nil { if err := taskLoop(cmd.Context(), app, surveyTasks, interfaces); err != nil {
return returnIfUserQuit(err, "task loop failed") return returnIfUserQuit(err, "task loop failed")
} }