utils

package
v0.0.0-...-5073c34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2018 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package utils contains common methods used in most packages in Statup. 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 STATUP_DIR environment variable to be an absolute path.

More info on: https://github.com/hunterlong/statup

Index

Examples

Constants

View Source
const (
	FlatpickrTime     = "2006-01-02 15:04"
	FlatpickrDay      = "2006-01-02"
	FlatpickrReadable = "Mon, 02 Jan 2006"
)

Variables

View Source
var (
	LastLines []*LogRow
	LockLines sync.Mutex
)
View Source
var (
	// Directory returns the current path or the STATUP_DIR environment variable
	Directory string
)

Functions

func Command

func Command(cmd string) (string, string, error)

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

func DeleteDirectory(directory string) error

DeleteDirectory will attempt to delete a directory and all contents inside

DeleteDirectory("assets")

func DeleteFile

func DeleteFile(file string) error

DeleteFile will attempt to delete a file

DeleteFile("newfile.json")

func DurationReadable

func DurationReadable(d time.Duration) string

DurationReadable will return a time.Duration into a human readable string

t := time.Duration(5 * time.Minute)
DurationReadable(t)
// 5 minutes
Example
dur, _ := time.ParseDuration("25m")
readable := DurationReadable(dur)
fmt.Print(readable)
Output:

25 minutes

func FileExists

func FileExists(name string) bool

FileExists returns true if a file exists

exists := FileExists("assets/css/base.css")

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration converts a time.Duration into a string

func HashPassword

func HashPassword(password string) string

HashPassword returns the bcrypt hash of a password string

func Http

func Http(r *http.Request) string

Http returns a log for a HTTP request

func InitLogs

func InitLogs() error

InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging

func Log

func Log(level int, err interface{}) error

Log creates a new entry in the Logger. Log has 1-5 levels depending on how critical the log/error is

func NewSHA1Hash

func NewSHA1Hash(n ...int) string

NewSHA1Hash returns a random SHA1 hash based on a specific length

func RandomString

func RandomString(n int) string

RandomString generates a random string of n length

func SaveFile

func SaveFile(filename string, data []byte) error

SaveFile will create a new file with data inside it

SaveFile("newfile.json", []byte('{"data": "success"}')

func StringInt

func StringInt(s string) int64

StringInt converts a string to an int64

Example
amount := "42"
fmt.Print(StringInt(amount))
Output:

42

func Timezoner

func Timezoner(t time.Time, zone float32) time.Time

Timezoner returns the time.Time with the user set timezone

func ToString

func ToString(s interface{}) string

ToString converts a int to a string

Example
amount := 42
fmt.Print(ToString(amount))
Output:

42

func UnderScoreString

func UnderScoreString(str string) string

UnderScoreString will return a string that replaces spaces and other characters to underscores

UnderScoreString("Example String")
// example_string

Types

type LogRow

type LogRow struct {
	Date time.Time
	Line interface{}
}

func GetLastLine

func GetLastLine() *LogRow

GetLastLine returns 1 line for a recent log entry

func (*LogRow) FormatForHtml

func (o *LogRow) FormatForHtml() string

type Timestamp

type Timestamp time.Time

func (Timestamp) Ago

func (t Timestamp) Ago() string

Ago returns a human readable timestamp based on the Timestamp (time.Time) interface

type Timestamper

type Timestamper interface {
	Ago() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL