utl

package module
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 20 Imported by: 1

README

utl

Package utl is a library of very common housekeeping functions. Many of these functions seem very simple and almost unnecessary, but they make the calling code much easier to read and digest.

Documentation

Overview

Package utl is a library of very common housekeeping functions. Many of these functions seem very simple and almost unnecessary, but they can make code much easier to read and digest.

Index

Constants

This section is empty.

Variables

View Source
var (
	Red = color.FgLightRed.Render
	Blu = color.FgLightBlue.Render
	Gre = color.FgGreen.Render
	Yel = color.FgYellow.Render
	Whi = color.FgWhite.Render
	Cya = color.FgCyan.Render
	Mag = color.FgLightMagenta.Render
	Gra = color.FgDarkGray.Render

	Red2 = color.FgRed.Render
	Blu2 = color.FgBlue.Render
	Gre2 = color.FgLightGreen.Render
	Yel2 = color.FgLightYellow.Render
	Whi2 = color.FgLightWhite.Render
	Cya2 = color.FgLightCyan.Render
	Mag2 = color.FgMagenta.Render
)

Functions

func BytesToYamlObject added in v1.1.9

func BytesToYamlObject(yamlBytes []byte) (yamlObject interface{}, err error)

Convert byte slice to YAML interface object

func ConvertDateFormat added in v1.1.2

func ConvertDateFormat(dateString, srcFormat, dstFormat string) (string, error)

Converts dateString from source format to destination format. Returns date string in destination format and an error if the conversion fails.

func DateStringToEpocInt64 added in v1.1.2

func DateStringToEpocInt64(dateString, dateFormat string) (int64, error)

Convert dateString, given in dateFormat, to Unix Epoc seconds int64

func Die

func Die(format string, args ...interface{})

Same as regular Printf function but always exits, with a return code of 1

func EpocInt64ToTime added in v1.1.8

func EpocInt64ToTime(epocInt int64) time.Time

Converts an epoch timestamp in int64 format to a time.Time object.

func EpocStringToTime added in v1.1.2

func EpocStringToTime(epocString string) (time.Time, error)

Converts an epoch timestamp in string format to a time.Time object. Returns a time.Time object and an error if the conversion fails.

func FileAge added in v1.2.0

func FileAge(filePath string) int64

Returns given filePath age in seconds int64

func FileExist

func FileExist(filePath string) (e bool)

Returns true if given filePath exists. False otherwise.

func FileModTime

func FileModTime(filePath string) int

Returns given filePath modified time in Unix epoch int

func FileNotExist

func FileNotExist(filePath string) (e bool)

Returns true is given filePath does not exist. False otherwise.

func FileSize

func FileSize(filePath string) int64

Returns size of given filePath as int64

func FileUsable

func FileUsable(filePath string) (e bool)

Returns true if filepath exists and has some content. False otherwise.

func FirstN added in v1.1.3

func FirstN(s string, n int) string

Return first N chars of string n

func GetDateInDays added in v1.1.8

func GetDateInDays(days string) time.Time

Print yyyy-mm-dd date for given number of +/- days in future or past

func GetDaysBetween added in v1.1.8

func GetDaysBetween(date1, date2 string) int64

Return number of days between 2 dates

func GetDaysSinceOrTo added in v1.1.8

func GetDaysSinceOrTo(date1 string) int64

Calculate and return number of +/- days from NOW to date given Note: Calculations are all in UTC time. And it takes leap year into account.

func GetType added in v1.0.0

func GetType(v interface{}) string

func Int64Abs added in v1.1.8

func Int64Abs(x int64) int64

Returns absolute value of int64 value

func Int64ToString added in v1.1.8

func Int64ToString(i int64) string

Converts int64 number to string

func IntAbs added in v1.1.2

func IntAbs(x int) int

Return absolute value of int value

func InternetIsAvailable added in v1.2.0

func InternetIsAvailable() bool

Checks if internet is available. This is of course a very poor check relying on DNS resolution working and this particular URL being available.

func InternetNotAvailable added in v1.2.0

func InternetNotAvailable() bool

Inverse of above function.

func IsAlpha added in v1.1.12

func IsAlpha(c rune) bool

Checks if given rune is an alphabetic character (either uppercase or lowercase). Returns true if it is, false otherwise.

func IsDigit added in v1.1.12

func IsDigit(c rune) bool

Returns true if rune is a numerical digit. False otherwise.

func IsHexDigit added in v1.0.6

func IsHexDigit(c rune) bool

Returns true if rune is a hexadeximal digit. False otherwise.

func IsIpPortStrReachable added in v1.1.12

func IsIpPortStrReachable(ipPortStr string) bool

Checks if IP_Address:Port string is reachable

func IsLeapYear added in v1.1.8

func IsLeapYear(year int64) bool

Returns true if given year is a leap year. False otherwise.

func ItemInList

func ItemInList(arg string, argList []string) bool

ItemInList checks if a given string (arg) is present in a list of strings (argList). Returns true if found, false otherwise.

func JsonBytesReindent added in v1.1.9

func JsonBytesReindent(jsonBytes []byte, indent int) (jsonBytes2 []byte, err error)

Convert JSON interface object to byte slice, with option to indent spacing

func JsonBytesToJsonObj added in v1.3.3

func JsonBytesToJsonObj(jsonBytes []byte) (jsonObject interface{}, err error)

Convert JSON byte slice to JSON interface object, with default 2-space indentation

func JsonToBytes added in v1.1.9

func JsonToBytes(jsonObject interface{}) (jsonBytes []byte, err error)

Convert JSON interface object to byte slice, with default 2-space indentation

func JsonToBytesIndent added in v1.1.9

func JsonToBytesIndent(jsonObject interface{}, indent int) (jsonBytes []byte, err error)

Convert JSON interface object to byte slice, with option to indent spacing

func LastElem

func LastElem(s, splitter string) string

Split the string and return last element

func LoadFileJson

func LoadFileJson(filePath string) (jsonObject interface{}, err error)

Reads, load, and decode given filePath as a JSON object text file. Returns JSON object and err if any.

func LoadFileJsonGzip added in v1.2.0

func LoadFileJsonGzip(filePath string) (jsonObject interface{}, err error)

Reads, load, and decode given filePath as a gzipped JSON object text file. Returns JSON object and err if any. The compression helps speed things up for some very large objects.

func LoadFileText added in v1.3.4

func LoadFileText(filePath string) (rawBytes []byte, err error)

Read and recode given filePath as text byte slice. Returns the byte slice and error if any.

func LoadFileYaml

func LoadFileYaml(filePath string) (yamlObject interface{}, err error)

Trys to read, load, and decode given filePath as some YAML object. Returns YAML object and error if any.

func LoadFileYamlBytes added in v1.1.8

func LoadFileYamlBytes(filePath string) (yamlBytes []byte, err error)

Tries to read, load, and recode given filePath as some YAML object as byte slice. Returns YAML object as byte slice. and error if any.

func MergeMaps

func MergeMaps(m1, m2 map[string]string) (result map[string]string)

Combines two string-to-string maps, with keys from the second map overwriting those from the first if duplicates exist. Returns the merged map.

func MergeObjects

func MergeObjects(x, y map[string]interface{}) (obj map[string]interface{})

Non-recursive merge of first-level attributes in JSON object y onto object x If attribute exists in y, it is overwritten

func PadSpaces

func PadSpaces(targetWidth, stringWidth int) string

Return string of spaces for padded printing. Needed when printing terminal colors. Colorize output uses % sequences that conflict with Printf's own formatting with %

func PostSpc added in v1.1.11

func PostSpc(value interface{}, width int) string

Return value as a string, padded with trailing spaces, totalling width size wide. This is needed when printing terminal colors, because they conflict with Printf's own '%' formatting

func PreSpc added in v1.1.11

func PreSpc(value interface{}, width int) string

Return value as a string, padded with leading spaces, totalling width size wide. This is needed when printing terminal colors, because they conflict with Printf's own '%' formatting

func Prettify

func Prettify(jsonObject interface{}) (pretty string, err error)

NOTE: To be replaced by JsonToBytes()

func PrintColorSamples added in v1.0.14

func PrintColorSamples()

PrintColorSamples is a dormant function and not actively used anywhere. It is left around for those times when someone wants to test and see what the colors would look like.

func PrintDays added in v1.1.2

func PrintDays(days int64)

Print number of days, also in years and days

func PrintJson

func PrintJson(jsonObject interface{})

Prints JSON object, flushing the output buffer

func PrintJsonBytesColor added in v1.1.9

func PrintJsonBytesColor(jsonBytes []byte)

Prints JSON byte slice in color. Just an alias of yaml.go:PrintYamlBytesColor().

func PrintJsonColor added in v1.1.9

func PrintJsonColor(jsonObject interface{})

Print JSON object in color

func PrintYaml

func PrintYaml(yamlObject interface{})

Print YAML object

func PrintYamlBytesColor added in v1.1.8

func PrintYamlBytesColor(yamlBytes []byte)

Print YAML bytes in color, includes comments Also prints JSON byte slice in color, but use json.go:PrintJsonBytesColor() alias instead. Caller must ensure yamlBytes is proper YAML/JSON

func PrintYamlColor added in v1.1.8

func PrintYamlColor(yamlObject interface{})

Print YAML object (that don't usually include comments) in color

func PromptMsg added in v1.0.13

func PromptMsg(msg string) rune

Print prompt message and return single rune character input

func RemoveFile

func RemoveFile(filePath string)

Removes given filepath

func SameType

func SameType(a, b interface{}) bool

Check if two variables are of the same type

func SaveFileJson

func SaveFileJson(jsonObject interface{}, filePath string)

Save given JSON object as text file

func SaveFileJsonGzip added in v1.2.0

func SaveFileJsonGzip(jsonObject interface{}, filePath string)

Save given JSON object as gzipped text file

func SaveFileText added in v1.3.4

func SaveFileText(filePath string, rawBytes []byte) error

Saves given byte slice as text file. Returns error is any.

func SaveFileYaml

func SaveFileYaml(yamlObject interface{}, filePath string)

Save given YAML object to given filePath

func SortMapStringKeys added in v1.1.1

func SortMapStringKeys(obj map[string]string) (sortedKeys []string)

Return the map string object's keys sorted

func SortObjStringKeys added in v1.1.1

func SortObjStringKeys(obj map[string]interface{}) (sortedKeys []string)

Return the object's keys sorted

func Str added in v1.0.2

func Str(x interface{}) string

Return the best printable string value for given x variable

func StrSingleQuote added in v1.3.2

func StrSingleQuote(x interface{}) string

Returns string value of unknown variable, wrapped in single quotes. This is a special function to lookout for leading '*', which YAML does not allow and must be single-quoted

func StringInJson added in v1.3.0

func StringInJson(jsonObject interface{}, filter string) bool

Recursive function returns True if filter string value is anywhere within jsonObject

func StringToInt64 added in v1.1.2

func StringToInt64(s string) (int64, error)

Converts string number to int64

func SubString

func SubString(large, small string) bool

Case insensitive substring check

func ToStr added in v1.1.11

func ToStr(value interface{}) string

Converts any value to its string representation using default formatting.

func Trace

func Trace() string

Returns returns a string showing the current filepath line number and function name. See https://stackoverflow.com/questions/25927660/how-to-get-the-current-function-name

func ValidDate added in v1.1.4

func ValidDate(dateString, expectedFormat string) bool

Check if string is a valid date in expectedFormat. Return true if so, false otherwise. See https://pkg.go.dev/time

func ValidIpStr added in v1.1.12

func ValidIpStr(ipStr string) bool

Returns true if given string is a valid IP address. False otherwise.

func ValidUuid

func ValidUuid(s string) bool

Returns true if given string is a valid UUID number. False otherwise.

func YamlToBytes added in v1.1.9

func YamlToBytes(yamlObject interface{}) (yamlBytes []byte, err error)

With default 2 space indent

func YamlToBytesIndent added in v1.1.9

func YamlToBytesIndent(yamlObject interface{}, indent int) (yamlBytes []byte, err error)

Convert YAML interface object to byte slice, with option indent spacing

Types

This section is empty.

Jump to

Keyboard shortcuts

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