2016-04-18 17:59:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-04-18 17:59:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-17 12:40:37 +00:00
|
|
|
"os"
|
2016-04-18 17:59:34 +00:00
|
|
|
"regexp"
|
2016-06-17 06:03:18 +00:00
|
|
|
"strings"
|
2016-04-18 17:59:34 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
prettifyDurationRegexp = regexp.MustCompile("([.][0-9]+)")
|
|
|
|
)
|
|
|
|
|
|
|
|
func PrettifyDurationOutput(d time.Duration) string {
|
|
|
|
if d < time.Second {
|
|
|
|
return "0s"
|
|
|
|
}
|
|
|
|
result := fmt.Sprintf("%s", d)
|
|
|
|
result = prettifyDurationRegexp.ReplaceAllString(result, "")
|
|
|
|
return result
|
|
|
|
}
|
2016-05-17 12:40:37 +00:00
|
|
|
|
|
|
|
func FileExists(fileName string) bool {
|
|
|
|
if _, err := os.Stat(fileName); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-06-17 06:03:18 +00:00
|
|
|
|
2016-06-18 19:12:07 +00:00
|
|
|
// StringContainsAll returns true if `s` contains all non empty given `substrings`
|
|
|
|
// The function returns `false` if no non-empty arguments are given.
|
2016-06-17 06:03:18 +00:00
|
|
|
func StringContainsAll(s string, substrings ...string) bool {
|
|
|
|
nonEmptyStringsFound := false
|
|
|
|
for _, substring := range substrings {
|
2016-06-18 19:12:07 +00:00
|
|
|
if substring == "" {
|
2016-06-17 06:03:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(s, substring) {
|
|
|
|
nonEmptyStringsFound = true
|
|
|
|
} else {
|
|
|
|
// Immediate failure
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nonEmptyStringsFound
|
|
|
|
}
|