probo/cmd/filter.go

82 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-03-25 15:53:20 +01:00
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
2024-05-13 12:00:47 +02:00
"log"
2024-03-25 15:53:20 +01:00
"os"
"git.andreafazzi.eu/andrea/probo/cmd/filter"
2024-04-22 14:24:29 +02:00
"git.andreafazzi.eu/andrea/probo/cmd/util"
2024-03-25 15:53:20 +01:00
tea "github.com/charmbracelet/bubbletea"
2024-05-13 12:00:47 +02:00
"github.com/charmbracelet/huh"
2024-04-10 08:44:49 +02:00
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
2024-03-25 15:53:20 +01:00
"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,
}
2024-05-13 12:00:47 +02:00
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")
2024-04-10 08:44:49 +02:00
if err != nil {
panic(err)
}
2024-05-13 12:00:47 +02:00
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]
}
2024-04-22 14:24:29 +02:00
2024-04-10 08:44:49 +02:00
model, err := tea.NewProgram(
2024-05-13 12:00:47 +02:00
filter.New(path, storeType, util.ReadStdin()),
2024-04-10 08:44:49 +02:00
tea.WithOutput(os.Stderr),
).Run()
if err != nil {
2024-03-25 15:53:20 +01:00
fmt.Println("Error running program:", err)
os.Exit(1)
}
2024-05-13 12:00:47 +02:00
2024-04-10 08:44:49 +02:00
result := model.(*filter.FilterModel)
2024-05-13 12:00:47 +02:00
2024-04-12 09:52:11 +02:00
if result.Result != "" {
fmt.Fprintf(os.Stdout, result.Result)
2024-04-10 08:44:49 +02:00
}
2024-05-13 12:00:47 +02:00
2024-03-25 15:53:20 +01:00
}