probo/cmd/filter.go

62 lines
1.2 KiB
Go
Raw Normal View History

2024-03-25 15:53:20 +01:00
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"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-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-06-05 12:16:08 +02:00
f := util.LogToFile()
if f != nil {
defer f.Close()
}
2024-05-13 12:00:47 +02:00
2024-06-05 12:16:08 +02:00
lipgloss.SetColorProfile(termenv.TrueColor)
2024-05-13 12:00:47 +02:00
2024-06-05 12:16:08 +02:00
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-03-25 15:53:20 +01:00
}