-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcommon.go
161 lines (138 loc) · 3.82 KB
/
common.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
/*
Copyright 2021 RadonDB.
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
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
// Min returns the smallest int64 that was passed in the arguments.
func Min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
// Max returns the largest int64 that was passed in the arguments.
func Max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
// StringInArray check whether the str is in the strArray.
func StringInArray(str string, strArray []string) bool {
sort.Strings(strArray)
index := sort.SearchStrings(strArray, str)
return index < len(strArray) && strArray[index] == str
}
func GetOrdinal(name string) (int, error) {
idx := strings.LastIndexAny(name, "-")
if idx == -1 {
return -1, fmt.Errorf("failed to extract ordinal from name: %s", name)
}
ordinal, err := strconv.Atoi(name[idx+1:])
if err != nil {
return -1, fmt.Errorf("failed to extract ordinal from name: %s", name)
}
return ordinal, nil
}
// Create the Update file.
func TouchUpdateFile() error {
var err error
var file *os.File
if file, err = os.Create(FileIndicateUpdate); err != nil {
return err
}
file.Close()
return nil
}
// Remove the Update file.
func RemoveUpdateFile() error {
return os.Remove(FileIndicateUpdate)
}
// Check update file exist.
func ExistUpdateFile() bool {
f, err := os.Open(FileIndicateUpdate)
if os.IsNotExist(err) {
return false
} else if err != nil {
return true
}
f.Close()
return true
}
// Build the backup directory name by time.
func BuildBackupName(name string) (string, string) {
cur_time := time.Now()
return fmt.Sprintf("%s_%v%v%v%v%v%v", name, cur_time.Year(), int(cur_time.Month()),
cur_time.Day(), cur_time.Hour(), cur_time.Minute(), cur_time.Second()),
fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
cur_time.Year(), cur_time.Month(), cur_time.Day(),
cur_time.Hour(), cur_time.Minute(), cur_time.Second())
}
func StringDiffIn(actual, desired []string) []string {
diff := []string{}
for _, aStr := range actual {
// if is not in the desired list remove it.
if _, exists := stringIn(aStr, desired); !exists {
diff = append(diff, aStr)
}
}
return diff
}
func stringIn(str string, strs []string) (int, bool) {
for i, s := range strs {
if s == str {
return i, true
}
}
return 0, false
}
func UnmarshalJSON(in io.Reader, obj interface{}) error {
body, err := ioutil.ReadAll(in)
if err != nil {
return fmt.Errorf("io read error: %s", err)
}
if err = json.Unmarshal(body, obj); err != nil {
return fmt.Errorf("error unmarshal data, error: %s, body: %s", err, string(body))
}
return nil
}
// Parase image prefix,image name,image tag. eg: percona/percona-server:5.7.35 -> percona,percona-server,5.7.35
func ParseImageName(image string) (string, string, string, error) {
imagePrefix, imageString := filepath.Split(image)
imagePrefix = strings.TrimSuffix(imagePrefix, "/")
imageNameArry := strings.Split(imageString, ":")
if len(imageNameArry) <= 1 {
return "", "", "", fmt.Errorf("image name or tag is empty")
}
imageName := imageNameArry[0]
imageTag := imageNameArry[1]
return imagePrefix, imageName, imageTag, nil
}
func ParseIPAndPath(nfsaddr string) (string, string) {
res := strings.Split(nfsaddr, ":")
if len(res) == 2 {
return res[0], res[1]
} else {
return res[0], "/"
}
}