-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
78 lines (62 loc) · 1.33 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
package main
import (
"flag"
"fmt"
"os"
"reflect"
"github.com/gopherdojo/dojo6/kadai3-2/pei/pkg/download"
)
const (
exitCodeOk = 0
exitCodeError = 1
splitNum = 4
)
type cliArgs struct {
url, outputPath string
}
func (ca *cliArgs) validate() error {
if ca.url == "" {
return fmt.Errorf("No URL")
}
if ca.outputPath == "" {
ca.outputPath = "./"
}
return nil
}
func main() {
os.Exit(Run())
}
// Run runs download
func Run() int {
ca := parseArgs()
if err := ca.validate(); err != nil {
fmt.Fprintln(os.Stderr, "Args error: ", err)
return exitCodeError
}
downloader, err := download.NewDownloader(splitNum, ca.url, ca.outputPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Create downloader error: ", err)
return exitCodeError
}
outputPath, err := downloader.Do()
if err != nil {
fmt.Fprintln(os.Stderr, "Download error: ", err)
return exitCodeError
}
var downloadType string
if reflect.TypeOf(downloader) == reflect.TypeOf(&download.RangeDownloader{}) {
downloadType = "Split Download"
} else {
downloadType = "Download"
}
fmt.Println("Download Type: ", downloadType)
fmt.Println("Download completed. Output: ", outputPath)
return exitCodeOk
}
func parseArgs() *cliArgs {
var ca cliArgs
flag.StringVar(&ca.outputPath, "o", "", "output path")
flag.Parse()
ca.url = flag.Arg(0)
return &ca
}