-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (71 loc) · 1.71 KB
/
main.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"context"
"os"
"github.com/m18/cpb/config"
"github.com/m18/cpb/db"
"github.com/m18/cpb/printer"
"github.com/m18/cpb/protos"
"github.com/m18/cpb/sys"
)
// TODO: add commands at root, e.g., config to print config
func main() {
cfg, err := config.New(os.Args[1:], os.DirFS)
sys.ExitIf(err)
args, pipe, err := querySources(cfg)
sys.ExitIf(err)
if !args && !pipe {
return
}
p, err := protos.New(cfg.Proto.C, cfg.Proto.Dir, cfg.Proto.Deterministic, os.DirFS, nil, false)
sys.ExitIf(err)
db, err := db.New(cfg.DB, p, cfg.InMessages, cfg.OutMessages, cfg.AutoMapOutMessages)
sys.ExitIf(err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sys.HandleInterrupt(ctx, cancel)
err = db.Ping(ctx)
sys.ExitIf(err, ctx.Err())
// TODO: config
pr, err := printer.New(
os.Stdout,
printer.WithFormat(printer.FormatTable),
printer.WithHeader(true),
printer.WithSpacing(1),
)
sys.ExitIf(err)
if args {
err = queryAndPrint(ctx, db, cfg.DB.Query, pr)
sys.ExitIf(err, ctx.Err())
}
if pipe {
err = queryFromPipe(ctx, db, pr)
sys.ExitIf(err, ctx.Err())
}
}
func querySources(cfg *config.Config) (args, pipe bool, err error) {
args = cfg.DB.Query != ""
pipe, err = sys.IsPipedIn()
return args, pipe, err
}
func queryFromPipe(ctx context.Context, db *db.DB, pr *printer.Printer) error {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
if err := queryAndPrint(ctx, db, s.Text(), pr); err != nil {
return err
}
}
return nil
}
func queryAndPrint(ctx context.Context, db *db.DB, q string, pr *printer.Printer) error {
if q == "" {
return nil
}
cols, rows, err := db.Query(ctx, q)
if err != nil {
return err
}
pr.Print(cols, rows)
return nil
}