/* Copyright © 2024 NAME HERE */ package cmd import ( "fmt" "log" "os" "git.andreafazzi.eu/andrea/probo/cmd/filter" "git.andreafazzi.eu/andrea/probo/cmd/util" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/huh" "github.com/charmbracelet/lipgloss" "github.com/muesli/termenv" "github.com/spf13/cobra" ) var filterCmd = &cobra.Command{ Use: "filter {participants,quizzes,responses}", Short: "Filter the given store", Long: util.RenderMarkdownTemplates("cli/*.tmpl", "cli/filter/*.tmpl"), Run: runFilter, } func init() { rootCmd.AddCommand(filterCmd) filterCmd.PersistentFlags().StringP("input", "i", "", "Specify an input file") } func runFilter(cmd *cobra.Command, args []string) { var storeType string path, err := cmd.Flags().GetString("input") if err != nil { panic(err) } if len(args) < 1 { f := util.LogToFile() if f != nil { defer f.Close() } lipgloss.SetColorProfile(termenv.TrueColor) form := huh.NewForm(huh.NewGroup( huh.NewSelect[string](). Title("Choose the store you want to filter"). Options( huh.NewOption("Participants", "participants"), huh.NewOption("Quizzes", "quizzes"), huh.NewOption("Responses", "responses"), ).Value(&storeType), )) err = form.Run() if err != nil { log.Fatal(err) } } else { storeType = args[0] } model, err := tea.NewProgram( filter.New(path, storeType, util.ReadStdin()), tea.WithOutput(os.Stderr), ).Run() if err != nil { fmt.Println("Error running program:", err) os.Exit(1) } result := model.(*filter.FilterModel) if result.Result != "" { fmt.Fprintf(os.Stdout, result.Result) } }