-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
98 lines (77 loc) · 2.04 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"time"
"github.com/go-errors/errors"
"github.com/itchio/zipserver/zipserver"
)
var _ fmt.Formatter
var (
configFname string
listenTo string
dumpConfig bool
serve string
extract string
)
func init() {
flag.StringVar(&configFname, "config", zipserver.DefaultConfigFname, "Path to json config file")
flag.StringVar(&listenTo, "listen", "127.0.0.1:8090", "Address to listen to")
flag.BoolVar(&dumpConfig, "dump", false, "Dump the parsed config and exit")
flag.StringVar(&serve, "serve", "", "Serve a given zip from a local HTTP server")
flag.StringVar(&extract, "extract", "", "Extract zip file to random name on GCS (requires a config with bucket)")
}
func must(err error) {
if err == nil {
return
}
if se, ok := err.(*errors.Error); ok {
log.Fatal(se.ErrorStack())
} else {
log.Fatal(err.Error())
}
}
func main() {
flag.Parse()
config, err := zipserver.LoadConfig(configFname)
must(err)
if dumpConfig {
fmt.Println(config)
return
}
if serve != "" {
must(zipserver.ServeZip(config, serve))
return
}
if extract != "" {
archiver := zipserver.NewArchiver(config)
limits := zipserver.DefaultExtractLimits(config)
log.Println("Extraction threads:", limits.ExtractionThreads)
log.Println("Bucket:", config.Bucket)
rand.Seed(time.Now().UTC().UnixNano())
var letters = []rune("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz")
var randChars = make([]rune, 20)
for i := range randChars {
randChars[i] = letters[rand.Intn(len(letters))]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.JobTimeout))
defer cancel()
files, err := archiver.UploadZipFromFile(ctx, extract, string(randChars), limits)
if err != nil {
log.Fatal(err.Error())
return
}
blob, _ := json.Marshal(struct {
Success bool
ExtractedFiles []zipserver.ExtractedFile
}{true, files})
fmt.Println(string(blob))
return
}
err = zipserver.StartZipServer(listenTo, config)
must(err)
}