filter.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright © 2024 NAME HERE <EMAIL ADDRESS>
  3. */
  4. package cmd
  5. import (
  6. "fmt"
  7. "log"
  8. "os"
  9. "git.andreafazzi.eu/andrea/probo/cmd/filter"
  10. "git.andreafazzi.eu/andrea/probo/cmd/util"
  11. tea "github.com/charmbracelet/bubbletea"
  12. "github.com/charmbracelet/huh"
  13. "github.com/charmbracelet/lipgloss"
  14. "github.com/muesli/termenv"
  15. "github.com/spf13/cobra"
  16. )
  17. var filterCmd = &cobra.Command{
  18. Use: "filter {participants,quizzes,responses}",
  19. Short: "Filter the given store",
  20. Long: util.RenderMarkdownTemplates("cli/*.tmpl", "cli/filter/*.tmpl"),
  21. Run: runFilter,
  22. }
  23. func init() {
  24. rootCmd.AddCommand(filterCmd)
  25. filterCmd.PersistentFlags().StringP("input", "i", "", "Specify an input file")
  26. }
  27. func runFilter(cmd *cobra.Command, args []string) {
  28. var storeType string
  29. path, err := cmd.Flags().GetString("input")
  30. if err != nil {
  31. panic(err)
  32. }
  33. if len(args) < 1 {
  34. f := util.LogToFile()
  35. if f != nil {
  36. defer f.Close()
  37. }
  38. lipgloss.SetColorProfile(termenv.TrueColor)
  39. form := huh.NewForm(huh.NewGroup(
  40. huh.NewSelect[string]().
  41. Title("Choose the store you want to filter").
  42. Options(
  43. huh.NewOption("Participants", "participants"),
  44. huh.NewOption("Quizzes", "quizzes"),
  45. huh.NewOption("Responses", "responses"),
  46. ).Value(&storeType),
  47. ))
  48. err = form.Run()
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. } else {
  53. storeType = args[0]
  54. }
  55. model, err := tea.NewProgram(
  56. filter.New(path, storeType, util.ReadStdin()),
  57. tea.WithOutput(os.Stderr),
  58. ).Run()
  59. if err != nil {
  60. fmt.Println("Error running program:", err)
  61. os.Exit(1)
  62. }
  63. result := model.(*filter.FilterModel)
  64. if result.Result != "" {
  65. fmt.Fprintf(os.Stdout, result.Result)
  66. }
  67. }