-
Notifications
You must be signed in to change notification settings - Fork 6
/
args.go
51 lines (40 loc) · 858 Bytes
/
args.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
package main
import (
"fmt"
"github.com/alexflint/go-arg"
"os"
)
var ErrNotNetflixUrl = fmt.Errorf("VideoURL must be a netflix url")
var ErrDownloadDirInvalid = fmt.Errorf("DownloadDir is invalid")
func mustParse(a *Args) {
arg.MustParse(a)
argParser := arg.MustParse(a)
if err := validateArgs(a); err != nil {
argParser.Fail(err.Error())
}
}
func validateArgs(a *Args) error {
if !isNetflixUrl(a) {
return ErrNotNetflixUrl
}
if err := processDownloadDir(a); err != nil {
return ErrDownloadDirInvalid
}
return nil
}
func processDownloadDir(a *Args) error {
var path = a.DownloadDir
var err error
if path != "." {
a.DownloadDir = path
return nil
}
if cwd, err := os.Getwd(); err == nil {
a.DownloadDir = cwd
return nil
}
return err
}
func isNetflixUrl(a *Args) bool {
return a.VideoURL.Host == "www.netflix.com"
}