-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (164 loc) · 4.96 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Command gh-sql is the main entrypoint for the gh-sql tool.
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gnolang/gh-sql/ent"
"github.com/gnolang/gh-sql/ent/migrate"
"github.com/gnolang/gh-sql/pkg/model"
"github.com/gnolang/gh-sql/pkg/rest"
"github.com/gnolang/gh-sql/pkg/sync"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
entsql "entgo.io/ent/dialect/sql"
_ "modernc.org/sqlite"
)
func main() {
if err := run(); err != nil {
if !errors.Is(err, ff.ErrHelp) {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
os.Exit(1)
}
}
func run() error {
// Parse configuration
var (
globalFS = ff.NewFlagSet("gh-sql")
entDebug = globalFS.BoolLong("debug.ent", "use ent's debug client (very verbose)")
_ = globalFS.StringLong("config", "", "config file (optional)")
ctx = &execContext{}
)
cmd := &ff.Command{
Name: "gh-sql",
Usage: "gh-sql SUBCOMMAND ...",
ShortHelp: "a tool to locally store information about GitHub repository, query them and serve them.",
LongHelp: "", // TODO
Flags: globalFS,
Subcommands: []*ff.Command{
newSyncCmd(globalFS, ctx),
newServeCmd(globalFS, ctx),
// gh-sql query # simple readonly query interface/CLI to get info from the database, json/csv/msgpack output.
},
Exec: func(context.Context, []string) error { return ff.ErrHelp },
}
err := cmd.Parse(os.Args[1:],
ff.WithEnvVarPrefix("GHSQL_"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
)
if err != nil {
fmt.Fprintln(os.Stderr, ffhelp.Command(cmd))
return err
}
abs, err := filepath.Abs("./database.sqlite")
if err != nil {
return err
}
ctx.sqlDB, err = sql.Open("sqlite", fmt.Sprintf("file://%s?_pragma=foreign_keys(1)&_txlock=exclusive", abs))
if err != nil {
return fmt.Errorf("failed opening connection to postgres: %v", err)
}
defer ctx.sqlDB.Close()
entDrv := entsql.OpenDB("sqlite3", ctx.sqlDB)
ctx.db = ent.NewClient(ent.Driver(entDrv))
if *entDebug {
ctx.db = ctx.db.Debug()
}
// Run the auto migration tool.
if err := ctx.db.Schema.Create(context.Background(),
// Allows ent to drop indexes if we remove constraints from the schema.
migrate.WithDropIndex(true)); err != nil {
return fmt.Errorf("failed creating schema resources: %v", err)
}
err = cmd.Run(context.Background())
if errors.Is(err, ff.ErrHelp) {
fmt.Fprintln(os.Stderr, ffhelp.Command(cmd))
return err
}
return nil
}
type execContext struct {
sqlDB *sql.DB
db *ent.Client
}
func newSyncCmd(fs *ff.FlagSet, ec *execContext) *ff.Command {
var (
fset = ff.NewFlagSet("gh-sql sync").SetParent(fs)
full = fset.BoolLong("full", "sync repositories from scratch (non-incremental), automatic if more than >10_000 events to retrieve")
token = fset.StringLong("token", "", "github personal access token to use (heavily suggested)")
debugHTTP = fset.BoolLong("debug.http", "log http requests")
debugJSONCatcher = fset.StringLong(
"debug.jsoncatcher",
filepath.Join(os.TempDir(), "jsoncatcher.log"),
"use jsoncatcher to find incorrectly-parsed events. (enabled by default)")
)
return &ff.Command{
Name: "sync",
Usage: "gh-sql sync [FLAGS...] REPOS...",
ShortHelp: "synchronize the current state with GitHub",
LongHelp: "Repositories must be specified with the syntax <owner>/<repo>.",
Flags: fset,
Exec: func(ctx context.Context, args []string) error {
// sync has mostly writes to the database, and modernc.org/sqlite
// does not support concurrent writes. Set MaxOpenConns=1 to avoid errors.
// TODO: when adding support for multiple databases, ensure to drop
// this line for all other DBs
ec.sqlDB.SetMaxOpenConns(1)
if *debugJSONCatcher != "" {
model.EventsMissedData = &onDemandFile{fileName: *debugJSONCatcher}
}
return sync.Sync(ctx, args, sync.Options{
DB: ec.db,
Full: *full,
Token: *token,
DebugHTTP: *debugHTTP,
})
},
}
}
type onDemandFile struct {
*os.File
err error
fileName string
}
func (w *onDemandFile) Write(p []byte) (int, error) {
if w.err != nil {
return 0, w.err
}
if w.File == nil {
w.File, w.err = os.OpenFile(w.fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if w.err != nil {
return 0, w.err
}
}
return w.File.Write(p)
}
func newServeCmd(fs *ff.FlagSet, ec *execContext) *ff.Command {
var (
fset = ff.NewFlagSet("gh-sql serve").SetParent(fs)
addr = fset.StringLong("addr", ":31415", "listening address for web server")
)
return &ff.Command{
Name: "serve",
Usage: "gh-sql serve [--addr :31345] [FLAGS...]",
ShortHelp: "listen for requests on the given address, mimicking the GitHub REST API",
Flags: fset,
Exec: func(ctx context.Context, args []string) error {
if len(args) > 0 {
return ff.ErrHelp
}
srv := rest.Handler(rest.Options{
DB: ec.db,
})
log.Printf("Listening on %s", *addr)
return http.ListenAndServe(*addr, srv)
},
}
}