forked from cdnjs/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (126 loc) · 3.45 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
package main
import (
"flag"
"fmt"
"github.com/cdnjs/tools/kv"
"github.com/cdnjs/tools/sentry"
"github.com/cdnjs/tools/util"
)
var (
// initialize standard debug logger
logger = util.GetStandardLogger()
)
func init() {
sentry.Init()
}
// Returns true if zero or one of the booleans are true.
func isZeroOrOne(bs []bool) bool {
var found bool
for _, b := range bs {
if b {
if found {
return false
}
found = true
}
}
return true
}
func main() {
defer sentry.PanicHandler()
var metaOnly, srisOnly, filesOnly, count, noPush, panicOversized, ungzip, unbrotli bool
flag.BoolVar(&metaOnly, "meta-only", false, "If set, only version metadata is uploaded to KV (no files, no SRIs).")
flag.BoolVar(&srisOnly, "sris-only", false, "If set, only file SRIs are uploaded to KV (no files, no metadata).")
flag.BoolVar(&filesOnly, "files-only", false, "If set, only files are uploaded to KV (no metadata, no SRIs).")
flag.BoolVar(&count, "count", false, "If set, the the count of theoretical KV keys that should be in KV will be outputted. Will assume all entries can fit into KV (<= 10MiB).")
flag.BoolVar(&panicOversized, "panic-oversized", false, "If set, the program will panic if any KV compressed file is oversized (> 10MiB).")
flag.BoolVar(&noPush, "no-push", false, "If set, nothing will be written to KV. However, theoretical keys will be counted if the -count flag is set.")
flag.BoolVar(&ungzip, "ungzip", false, "If set, the file content will be decompressed with gzip.")
flag.BoolVar(&unbrotli, "unbrotli", false, "If set, the file content will be decompressed with brotli.")
flag.Parse()
if util.IsDebug() {
fmt.Println("Running in debug mode")
}
if !isZeroOrOne([]bool{metaOnly, srisOnly, filesOnly}) {
panic("can only set one of -meta-only, -sris-only, -files-only")
}
switch subcommand := flag.Arg(0); subcommand {
case "upload":
{
pckgs := flag.Args()[1:]
if len(pckgs) == 0 {
panic("no packages specified")
}
kv.InsertFromDisk(logger, pckgs, metaOnly, srisOnly, filesOnly, count, noPush, panicOversized)
}
case "upload-version":
{
args := flag.Args()[1:]
if len(args) != 2 {
panic("must specify package and version")
}
kv.InsertVersionFromDisk(logger, args[0], args[1], metaOnly, srisOnly, filesOnly, count, noPush, panicOversized)
}
case "upload-aggregate":
{
pckgs := flag.Args()[1:]
if len(pckgs) == 0 {
panic("no packages specified")
}
kv.InsertAggregateMetadataFromScratch(logger, pckgs)
}
case "aggregate-packages":
{
kv.OutputAllAggregatePackages()
}
case "packages":
{
kv.OutputAllPackages()
}
case "file":
{
if ungzip && unbrotli {
panic("can only set one of -ungzip, -unbrotli")
}
file := flag.Arg(1)
if file == "" {
panic("no file specified")
}
kv.OutputFile(logger, file, ungzip, unbrotli)
}
case "files":
{
pckg := flag.Arg(1)
if pckg == "" {
panic("no package specified")
}
kv.OutputAllFiles(logger, pckg)
}
case "meta":
{
pckg := flag.Arg(1)
if pckg == "" {
panic("no package specified")
}
kv.OutputAllMeta(logger, pckg)
}
case "aggregate":
{
pckg := flag.Arg(1)
if pckg == "" {
panic("no package specified")
}
kv.OutputAggregate(pckg)
}
case "sris":
{
prefix := flag.Arg(1)
if prefix == "" {
panic("no prefix specified") // avoid listing all SRIs
}
kv.OutputSRIs(prefix)
}
default:
panic(fmt.Sprintf("unknown subcommand: `%s`", subcommand))
}
}