summaryrefslogtreecommitdiff
path: root/internal/cli/cmd_query.go
blob: a22aeb418c9c3faeeb414445a46f4698c6659041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cli

import (
	"context"
	"flag"
	"fmt"
	"os"

	"codeberg.org/snonux/goprecords/internal/goprecords"
	"codeberg.org/snonux/goprecords/internal/storage"
)

func runQuery(args []string) error {
	fs := flag.NewFlagSet("query", flag.ExitOnError)
	dbPath := fs.String("db", "goprecords.db", "SQLite database path")
	rf := goprecords.RegisterReportFlags(fs)
	if err := fs.Parse(args); err != nil {
		return err
	}
	ctx := context.Background()
	db, err := storage.Open(ctx, *dbPath)
	if err != nil {
		return fmt.Errorf("open db: %w", err)
	}
	defer db.Close()
	aggregates, err := goprecords.LoadAggregates(ctx, db)
	if err != nil {
		return fmt.Errorf("load: %w", err)
	}
	cfg, err := rf.Parse()
	if err != nil {
		return err
	}
	return goprecords.WriteReports(os.Stdout, aggregates, cfg)
}