d715b053e7
Enables request-scoped cancellation, timeouts, and graceful shutdown for all database operations across API handlers, CLI commands, and TUI.
79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package cmd
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"github.com/lerko/nib/internal/db"
|
||
"github.com/lerko/nib/internal/display"
|
||
"github.com/spf13/cobra"
|
||
)
|
||
|
||
var (
|
||
cardsTag string
|
||
cardsType string
|
||
)
|
||
|
||
var cardsCmd = &cobra.Command{
|
||
Use: "cards",
|
||
Short: "list crystallized cards by usage",
|
||
RunE: runCards,
|
||
}
|
||
|
||
func init() {
|
||
cardsCmd.Flags().StringVar(&cardsTag, "tag", "", "filter by tag")
|
||
cardsCmd.Flags().StringVar(&cardsType, "type", "", "filter by card type")
|
||
rootCmd.AddCommand(cardsCmd)
|
||
}
|
||
|
||
func runCards(cmd *cobra.Command, _ []string) error {
|
||
store, err := openStore()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer store.Close()
|
||
|
||
p := db.DefaultListParams()
|
||
p.CardsOnly = true
|
||
p.Sort = "use_count"
|
||
p.Order = "desc"
|
||
|
||
if cardsTag != "" {
|
||
p.Tag = &cardsTag
|
||
}
|
||
if cardsType != "" {
|
||
if !db.ValidCardType(cardsType) {
|
||
return fmt.Errorf("invalid_type — %q is not a valid card type", cardsType)
|
||
}
|
||
ct := db.CardType(cardsType)
|
||
p.CardTypeFilter = &ct
|
||
}
|
||
|
||
entities, err := store.List(cmd.Context(), p)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
for _, e := range entities {
|
||
glyph := display.DisplayGlyph(e.Glyph, e.CardType)
|
||
shortID := display.FormatID(e.ID)
|
||
|
||
var tagStr string
|
||
for _, tag := range e.Tags {
|
||
tagStr += " #" + tag
|
||
}
|
||
|
||
label := e.Body
|
||
if e.Title != nil {
|
||
label = *e.Title
|
||
}
|
||
|
||
fmt.Printf("%s %-40s %-16s %3d× %s\n",
|
||
glyph, label,
|
||
strings.TrimSpace(tagStr),
|
||
e.UseCount, shortID)
|
||
}
|
||
|
||
return nil
|
||
}
|