2024-03-01 15:06:08 +01:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// misc-utis.go
|
2024-02-16 15:51:28 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
"golang.org/x/text/message"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewEnglishPrinter() *message.Printer {
|
|
|
|
return message.NewPrinter(language.English)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExitErrorf(rc int, format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
|
|
|
os.Exit(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExitMessagef(rc int, format string, args ...interface{}) {
|
|
|
|
fmt.Printf(format+"\n", args...)
|
|
|
|
os.Exit(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnStringIndex(index uint, values ...string) (value string) {
|
|
|
|
if index >= 0 && index < uint(len(values)) {
|
|
|
|
value = values[index]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnEmpty(s string, altValues ...string) (value string) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
for _, altValue := range altValues {
|
|
|
|
if len(altValue) > 0 {
|
|
|
|
value = altValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnCond(cond bool, trueValue, falseValue string) string {
|
|
|
|
if cond {
|
|
|
|
return trueValue
|
|
|
|
} else {
|
|
|
|
return falseValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnCondIface(cond bool, trueValue, falseValue interface{}) interface{} {
|
|
|
|
if cond {
|
|
|
|
return trueValue
|
|
|
|
} else {
|
|
|
|
return falseValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnCondInt(cond bool, trueValue, falseValue int) int {
|
|
|
|
if cond {
|
|
|
|
return trueValue
|
|
|
|
} else {
|
|
|
|
return falseValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnCondInt64(cond bool, trueValue, falseValue int64) int64 {
|
|
|
|
if cond {
|
|
|
|
return trueValue
|
|
|
|
} else {
|
|
|
|
return falseValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnSuccess(cond bool) string {
|
|
|
|
return OnCond(cond, "SUCCEEDED", "FAILED")
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnOk(cond bool) string {
|
|
|
|
return OnCond(cond, "OK", "KO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func RandBool() bool {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
return rand.Intn(2) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func S2Uint32(s string, defaultValue uint32) uint32 {
|
|
|
|
var res uint32
|
|
|
|
v, err := strconv.ParseUint(s, 10, 32)
|
|
|
|
if err == nil {
|
|
|
|
res = uint32(v)
|
|
|
|
} else {
|
|
|
|
res = defaultValue
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func S2Int32(s string, defaultValue int32) int32 {
|
|
|
|
var res int32
|
|
|
|
v, err := strconv.ParseUint(s, 10, 32)
|
|
|
|
if err == nil {
|
|
|
|
res = int32(v)
|
|
|
|
} else {
|
|
|
|
res = defaultValue
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveQuotes(s string) string {
|
|
|
|
l := len(s)
|
|
|
|
if l >= 2 {
|
|
|
|
if (s[0] == '"' && s[l-1] == '"') || (s[0] == '\'' && s[l-1] == '\'') {
|
|
|
|
s = s[1 : l-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Credits: https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
|
|
|
|
func ByteCountSI(b int64) string {
|
|
|
|
const unit = 1000
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %cB",
|
|
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Credits: https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
|
|
|
|
func ByteCountIEC(b int64) string {
|
|
|
|
const unit = 1024
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %ciB",
|
|
|
|
float64(b)/float64(div), "KMGTPE"[exp])
|
|
|
|
}
|