github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/pkg/utils/common/utils.go (about) 1 package commonUtils 2 3 import ( 4 "fmt" 5 "math/rand" 6 "net" 7 "os" 8 "path/filepath" 9 "reflect" 10 "regexp" 11 "runtime" 12 "strconv" 13 "strings" 14 "time" 15 16 consts "github.com/easysoft/zendata/internal/pkg/const" 17 "github.com/easysoft/zendata/internal/pkg/model" 18 stringUtils "github.com/easysoft/zendata/pkg/utils/string" 19 "github.com/emirpasic/gods/maps" 20 ) 21 22 func Base(pathStr string) string { 23 pathStr = filepath.ToSlash(pathStr) 24 return filepath.Base(pathStr) 25 } 26 27 func RemoveBlankLine(str string) string { 28 myExp := regexp.MustCompile(`\n{3,}`) // 连续换行 29 ret := myExp.ReplaceAllString(str, "\n\n") 30 31 ret = strings.Trim(ret, "\n") 32 //ret = strings.TrimSpace(ret) 33 34 return ret 35 } 36 37 func BoolToPass(b bool) string { 38 if b { 39 return consts.PASS.String() 40 } else { 41 return consts.FAIL.String() 42 } 43 } 44 45 func GetOs() string { 46 osName := runtime.GOOS 47 48 if osName == "darwin" { 49 return "mac" 50 } else { 51 return strings.Replace(osName, "windows", "win", 1) 52 } 53 } 54 func IsWin() bool { 55 return GetOs() == "win" 56 } 57 func IsLinux() bool { 58 return GetOs() == "linux" 59 } 60 func IsMac() bool { 61 return GetOs() == "mac" 62 } 63 64 func IsRelease() bool { 65 if os.Getenv("DEBUG") == "true" { 66 return false 67 } 68 69 arg1 := strings.ToLower(os.Args[0]) 70 name := filepath.Base(arg1) 71 72 ret := strings.Index(arg1, "go-build") < 0 && 73 strings.Index(name, "___") != 0 && strings.Index(name, "go-build") != 0 74 75 return ret 76 } 77 78 func UpdateUrl(url string) string { 79 if strings.LastIndex(url, "/") < len(url)-1 { 80 url += "/" 81 } 82 83 return url 84 } 85 86 func IgnoreFile(path string) bool { 87 path = filepath.Base(path) 88 89 if strings.Index(path, ".") == 0 || 90 path == "bin" || path == "release" || path == "logs" || path == "xdoc" { 91 return true 92 } else { 93 return false 94 } 95 } 96 97 func GetFieldVal(config model.Config, key string) string { 98 key = stringUtils.Ucfirst(key) 99 100 immutable := reflect.ValueOf(config) 101 val := immutable.FieldByName(key).String() 102 103 return val 104 } 105 106 func SetFieldVal(config *model.Config, key string, val string) string { 107 key = stringUtils.Ucfirst(key) 108 109 mutable := reflect.ValueOf(config).Elem() 110 mutable.FieldByName(key).SetString(val) 111 112 return val 113 } 114 115 func LinkedMapToMap(mp maps.Map) map[string]string { 116 ret := make(map[string]string, 0) 117 118 for _, keyIfs := range mp.Keys() { 119 valueIfs, _ := mp.Get(keyIfs) 120 121 key := strings.TrimSpace(keyIfs.(string)) 122 value := strings.TrimSpace(valueIfs.(string)) 123 124 ret[key] = value 125 } 126 127 return ret 128 } 129 130 func GetIp() string { 131 ifaces, err := net.Interfaces() 132 if err != nil { 133 return "" 134 } 135 136 ipMap := map[string]string{} 137 for _, iface := range ifaces { 138 if iface.Flags&net.FlagUp == 0 { 139 continue // interface down 140 } 141 if iface.Flags&net.FlagLoopback != 0 { 142 continue // loopback interface 143 } 144 addrs, err := iface.Addrs() 145 if err != nil { 146 return "" 147 } 148 for _, addr := range addrs { 149 ip := getIpFromAddr(addr) 150 if ip == nil { 151 continue 152 } 153 154 ipType := GetIpType(ip) 155 ipMap[ipType] = ip.String() 156 } 157 } 158 159 if ipMap["public"] != "" { 160 return ipMap["public"] 161 } else if ipMap["private"] != "" { 162 return ipMap["private"] 163 } else { 164 return "" 165 } 166 } 167 168 func getIpFromAddr(addr net.Addr) net.IP { 169 var ip net.IP 170 switch v := addr.(type) { 171 case *net.IPNet: 172 ip = v.IP 173 case *net.IPAddr: 174 ip = v.IP 175 } 176 if ip == nil || ip.IsLoopback() { 177 return nil 178 } 179 ip = ip.To4() 180 if ip == nil { 181 return nil // not an ipv4 address 182 } 183 184 return ip 185 } 186 187 func GetIpType(IP net.IP) string { 188 if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() { 189 return "" 190 } 191 if ip4 := IP.To4(); ip4 != nil { 192 switch true { 193 case ip4[0] == 10: 194 return "private" 195 case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31: 196 return "private" 197 case ip4[0] == 192 && ip4[1] == 168: 198 return "private" 199 default: 200 return "public" 201 } 202 } 203 return "" 204 } 205 206 func RandNum(length int) int { 207 randSeeds := time.Now().Unix() + int64(rand.Intn(100000000)) 208 rand.Seed(randSeeds) 209 210 seedInt := rand.Intn(length) 211 return seedInt 212 } 213 func RandNum64(length int64) int64 { 214 randSeeds := time.Now().Unix() + int64(rand.Intn(100000000)) 215 rand.Seed(randSeeds) 216 217 seedInt := rand.Int63n(length) 218 return seedInt 219 } 220 221 func ChanePrecision(value float64, precision int) float64 { 222 temp := fmt.Sprintf("%."+strconv.Itoa(precision)+"f", value) 223 value, _ = strconv.ParseFloat(temp, 64) 224 return value 225 } 226 227 func GetType(i interface{}) string { 228 switch i.(type) { 229 case int64: 230 return "int" 231 case float64: 232 return "float" 233 case byte: 234 return "byte" 235 case string: 236 return "string" 237 } 238 239 return "string" 240 } 241 242 func IsRunAsBackendProcess() (found bool) { 243 found, _ = stringUtils.FindInArr("-uuid", os.Args) 244 return 245 }