Documentation ¶
Overview ¶
Package utils contains common methods used in most packages in Statping. This package contains multiple function like: Logging, encryption, type conversions, setting utils.Directory as the current directory, running local CMD commands, and creating/deleting files/folder.
You can overwrite the utils.Directory global variable by including STATPING_DIR environment variable to be an absolute path.
More info on: https://github.com/hunterlong/statping
Index ¶
- Constants
- Variables
- func Command(cmd string) (string, string, error)
- func DeleteDirectory(directory string) error
- func DeleteFile(file string) error
- func DurationReadable(d time.Duration) string
- func FileExists(name string) bool
- func FormatDuration(d time.Duration) string
- func HashPassword(password string) string
- func Http(r *http.Request) string
- func HttpRequest(url, method string, content interface{}, headers []string, body io.Reader, ...) ([]byte, *http.Response, error)
- func InitLogs() error
- func Log(level int, err interface{}) error
- func NewSHA1Hash(n ...int) string
- func RandomString(n int) string
- func SaveFile(filename string, data []byte) error
- func Timezoner(t time.Time, zone float32) time.Time
- func ToInt(s interface{}) int64
- func ToString(s interface{}) string
- func UnderScoreString(str string) string
- type LogRow
- type Timestamp
- type Timestamper
Examples ¶
Constants ¶
const ( FlatpickrTime = "2006-01-02 15:04" FlatpickrDay = "2006-01-02" FlatpickrReadable = "Mon, 02 Jan 2006" )
Variables ¶
var ( LastLines []*LogRow LockLines sync.Mutex )
var ( // Directory returns the current path or the STATPING_DIR environment variable Directory string )
Functions ¶
func Command ¶ added in v0.79.1
Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings
in, out, err := Command("sass assets/scss assets/css/base.css")
func DeleteDirectory ¶ added in v0.79.1
DeleteDirectory will attempt to delete a directory and all contents inside
DeleteDirectory("assets")
func DeleteFile ¶ added in v0.29.3
DeleteFile will attempt to delete a file
DeleteFile("newfile.json")
func DurationReadable ¶ added in v0.79.1
DurationReadable will return a time.Duration into a human readable string // t := time.Duration(5 * time.Minute) // DurationReadable(t) // returns: 5 minutes
Example ¶
dur, _ := time.ParseDuration("25m") readable := DurationReadable(dur) fmt.Print(readable)
Output: 25 minutes
func FileExists ¶ added in v0.79.1
FileExists returns true if a file exists
exists := FileExists("assets/css/base.css")
func FormatDuration ¶ added in v0.79.1
FormatDuration converts a time.Duration into a string
func HashPassword ¶
HashPassword returns the bcrypt hash of a password string
func HttpRequest ¶ added in v0.79.9
func HttpRequest(url, method string, content interface{}, headers []string, body io.Reader, timeout time.Duration) ([]byte, *http.Response, error)
HttpRequest is a global function to send a HTTP request // url - The URL for HTTP request // method - GET, POST, DELETE, PATCH // content - The HTTP request content type (text/plain, application/json, or nil) // headers - An array of Headers to be sent (KEY=VALUE) []string{"Authentication=12345", ...} // body - The body or form data to send with HTTP request // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
func InitLogs ¶ added in v0.79.1
func InitLogs() error
InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging
func Log ¶
Log creates a new entry in the Logger. Log has 1-5 levels depending on how critical the log/error is
func NewSHA1Hash ¶
NewSHA1Hash returns a random SHA1 hash based on a specific length
func RandomString ¶
RandomString generates a random string of n length
func SaveFile ¶ added in v0.79.1
SaveFile will create a new file with data inside it
SaveFile("newfile.json", []byte('{"data": "success"}')
func ToString ¶ added in v0.79.1
func ToString(s interface{}) string
ToString converts a int to a string
Example ¶
amount := 42 fmt.Print(ToString(amount))
Output: 42
func UnderScoreString ¶
UnderScoreString will return a string that replaces spaces and other characters to underscores
UnderScoreString("Example String") // example_string
Types ¶
type LogRow ¶ added in v0.79.1
func GetLastLine ¶ added in v0.79.1
func GetLastLine() *LogRow
GetLastLine returns 1 line for a recent log entry
func (*LogRow) FormatForHtml ¶ added in v0.79.1
type Timestamper ¶ added in v0.79.1
type Timestamper interface {
Ago() string
}