-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
111 lines (105 loc) · 2.61 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
package main
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/fumiama/terasu/dns"
"github.com/fumiama/terasu/ip"
ui "github.com/gizak/termui/v3"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
_ "rvcmd/console"
)
//go:generate ./pckcfg.sh assets
var (
notui = false
sc screen
)
func main() {
ntrs := flag.Bool("notrs", false, "use standard TLS client")
dnsf := flag.String("dns", "", "custom dns.yaml")
cust := flag.Bool("c", false, "use custom yaml instruction")
force := flag.Bool("f", false, "force download even file exists")
wait := flag.Uint("w", 4, "connection waiting seconds")
ua := flag.String("ua", defua, "customize user agent")
h := flag.Bool("h", false, "display this help")
flag.BoolVar(¬ui, "notui", false, "use plain text instead of TUI")
flag.Parse()
args := flag.Args()
if len(args) != 1 || *h {
fmt.Println("Usage:", os.Args[0], "[-notrs] [-dns dns.yaml] 'target/to/download'")
flag.PrintDefaults()
fmt.Println(" 'target/to/download'\n like packs/general/latest")
fmt.Println("All available targets:")
fmt.Println(cmdlst.String())
return
}
if notui {
logrus.Infoln("RVC Models Downloader start at", time.Now().Local().Format(time.DateTime+" (MST)"))
logrus.Infof("operating system: %s, architecture: %s", runtime.GOOS, runtime.GOARCH)
logrus.Infoln("is ipv6 available:", ip.IsIPv6Available.Get())
} else {
if err := ui.Init(); err != nil {
logrus.Errorln("failed to initialize termui:", err)
return
}
defer ui.Close()
sc = newscreen()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if *dnsf != "" {
f, err := os.Open(*dnsf)
if err != nil {
errorln("open custom dns file", *dnsf, "err:", err)
return
}
m := dns.DNSConfig{}
err = yaml.NewDecoder(f).Decode(&m)
if err != nil {
errorln("decode custom dns file", *dnsf, "err:", err)
return
}
_ = f.Close()
if ip.IsIPv6Available.Get() {
dns.IPv6Servers.Add(&m)
} else {
dns.IPv4Servers.Add(&m)
}
infoln("custom dns file added")
}
usercfg, err := readconfig(args[0], *cust)
if err != nil {
errorln(err)
return
}
ch := make(chan struct{})
go func() {
err := usercfg.download(args[0], "", *ua, time.Second*time.Duration(*wait), *cust, !*ntrs, *force)
ch <- struct{}{}
if err != nil {
errorln(err)
return
}
}()
select {
case <-ch:
infoln("all download tasks finished.")
case <-ctx.Done():
logrus.Warnln("download canceled")
}
}()
if notui {
wg.Wait()
} else {
sc.show(time.Second)
}
}