-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
111 lines (92 loc) · 2.18 KB
/
setup.go
File metadata and controls
111 lines (92 loc) · 2.18 KB
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 bedis
import (
"bufio"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
)
const (
_repo = "https://github.com/wrfly/redis-server.tgz"
_linkLinux = _repo + "/raw/master/7.4.2/redis-server-linux-libc-x86_64.tgz"
_linkDarwin = _repo + "/raw/master/7.4.2/redis-server-darwin-libc-arm64.tgz"
)
var (
root = filepath.Join(os.TempDir(), "builtin-redis")
binPath = filepath.Join(root, "redis-server")
)
func _makeDir(path string) error {
f, err := os.Open(path)
if err == nil {
f.Close()
return nil
}
if os.IsNotExist(err) {
err := os.Mkdir(path, 0755)
if err != nil {
return fmt.Errorf("cannot make temp dir `%s`, err: %s", path, err)
}
return nil
}
return fmt.Errorf("cannot open temp dir `%s`, err: %s", path, err)
}
func _smokeTest() error {
cmd := exec.Command(binPath, "--version")
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
// read command's stdout line by line
in := bufio.NewScanner(stdout)
for in.Scan() {
logs.Println(in.Text())
}
return in.Err()
}
// mk all the directories, download binary tarball, write config
// and check the binary downloaded
func (r *builtinRedis) setup() error {
logs.Printf("setup redis under %s", r.runtimePath)
// download binary is not exist
if !fileExists(binPath) {
if err := downloadRedisBinary(); err != nil {
return err
}
}
// write config
if err := r.buildConfig(); err != nil {
return fmt.Errorf("build config err: %s", err)
}
// check whether redis can be executed
return _smokeTest()
}
func downloadRedisBinary() error {
if f, err := os.Open(binPath); err == nil {
f.Close()
return nil
}
var link string
GOOS := runtime.GOOS
if runtime.GOOS == "darwin" {
link = _linkDarwin
} else if runtime.GOOS == "linux" {
link = _linkLinux
} else {
return fmt.Errorf("unsupported platform %s", GOOS)
}
logs.Printf("download redis-server from %s", link)
resp, err := http.Get(link)
if err != nil {
return fmt.Errorf("cannot download, err: %s", err)
}
defer resp.Body.Close()
if err := unTarball(root, resp.Body); err != nil {
return fmt.Errorf("extract tarball err: %s", err)
}
return nil
}