grafana/pkg/util/strings.go
Mario Trangoni 5f6383a750 pkg/util/*: Add missing function comments.
See,

$ gometalinter --vendor --deadline 10m --disable-all --enable=golint  ./...
encoding.go:15:1⚠️ comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint)
encoding.go:30:1⚠️ exported function EncodePassword should have comment or be unexported (golint)
encoding.go:35:1⚠️ comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint)
encoding.go:42:1⚠️ comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint)
encoding.go:80:1⚠️ exported function GetBasicAuthHeader should have comment or be unexported (golint)
encoding.go:85:1⚠️ exported function DecodeBasicAuthHeader should have comment or be unexported (golint)
encoding.go:105:1⚠️ exported function RandomHex should have comment or be unexported (golint)
encryption.go:14:1⚠️ exported function Decrypt should have comment or be unexported (golint)
encryption.go:39:1⚠️ exported function Encrypt should have comment or be unexported (golint)
ip.go:7:1⚠️ exported function SplitIpPort should have comment or be unexported (golint)
json.go:3:6⚠️ exported type DynMap should have comment or be unexported (golint)
md5.go:22:1⚠️ comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint)
strings.go:10:1⚠️ exported function StringsFallback2 should have comment or be unexported (golint)
strings.go:14:1⚠️ exported function StringsFallback3 should have comment or be unexported (golint)
strings.go:27:1⚠️ exported function SplitString should have comment or be unexported (golint)
strings.go:35:1⚠️ exported function GetAgeString should have comment or be unexported (golint)
url.go:8:6⚠️ exported type UrlQueryReader should have comment or be unexported (golint)
url.go:12:1⚠️ exported function NewUrlQueryReader should have comment or be unexported (golint)
url.go:23:1⚠️ exported method UrlQueryReader.Get should have comment or be unexported (golint)
url.go:32:1⚠️ exported function JoinUrlFragments should have comment or be unexported (golint)
validation.go:16:1⚠️ exported function IsEmail should have comment or be unexported (golint)
2019-01-28 22:09:40 +01:00

69 lines
1.4 KiB
Go

package util
import (
"fmt"
"math"
"regexp"
"time"
)
// StringsFallback2 returns the first of two not empty strings.
func StringsFallback2(val1 string, val2 string) string {
return stringsFallback(val1, val2)
}
// StringsFallback3 returns the first of three not empty strings.
func StringsFallback3(val1 string, val2 string, val3 string) string {
return stringsFallback(val1, val2, val3)
}
func stringsFallback(vals ...string) string {
for _, v := range vals {
if v != "" {
return v
}
}
return ""
}
// SplitString splits a string by commas or empty spaces.
func SplitString(str string) []string {
if len(str) == 0 {
return []string{}
}
return regexp.MustCompile("[, ]+").Split(str, -1)
}
// GetAgeString returns a string representing certain time from years to minutes.
func GetAgeString(t time.Time) string {
if t.IsZero() {
return "?"
}
sinceNow := time.Since(t)
minutes := sinceNow.Minutes()
years := int(math.Floor(minutes / 525600))
months := int(math.Floor(minutes / 43800))
days := int(math.Floor(minutes / 1440))
hours := int(math.Floor(minutes / 60))
if years > 0 {
return fmt.Sprintf("%dy", years)
}
if months > 0 {
return fmt.Sprintf("%dM", months)
}
if days > 0 {
return fmt.Sprintf("%dd", days)
}
if hours > 0 {
return fmt.Sprintf("%dh", hours)
}
if int(minutes) > 0 {
return fmt.Sprintf("%dm", int(minutes))
}
return "< 1m"
}