filter.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright © 2024 NAME HERE <EMAIL ADDRESS>
  3. */
  4. package cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "git.andreafazzi.eu/andrea/probo/cmd/filter"
  9. "git.andreafazzi.eu/andrea/probo/cmd/util"
  10. tea "github.com/charmbracelet/bubbletea"
  11. "github.com/charmbracelet/lipgloss"
  12. "github.com/muesli/termenv"
  13. "github.com/spf13/cobra"
  14. )
  15. // filterCmd represents the filter command
  16. var filterCmd = &cobra.Command{
  17. Use: "filter",
  18. Short: "Create a new filter",
  19. Long: "Create a new filter for selecting quizzes and participants",
  20. Run: createFilter,
  21. }
  22. func init() {
  23. createCmd.AddCommand(filterCmd)
  24. filterCmd.Flags().StringP("type", "t", "participants", "Select the type of filter (participants or quizzes)")
  25. }
  26. func createFilter(cmd *cobra.Command, args []string) {
  27. f := util.LogToFile()
  28. if f != nil {
  29. defer f.Close()
  30. }
  31. path, err := cmd.Flags().GetString("input")
  32. if err != nil {
  33. panic(err)
  34. }
  35. filterType, err := cmd.Flags().GetString("type")
  36. if err != nil {
  37. panic(err)
  38. }
  39. lipgloss.SetColorProfile(termenv.TrueColor)
  40. model, err := tea.NewProgram(
  41. filter.New(path, filterType, util.ReadStdin()),
  42. tea.WithOutput(os.Stderr),
  43. ).Run()
  44. if err != nil {
  45. fmt.Println("Error running program:", err)
  46. os.Exit(1)
  47. }
  48. result := model.(*filter.FilterModel)
  49. if result.Result != "" {
  50. fmt.Fprintf(os.Stdout, result.Result)
  51. }
  52. }