// Code generated by summer make. DO NOT EDIT. package console import ( "context" "errors" "fmt" "os" "git.golem15.com/golem15/sm-quizzes-plugin/classes" "git.golem15.com/golem15/summercms/modules/bonfire" "gorm.io/gorm" ) // ImportCommand returns the quizzes:import console command. withDB runs the // command's work with the application's database; the plugin supplies it. func ImportCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error) bonfire.Command { return bonfire.Command{ Name: "quizzes:import", Description: "Import one quiz from a JSON file (see the plugin README for the format)", Args: []bonfire.Arg{{Name: "file", Description: "Path to the quiz JSON file", Required: true}}, Flags: []bonfire.Flag{ {Name: "replace", Description: "Replace the questions of an existing quiz with the same slug", Bare: true}, }, Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error { file, _ := in.Argument("file") if file == "" { return fmt.Errorf("quizzes:import: a file is required") } raw, err := os.ReadFile(file) if err != nil { return fmt.Errorf("quizzes:import: %w", err) } doc, err := classes.ParseImport(raw) if err != nil { return fmt.Errorf("quizzes:import: %s: %w", file, err) } replace, _ := in.Flag("replace") return withDB(ctx, func(db *gorm.DB) error { res, err := classes.Import(ctx, db, doc, replace == "true") if errors.Is(err, classes.ErrQuizExists) { return fmt.Errorf("quizzes:import: a quiz with the slug %q already exists; pass --replace to replace its questions", doc.Slug) } if err != nil { return fmt.Errorf("quizzes:import: %w", err) } verb := "replaced" if res.Created { verb = "created" } out.Printf("%s quiz %s (id %d): %d questions, %d answers\n", verb, res.Quiz.Slug, res.Quiz.ID, res.Questions, res.Answers) return nil }) }, } }