This repository has been archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (115 loc) · 2.83 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
package main
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"github.com/anaminus/rbxark/filters"
"github.com/jessevdk/go-flags"
)
var Main, CancelMain = context.WithCancel(context.Background())
var FlagOptions struct {
Config string `short:"c" long:"config" description:"Path to configuration file. Defaults to the database file path appended with '.json'."`
}
var FlagParser = flags.NewParser(&FlagOptions, flags.Default)
func init() {
log.SetFlags(0)
}
// Gets a database path from a list of arguments and opens the database. Returns
// the database and the directory of the database.
func OpenDatabase(args []string) (db *sql.DB, dir string, err error) {
if len(args) == 0 {
return nil, "", fmt.Errorf("expected database file")
}
if db, err = sql.Open("sqlite3", args[0]); err != nil {
return nil, "", err
}
return db, args[0] + ".json", nil
}
func LoadConfig(path string) (config *Config, err error) {
if FlagOptions.Config != "" {
path = FlagOptions.Config
}
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open config: %w", err)
}
config = &Config{}
err = json.NewDecoder(f).Decode(config)
f.Close()
if err != nil {
if serr := (*json.SyntaxError)(nil); errors.As(err, &serr) {
return nil, fmt.Errorf("decode config: offset %d: %w", serr.Offset, serr)
}
return nil, fmt.Errorf("decode config: %w", err)
}
if config.ObjectsPath != "" && !filepath.IsAbs(config.ObjectsPath) {
// Path is relative to config file.
config.ObjectsPath = filepath.Join(filepath.Dir(path), config.ObjectsPath)
}
return config, nil
}
func LoadFilter(list []string, typ string) (query filters.Query, err error) {
filter := &filters.Filter{}
filter.AllowDomains(
"headers",
"content",
)
filter.AllowVars("headers",
"server",
"build",
"file",
)
filter.AllowVars("content",
"server",
"build",
"file",
)
for i, f := range list {
if err := filter.Append(f); err != nil {
return filters.Query{}, fmt.Errorf("load filters: filter[%d]: %w", i, err)
}
}
if query, err = filter.AsQuery(typ); err != nil {
return filters.Query{}, fmt.Errorf("load filters: %q: %w", typ, err)
}
return query, nil
}
func MonitorSignals(cancel context.CancelFunc) {
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
for {
select {
case <-sig:
cancel()
return
}
}
}()
}
type OptionTags map[string]*flags.Option
func (tags OptionTags) AddTo(cmd *flags.Command, err error) (*flags.Command, error) {
if cmd == nil {
return cmd, err
}
for name, info := range tags {
opt := cmd.FindOptionByLongName(name)
if opt == nil {
continue
}
opt.Description = info.Description
opt.ValueName = info.ValueName
opt.Default = info.Default
}
return cmd, err
}
func main() {
MonitorSignals(CancelMain)
FlagParser.Parse()
}