probo/cmd/filter.go
2024-06-05 12:16:08 +02:00

61 lines
1.2 KiB
Go

/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"git.andreafazzi.eu/andrea/probo/cmd/filter"
"git.andreafazzi.eu/andrea/probo/cmd/util"
tea "github.com/charmbracelet/bubbletea"
"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)
}
f := util.LogToFile()
if f != nil {
defer f.Close()
}
lipgloss.SetColorProfile(termenv.TrueColor)
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)
}
}