statping/utils/utils.go

71 lines
1.3 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package utils
import (
2018-07-17 09:18:20 +00:00
"github.com/ararog/timeago"
"os"
2018-06-30 00:57:05 +00:00
"regexp"
"strconv"
"strings"
2018-07-17 09:18:20 +00:00
"time"
2018-06-30 00:57:05 +00:00
)
func StringInt(s string) int64 {
num, _ := strconv.Atoi(s)
return int64(num)
}
2018-07-17 09:18:20 +00:00
func IntString(s int) string {
return strconv.Itoa(s)
}
2018-07-28 01:50:13 +00:00
func Dir() string {
dir, err := os.Getwd()
if err != nil {
return "."
}
return dir
}
2018-07-17 09:18:20 +00:00
type Timestamp time.Time
type Timestamper interface {
Ago() string
}
func (t Timestamp) Ago() string {
got, _ := timeago.TimeAgoWithTime(time.Now(), time.Time(t))
return got
}
2018-06-30 00:57:05 +00:00
func UnderScoreString(str string) string {
// convert every letter to lower case
newStr := strings.ToLower(str)
// convert all spaces/tab to underscore
regExp := regexp.MustCompile("[[:space:][:blank:]]")
newStrByte := regExp.ReplaceAll([]byte(newStr), []byte("_"))
regExp = regexp.MustCompile("`[^a-z0-9]`i")
newStrByte = regExp.ReplaceAll(newStrByte, []byte("_"))
regExp = regexp.MustCompile("[!/']")
newStrByte = regExp.ReplaceAll(newStrByte, []byte("_"))
// and remove underscore from beginning and ending
newStr = strings.TrimPrefix(string(newStrByte), "_")
newStr = strings.TrimSuffix(newStr, "_")
return newStr
}
func DeleteFile(file string) bool {
err := os.Remove(file)
if err != nil {
Log(3, err)
return false
}
return true
}