-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdinstaller.go
77 lines (67 loc) · 1.24 KB
/
wdinstaller.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
package wdinstaller
import (
"errors"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
)
type Option struct {
DestPath string
os string
arch string
filename string
driverVersion string
browserVersion string
baseURL string
driverURL string
}
func (opt *Option) init(filename string) error {
if opt.DestPath == "" {
return errors.New("")
}
if opt.os == "" {
opt.os = runtime.GOOS
}
if opt.arch == "" {
if strings.Contains(runtime.GOARCH, "64") {
opt.arch = "64"
} else {
opt.arch = "32"
}
}
if opt.os == "windows" {
opt.os = "win"
opt.filename = filename + ".exe"
} else {
opt.os = runtime.GOOS
opt.filename = filename
}
return nil
}
func downloadDriver(filepath string, url string) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, res.Body)
return err
}
func getVersionExec(command string, args ...string) string {
out, _ := exec.Command(command, args...).Output()
if len(out) != 0 {
r := regexp.MustCompile(`[\d*\.]+`)
return r.FindAllStringSubmatch(string(out), 1)[0][0]
} else {
return ""
}
}