-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (90 loc) · 1.9 KB
/
util.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
103
104
105
106
107
package glitterboot
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
tmjson "github.com/tendermint/tendermint/libs/json"
tmos "github.com/tendermint/tendermint/libs/os"
)
// ==== commands ====
func systemctl(args ...string) error {
return exec.Command("systemctl", args...).Run()
}
func systemctlOut(args ...string) (string, error) {
o, err := exec.Command("systemctl", args...).Output()
if err != nil {
return "", err
}
return string(o), nil
}
// ==== util funcs ====
type CopyFileDesc struct {
Src string
Dest string
}
func copyFile(d CopyFileDesc) error {
return tmos.CopyFile(d.Src, d.Dest)
}
func jsonToFile(v interface{}, filename string) error {
b, err := tmjson.Marshal(v)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0644)
}
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func pathJoin(elem ...string) string {
expands := make([]string, len(elem))
for i, s := range elem {
expands[i] = os.ExpandEnv(s)
}
return filepath.Join(expands...)
}
func chown(path string, user, group string, recursion bool) error {
var args []string
if recursion {
args = append(args, "-R")
}
args = append(args, fmt.Sprintf("%s:%s", user, group))
args = append(args, path)
return exec.Command("chown", args...).Run()
}
func checkUserGroup(u, grp string) error {
_, err := user.Lookup(u)
if err != nil {
return err
}
_, err = user.LookupGroup(grp)
if err != nil {
return err
}
return nil
}