-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathutils.go
187 lines (156 loc) · 4.16 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
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package utils provides reusable and sharable utilities for other packages and components.
package utils
import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/gocraft/work"
"github.com/pkg/errors"
"io"
"net"
"net/url"
"os"
"strconv"
"strings"
)
// NodeIDContextKey is used to keep node ID in the system context
type NodeIDContextKey string
const (
// NodeID is const of the ID context key
NodeID NodeIDContextKey = "node_id"
)
// MakeIdentifier creates uuid for job.
func MakeIdentifier() string {
b := make([]byte, 12)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
return ""
}
return fmt.Sprintf("%x", b)
}
// IsEmptyStr check if the specified str is empty (len ==0) after triming prefix and suffix spaces.
func IsEmptyStr(str string) bool {
return len(strings.TrimSpace(str)) == 0
}
// ReadEnv return the value of env variable.
func ReadEnv(key string) string {
return os.Getenv(key)
}
// FileExists check if the specified exists.
func FileExists(file string) bool {
if !IsEmptyStr(file) {
_, err := os.Stat(file)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
return false
}
// DirExists check if the specified dir exists
func DirExists(path string) bool {
if IsEmptyStr(path) {
return false
}
f, err := os.Stat(path)
if err != nil {
return false
}
return f.IsDir()
}
// IsValidPort check if port is valid.
func IsValidPort(port uint) bool {
return port != 0 && port < 65536
}
// IsValidURL validates if the url is well-formted
func IsValidURL(address string) bool {
if IsEmptyStr(address) {
return false
}
if _, err := url.Parse(address); err != nil {
return false
}
return true
}
// TranslateRedisAddress translates the comma format to redis URL
func TranslateRedisAddress(commaFormat string) (string, bool) {
if IsEmptyStr(commaFormat) {
return "", false
}
sections := strings.Split(commaFormat, ",")
totalSections := len(sections)
if totalSections == 0 {
return "", false
}
urlParts := make([]string, 0)
// section[0] should be host:port
redisURL := fmt.Sprintf("redis://%s", sections[0])
if _, err := url.Parse(redisURL); err != nil {
return "", false
}
urlParts = append(urlParts, "redis://", sections[0])
// Ignore weight
// Check password
if totalSections >= 3 && !IsEmptyStr(sections[2]) {
urlParts = []string{urlParts[0], fmt.Sprintf("%s:%s@", "arbitrary_username", sections[2]), urlParts[1]}
}
if totalSections >= 4 && !IsEmptyStr(sections[3]) {
if _, err := strconv.Atoi(sections[3]); err == nil {
urlParts = append(urlParts, "/", sections[3])
}
}
return strings.Join(urlParts, ""), true
}
// SerializeJob encodes work.Job to json data.
func SerializeJob(job *work.Job) ([]byte, error) {
return json.Marshal(job)
}
// DeSerializeJob decodes bytes to ptr of work.Job.
func DeSerializeJob(jobBytes []byte) (*work.Job, error) {
var j work.Job
err := json.Unmarshal(jobBytes, &j)
return &j, err
}
// ResolveHostnameAndIP gets the local hostname and IP
func ResolveHostnameAndIP() (string, error) {
host, err := os.Hostname()
if err != nil {
return "", err
}
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return fmt.Sprintf("%s:%s", host, ipnet.IP.String()), nil
}
}
}
return "", errors.New("failed to resolve local host&ip")
}
// GenerateNodeID returns ID of current node
func GenerateNodeID() string {
hIP, err := ResolveHostnameAndIP()
if err != nil {
return MakeIdentifier()
}
return hIP
}