This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathutils.go
223 lines (194 loc) · 4.8 KB
/
utils.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package utils
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"mime"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/docker/docker/pkg/signal"
)
var (
GITCOMMIT string = "0"
VERSION string
IAMSTATIC string = "true"
INITSHA1 string = ""
INITPATH string = ""
HYPER_ROOT string
HYPER_FILE string
HYPER_DAEMON interface{}
)
const (
APIVERSION = "1.17"
)
func MatchesContentType(contentType, expectedType string) bool {
mimetype, _, err := mime.ParseMediaType(contentType)
if err != nil {
fmt.Printf("Error parsing media type: %s error: %v", contentType, err)
}
return err == nil && mimetype == expectedType
}
func UriReader(uri string) (io.ReadCloser, error) {
if strings.HasPrefix(uri, "http:") || strings.HasPrefix(uri, "https:") {
req, _ := http.NewRequest("GET", uri, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
} else if strings.HasPrefix(uri, "file://") {
src := strings.TrimPrefix(uri, "file://")
f, err := os.Open(src)
if err != nil {
return nil, err
}
return f, nil
}
return nil, fmt.Errorf("Unsupported URI: %s", uri)
}
// FormatMountLabel returns a string to be used by the mount command.
// The format of this string will be used to alter the labeling of the mountpoint.
// The string returned is suitable to be used as the options field of the mount command.
// If you need to have additional mount point options, you can pass them in as
// the first parameter. Second parameter is the label that you wish to apply
// to all content in the mount point.
func FormatMountLabel(src, mountLabel string) string {
if mountLabel != "" {
switch src {
case "":
src = fmt.Sprintf("context=%q", mountLabel)
default:
src = fmt.Sprintf("%s,context=%q", src, mountLabel)
}
}
return src
}
func PermInt(str string) int {
var res = 0
if str[0] == '0' {
if len(str) == 1 {
res = 0
} else if str[1] == 'x' {
// this is hex number
i64, err := strconv.ParseInt(str[2:], 16, 0)
if err == nil {
res = int(i64)
}
} else {
// this is a octal number
i64, err := strconv.ParseInt(str[2:], 8, 0)
if err == nil {
res = int(i64)
}
}
} else {
res, _ = strconv.Atoi(str)
}
if res > 511 {
res = 511
}
return res
}
func UidInt(str string) int {
switch str {
case "", "root":
return 0
default:
i, err := strconv.Atoi(str)
if err != nil {
return 0
}
return i
}
}
func RandStr(strSize int, randType string) string {
var dictionary string
if randType == "alphanum" {
dictionary = "0123456789abcdefghijklmnopqrstuvwxyz"
}
if randType == "alpha" {
dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
if randType == "number" {
dictionary = "0123456789"
}
var bytes = make([]byte, strSize)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
func JSONMarshal(v interface{}, safeEncoding bool) ([]byte, error) {
b, err := json.Marshal(v)
if safeEncoding {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
return b, err
}
func SetDaemon(d interface{}) {
HYPER_DAEMON = d
}
func GetHostIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func ParseTimeString(str string) (time.Time, error) {
t := time.Date(0, 0, 0, 0, 0, 0, 0, time.Local)
if str == "" {
return t, nil
}
layout := "2006-01-02T15:04:05Z"
t, err := time.Parse(layout, str)
return t, err
}
func Timeout(second int) <-chan time.Time {
if second < 0 {
return make(chan time.Time)
}
return time.After(time.Duration(second) * time.Second)
}
func StringToSignal(s string) syscall.Signal {
sig, ok := signal.SignalMap[s]
if !ok {
sig = syscall.SIGTERM
}
return sig
}
func RsplitN(s, sep string, n int) []string {
if n == 0 {
return nil
}
ret := strings.Split(s, sep)
for n > 0 && len(ret) > n {
ret[1] = ret[0] + sep + ret[1]
ret = ret[1:]
}
return ret
}
const DockerRestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
var DockerRestrictedNamePattern = regexp.MustCompile(`^/?` + DockerRestrictedNameChars + `+$`)
// RestrictedVolumeNamePattern is a regular expression to validate volume names against the collection of restricted characters.
var RestrictedVolumeNamePattern = regexp.MustCompile(`^` + DockerRestrictedNameChars + `+$`)