probo/cmd/filter.go

115 lines
2.6 KiB
Go
Raw 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/glamour"
"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"
)
2024-05-13 12:00:47 +02:00
var longDescription string = `
# Filters
2024-03-25 15:53:20 +01:00
2024-05-13 12:00:47 +02:00
**Filters can made selection over stores.**
2024-03-25 15:53:20 +01:00
2024-05-13 12:00:47 +02:00
Filters allow you to narrow down selections across various stores. By
using filters, you can select participants, quizzes, and
responses. The command triggers a Text User Interface (TUI) that runs
a jq filter, displaying the outcome in real-time. After you're content
with the filtered JSON, pressing will present the result on
stdout, enabling you to further process it by piping it forward.
2024-03-25 15:53:20 +01:00
2024-05-13 12:00:47 +02:00
## Examples
2024-03-27 11:45:33 +01:00
2024-05-13 12:00:47 +02:00
Filter over participants store.
**probo filter participants**
Filter over quizzes store using the jq filter in tags.jq file
**probo filter quizzes -i data/filters/tags.jq**
Filter over participants and pipe the result on the next filter. The result is then stored in a JSON file.
**probo filter participants | probo filter quizzes > data/json/selection.json**
`
func init() {
desc, err := glamour.Render(fmt.Sprintf("```\n%s```\n%s", logo, longDescription), "dark")
2024-04-10 08:44:49 +02:00
if err != nil {
panic(err)
}
2024-05-13 12:00:47 +02:00
filterCmd := &cobra.Command{
Use: "filter {participants,quizzes,responses}",
Short: "Filter the given store",
Long: desc,
Run: runFilter,
}
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
}