-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (167 loc) · 4.13 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
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"sync"
"gopkg.in/yaml.v2"
"github.com/mholt/archiver"
"github.com/sbaildon/wow-addon-downloader/providers"
_ "github.com/sbaildon/wow-addon-downloader/providers/curseforge"
_ "github.com/sbaildon/wow-addon-downloader/providers/wowinterface"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
type yamlurl struct {
*url.URL
}
func (j *yamlurl) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
url, err := url.Parse(s)
j.URL = url
return err
}
const (
downloadError = 100
mkdirError = 101
saveError = 102
unzipError = 103
missingError = 104
)
func errorBar(bar *mpb.Bar, errorCode int64) {
bar.SetTotal(errorCode, true)
bar.IncrBy(int(bar.Total() - bar.Current()))
}
func download(provider providers.Provider, u url.URL, config config, bar *mpb.Bar, wg *sync.WaitGroup) {
defer wg.Done()
_, err := provider.GetName(u)
if err != nil {
errorBar(bar, missingError)
return
}
_, err = provider.GetVersion(u)
if err != nil {
errorBar(bar, missingError)
return
}
bar.Increment()
downloadURL, err := provider.DownloadURL(u)
if err != nil {
errorBar(bar, downloadError)
return
}
resp, err := http.Get(downloadURL)
if err != nil {
errorBar(bar, downloadError)
return
}
defer resp.Body.Close()
bar.Increment()
if resp.StatusCode != http.StatusOK {
errorBar(bar, downloadError)
return
}
/* Create a temporary directory for saving files */
tempFile, err := ioutil.TempFile("", "addon")
if err != nil {
errorBar(bar, saveError)
return
}
defer os.Remove(tempFile.Name())
io.Copy(tempFile, resp.Body)
bar.Increment()
/* Unzip files */
err = archiver.Zip.Open(tempFile.Name(), config.System.AddonDir)
if err != nil {
errorBar(bar, saveError)
return
}
bar.Increment()
}
type config struct {
System struct {
AddonDir string `yaml:"addon_dir"`
CloseOnFinish bool `yaml:"close_on_finish"`
} `yaml:"system"`
AddOns []yamlurl `yaml:"addons"`
}
var (
configFile = flag.String("config-file", "./config.yml", "Configuration file")
)
func main() {
flag.Parse()
configSource, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatal("Problem reading config file")
}
var config config
err = yaml.Unmarshal(configSource, &config)
if err != nil {
log.Fatal("Can't understand config file. Is it malformed?")
}
var wg sync.WaitGroup
pool := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithWidth(10))
var steps = []string{"searching", "downloading", "saving", "unzipping", "done"}
for _, url := range config.AddOns {
provider, err := providers.GetProvider(url.URL.Hostname())
if err != nil {
bar := pool.AddBar(1,
mpb.BarClearOnComplete(),
mpb.PrependDecorators(
decor.StaticName(fmt.Sprintf("%s:", url.String()), 0, decor.DSyncSpace+decor.DidentRight),
decor.StaticName(fmt.Sprint("unsupported"), 16, 1),
),
)
bar.Increment()
continue
}
bar := pool.AddBar(int64(len(steps)-1),
mpb.BarClearOnComplete(),
mpb.AppendDecorators(
decor.OnComplete(decor.Percentage(5, 0), "", 0, decor.DwidthSync),
),
mpb.PrependDecorators(
decor.StaticName(fmt.Sprintf("%s:", url.String()), 0, decor.DSyncSpace+decor.DidentRight),
decor.DynamicName(func(s *decor.Statistics) string {
switch s.Total {
case downloadError:
return "failed to download"
case mkdirError:
return "failed to create addon directory"
case saveError:
return "unable to save addon"
case unzipError:
return "unable to unzip addon"
case missingError:
return "unable to locate"
default:
return fmt.Sprintf("%s", steps[s.Current])
}
}, 16, 1),
decor.OnComplete(
decor.Elapsed(3, decor.DSyncSpace), "", 0, decor.DwidthSync,
),
),
)
wg.Add(1)
go download(provider, *url.URL, config, bar, &wg)
}
pool.Wait()
if !config.System.CloseOnFinish {
var signalChannel chan os.Signal
signalChannel = make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)
fmt.Print("Finished")
<-signalChannel
}
}