/
restore.go
102 lines (92 loc) · 1.97 KB
/
restore.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
package lib
import (
"archive/tar"
"compress/gzip"
"errors"
"io"
"os"
"path"
"strings"
_ "github.com/MeshBoxFoundation/meshbox/internal/smc-devnet/statics/statik"
"github.com/rakyll/statik/fs"
)
var (
/*
UrlMap = map[string]map[string]string{
"amd64": {
"darwin": "/devnet.tar.gz",
},
}*/
dataUrl = "/devnet.tar.gz"
ErrorGoarchNotSupport = errors.New("goarch not support")
ErrorGoosNotSupport = errors.New("goos not support")
dataDir string
withoutNodekey bool
)
func Restore(dir string, reset, masterNode bool) (string, error) {
if reset {
os.RemoveAll(dir)
}
os.MkdirAll(dir, 0755)
dataDir = dir
withoutNodekey = !masterNode
return restore()
}
func restore() (string, error) {
var binUrl = dataUrl
/* if m, ok := UrlMap[runtime.GOARCH]; !ok {
return "", ErrorGoarchNotSupport
} else if url, ok := m[runtime.GOOS]; !ok {
return "", ErrorGoosNotSupport
} else {
binUrl = url[:]
}*/
if strings.LastIndex(binUrl, "tar.gz") != -1 {
err := opentargz(dataDir, binUrl)
if err != nil {
panic(err)
}
}
return binUrl, nil
}
func opentargz(binDir, binUrl string) error {
os.MkdirAll(binDir, 0755)
statikFS, err := fs.New()
srcFile, err := statikFS.Open(binUrl)
if err != nil {
return err
}
defer srcFile.Close()
gr, err := gzip.NewReader(srcFile)
if err != nil {
return err
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
if hdr.FileInfo().IsDir() && strings.Index(hdr.Name, "devnet") == 0 ||
withoutNodekey && strings.LastIndex(hdr.Name, "nodekey") > 0 {
continue
}
n := strings.Replace(hdr.Name, "devnet/", "", 1)
fpath := path.Join(binDir, n)
dir, _ := path.Split(fpath)
os.MkdirAll(dir, 0755)
if !hdr.FileInfo().IsDir() {
file, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
io.Copy(file, tr)
}
}
return nil
}