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/statping-ng /statping-ng
Index ¶
- Constants
- Variables
- func CheckHash(password, hash string) bool
- func CloseLogs()
- func Command(name string, args ...string) (string, string, error)
- func CreateDirectory(directory string) error
- func DeleteDirectory(directory string) error
- func DeleteFile(file string) error
- func DirWritable(path string) (bool, error)
- func DurationReadable(d time.Duration) string
- func FileExists(name string) bool
- func FileExtension(path string) string
- func FolderExists(folder string) bool
- func FormatDuration(d time.Duration) string
- func GetLastLine() *logRow
- func HashPassword(password string) string
- func HttpRequest(endpoint, method string, contentType interface{}, headers []string, ...) ([]byte, *http.Response, error)
- func InitEnvs()
- func InitLogs() error
- func NewSHA256Hash() string
- func NotNumber(val string) bool
- func Now() time.Time
- func OpenFile(filePath string) (string, error)
- func Ping(address string, secondsTimeout int) (int64, error)
- func RandomString(n int) string
- func RenameDirectory(fromDir string, toDir string) error
- func SaveFile(filename string, data []byte) error
- func SentryErr(err error)
- func SentryInit(allow bool)
- func SentryLogEntry(entry *Logger.Entry)
- func Sha256Hash(val string) string
- func ToFields(d ...interface{}) map[string]interface{}
- func ToInt(s interface{}) int64
- func ToString(s interface{}) string
- type Duration
- type Perlin
Examples ¶
Constants ¶
const ( B = 0x100 N = 0x1000 BM = 0xff )
Variables ¶
var ( Log = Logger.StandardLogger() LastLines []*logRow LockLines sync.Mutex VerboseMode int )
var ( // Directory returns the current path or the STATPING_DIR environment variable Directory string )
var (
Params *viper.Viper
)
Functions ¶
func CheckHash ¶ added in v0.90.77
CheckHash returns true if the password matches with a hashed bcrypt password
func CloseLogs ¶ added in v0.90.77
func CloseLogs()
CloseLogs will close the log file correctly on shutdown
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 CreateDirectory ¶ added in v0.90.77
CreateDirectory will attempt to create a directory
CreateDirectory("assets")
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 DirWritable ¶ added in v0.90.77
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 FileExtension ¶ added in v0.90.77
FileExtension returns the file extension based on a file path
func FolderExists ¶ added in v0.90.77
FolderExists will return true if the folder exists
func FormatDuration ¶ added in v0.79.1
FormatDuration converts a time.Duration into a string
func GetLastLine ¶ added in v0.79.1
func GetLastLine() *logRow
GetLastLine returns 1 line for a recent log entry
func HashPassword ¶
HashPassword returns the bcrypt hash of a password string
func HttpRequest ¶ added in v0.79.9
func HttpRequest(endpoint, method string, contentType interface{}, headers []string, body io.Reader, timeout time.Duration, verifySSL bool, customTLS *tls.Config) ([]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) // You can use a HTTP Proxy if you HTTP_PROXY environment variable
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 NewSHA256Hash ¶ added in v0.90.77
func NewSHA256Hash() string
NewSHA1Hash returns a random SHA1 hash based on a specific length
func RandomString ¶
RandomString generates a random string of n length
func RenameDirectory ¶ added in v0.90.77
RenameDirectory will attempt rename a directory to a new name
func SaveFile ¶ added in v0.79.1
SaveFile will create a new file with data inside it
SaveFile("newfile.json", []byte('{"data": "success"}')
func SentryInit ¶ added in v0.90.77
func SentryInit(allow bool)
func SentryLogEntry ¶ added in v0.90.77
func Sha256Hash ¶ added in v0.90.77
NewSHA1Hash returns a random SHA1 hash based on a specific length
func ToFields ¶ added in v0.90.77
func ToFields(d ...interface{}) map[string]interface{}
ToFields accepts any amount of interfaces to create a new mapping for log.Fields. You will need to turn on verbose mode by starting Statping with "-v". This function will convert a struct of to the base struct name, and each field into it's own mapping, for example: type "*services.Service", on string field "Name" converts to "service_name=value". There is also an additional field called "_pointer" that will return the pointer hex value.
Types ¶
type Perlin ¶ added in v0.80.51
type Perlin struct {
// contains filtered or unexported fields
}
Perlin is the noise generator