-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.go
More file actions
144 lines (126 loc) · 3.43 KB
/
utils.go
File metadata and controls
144 lines (126 loc) · 3.43 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package utils contains commonly useful functions from Deisctl
package utils
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/deis/deis/deisctl/constant"
uuid "github.com/satori/go.uuid"
)
// NewUUID returns a new V4-style unique identifier.
func NewUUID() string {
u1 := uuid.NewV4()
s1 := fmt.Sprintf("%s", u1)
return strings.Split(s1, "-")[0]
}
func getetcdClient() *etcd.Client {
machines := []string{"http://127.0.0.1:4001/"}
return etcd.NewClient(machines)
}
// GetKey returns the value of an etcd key, or of an environment variable if etcd didn't have
// a value.
func GetKey(dir, key, perm string) string {
c := getetcdClient()
result, err := c.Get(dir+key, false, false)
if err != nil || result.Node.Value == "" {
return os.Getenv(perm)
}
return result.Node.Value
}
// GetClientID returns the CoreOS Machine ID, or an unknown UUID string.
func GetClientID() string {
machineID := GetMachineID("/")
if machineID == "" {
return fmt.Sprintf("{unknown-" + NewUUID() + "}")
}
return machineID
}
// GetMachineID returns the CoreOS Machine ID.
func GetMachineID(root string) string {
fullPath := filepath.Join(root, constant.MachineID)
id, err := ioutil.ReadFile(fullPath)
if err != nil {
return ""
}
return strings.TrimSpace(string(id))
}
// GetVersion returns the package version from a text file resource.
func GetVersion() string {
id, err := ioutil.ReadFile(constant.Version)
if err != nil {
return "0.0.0"
}
return strings.TrimSpace(string(id))
}
// Extract expands a .tar archive file into the specified directory.
func Extract(file, dir string) (err error) {
var wd, _ = os.Getwd()
_ = os.Chdir(dir)
cmdl := exec.Command("tar", "-C", "/", "-xvf", file)
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
fmt.Printf("Failed:\n%v", err)
return err
}
_ = os.Chdir(wd)
return nil
}
// PutVersion updates the package version to a local text file resource.
func PutVersion(version string) error {
return ioutil.WriteFile(constant.Version, []byte(version), 0644)
}
// RunCommandWithStdoutStderr execs a command and returns its output.
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
var stdout, stderr bytes.Buffer
stderrPipe, err := cmd.StderrPipe()
stdoutPipe, err := cmd.StdoutPipe()
cmd.Env = os.Environ()
if err != nil {
fmt.Println("error at io pipes")
}
err = cmd.Start()
if err != nil {
fmt.Println("error at command start")
}
go func() {
io.Copy(&stdout, stdoutPipe)
fmt.Println(stdout.String())
}()
go func() {
io.Copy(&stderr, stderrPipe)
fmt.Println(stderr.String())
}()
time.Sleep(2000 * time.Millisecond)
err = cmd.Wait()
if err != nil {
fmt.Println("error at command wait")
}
return stdout, stderr, err
}
// Execute runs the given script in a shell.
func Execute(script string) error {
cmdl := exec.Command("sh", "-c", script)
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
fmt.Println("(Error )")
return err
}
return nil
}
// DeisIfy returns a pretty-printed deis logo along with the corresponding message
func DeisIfy(message string) string {
circle := "\033[31m●"
square := "\033[32m■"
triangle := "\033[34m▴"
reset := "\033[0m"
title := reset + message
return fmt.Sprintf("%s %s %s\n%s %s %s %s\n%s %s %s%s\n",
circle, triangle, square,
square, circle, triangle, title,
triangle, square, circle, reset)
}