-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
128 lines (108 loc) · 2.69 KB
/
source.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/jackc/pgx/v5"
"github.com/urfave/cli/v2"
)
var sourceCommand = &cli.Command{
Name: "source",
Usage: "Commands for managing data sources",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "List known sources",
Action: SourceList,
Flags: union([]cli.Flag{}, dbFlags, loggingFlags, hlogDefaultTrue),
},
{
Name: "add",
Usage: "Add a new source",
Action: SourceAdd,
Flags: union([]cli.Flag{
&cli.StringFlag{
Name: "name",
Required: true,
Usage: "Name of source.",
},
&cli.IntFlag{
Name: "provider-id",
Required: true,
Usage: "ID of provider.",
},
&cli.StringFlag{
Name: "dataset",
Required: false,
Usage: "Optional dataset within the provider for source.",
},
}, dbFlags, loggingFlags),
},
},
}
func SourceList(cc *cli.Context) error {
ctx := cc.Context
setupLogging()
db := NewDB(dbConnStr())
conn, err := db.NewConn(ctx)
if err != nil {
return fmt.Errorf("connect: %w", err)
}
rows, err := conn.Query(ctx, "select s.id, s.name, p.name, s.dataset from sources s join providers p on p.id=s.provider_id;")
if err != nil {
return fmt.Errorf("query: %w", err)
}
type SourceInfoRow struct {
ID int
Name string
ProviderName string
Dataset string
}
dss, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByPos[SourceInfoRow])
if err != nil {
return fmt.Errorf("collect: %w", err)
}
if len(dss) == 0 {
fmt.Println("No sources found")
return nil
}
w := tabwriter.NewWriter(os.Stdout, 1, 1, 4, ' ', 0)
fmt.Fprintln(w, "ID\t| Name\t| Provider\t| Dataset")
for _, ds := range dss {
fmt.Fprintf(w, "%d\t| %s\t| %s\t| %s\n", ds.ID, ds.Name, ds.ProviderName, ds.Dataset)
}
return w.Flush()
}
func SourceAdd(cc *cli.Context) error {
ctx := cc.Context
setupLogging()
name := strings.TrimSpace(cc.String("name"))
providerID := cc.Int("provider-id")
dataset := strings.TrimSpace(cc.String("dataset"))
if name == "" {
return fmt.Errorf("name must be supplied")
}
if providerID < 0 {
return fmt.Errorf("provider ID must be a positive integer")
}
db := NewDB(dbConnStr())
conn, err := db.NewConn(ctx)
if err != nil {
return fmt.Errorf("connect: %w", err)
}
tx, err := conn.Begin(ctx)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer tx.Rollback(ctx)
_, err = tx.Exec(ctx, "insert into sources(name,provider_id,dataset) values ($1,$2,$3)", name, providerID, dataset)
if err != nil {
return fmt.Errorf("exec (%T): %w", err, err)
}
err = tx.Commit(ctx)
if err != nil {
return fmt.Errorf("commit: %w", err)
}
return nil
}