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 ¶
- Variables
- func BytesToYamlObject(yamlBytes []byte) (yamlObject interface{}, err error)
- func ConvertDateFormat(dateString, srcFormat, dstFormat string) (string, error)
- func DateStringToEpocInt64(dateString, dateFormat string) (int64, error)
- func Die(format string, args ...interface{})
- func EpocInt64ToTime(epocInt int64) time.Time
- func EpocStringToTime(epocString string) (time.Time, error)
- func FileAge(filePath string) int64
- func FileExist(filePath string) (e bool)
- func FileModTime(filePath string) int
- func FileNotExist(filePath string) (e bool)
- func FileSize(filePath string) int64
- func FileUsable(filePath string) (e bool)
- func FirstN(s string, n int) string
- func GetDateInDays(days string) time.Time
- func GetDaysBetween(date1, date2 string) int64
- func GetDaysSinceOrTo(date1 string) int64
- func GetType(v interface{}) string
- func Int64Abs(x int64) int64
- func Int64ToString(i int64) string
- func IntAbs(x int) int
- func InternetIsAvailable() bool
- func InternetNotAvailable() bool
- func IsAlpha(c rune) bool
- func IsDigit(c rune) bool
- func IsHexDigit(c rune) bool
- func IsIpPortStrReachable(ipPortStr string) bool
- func IsLeapYear(year int64) bool
- func ItemInList(arg string, argList []string) bool
- func JsonBytesReindent(jsonBytes []byte, indent int) (jsonBytes2 []byte, err error)
- func JsonBytesToJsonObj(jsonBytes []byte) (jsonObject interface{}, err error)
- func JsonToBytes(jsonObject interface{}) (jsonBytes []byte, err error)
- func JsonToBytesIndent(jsonObject interface{}, indent int) (jsonBytes []byte, err error)
- func LastElem(s, splitter string) string
- func LoadFileJson(filePath string) (jsonObject interface{}, err error)
- func LoadFileJsonGzip(filePath string) (jsonObject interface{}, err error)
- func LoadFileText(filePath string) (rawBytes []byte, err error)
- func LoadFileYaml(filePath string) (yamlObject interface{}, err error)
- func LoadFileYamlBytes(filePath string) (yamlBytes []byte, err error)
- func MergeMaps(m1, m2 map[string]string) (result map[string]string)
- func MergeObjects(x, y map[string]interface{}) (obj map[string]interface{})
- func PadSpaces(targetWidth, stringWidth int) string
- func PostSpc(value interface{}, width int) string
- func PreSpc(value interface{}, width int) string
- func Prettify(jsonObject interface{}) (pretty string, err error)
- func PrintColorSamples()
- func PrintDays(days int64)
- func PrintJson(jsonObject interface{})
- func PrintJsonBytesColor(jsonBytes []byte)
- func PrintJsonColor(jsonObject interface{})
- func PrintYaml(yamlObject interface{})
- func PrintYamlBytesColor(yamlBytes []byte)
- func PrintYamlColor(yamlObject interface{})
- func PromptMsg(msg string) rune
- func RemoveFile(filePath string)
- func SameType(a, b interface{}) bool
- func SaveFileJson(jsonObject interface{}, filePath string)
- func SaveFileJsonGzip(jsonObject interface{}, filePath string)
- func SaveFileText(filePath string, rawBytes []byte) error
- func SaveFileYaml(yamlObject interface{}, filePath string)
- func SortMapStringKeys(obj map[string]string) (sortedKeys []string)
- func SortObjStringKeys(obj map[string]interface{}) (sortedKeys []string)
- func Str(x interface{}) string
- func StrSingleQuote(x interface{}) string
- func StringInJson(jsonObject interface{}, filter string) bool
- func StringToInt64(s string) (int64, error)
- func SubString(large, small string) bool
- func ToStr(value interface{}) string
- func Trace() string
- func ValidDate(dateString, expectedFormat string) bool
- func ValidIpStr(ipStr string) bool
- func ValidUuid(s string) bool
- func YamlToBytes(yamlObject interface{}) (yamlBytes []byte, err error)
- func YamlToBytesIndent(yamlObject interface{}, indent int) (yamlBytes []byte, err error)
Constants ¶
This section is empty.
Variables ¶
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
Convert byte slice to YAML interface object
func ConvertDateFormat ¶ added in v1.1.2
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
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
Converts an epoch timestamp in int64 format to a time.Time object.
func EpocStringToTime ¶ added in v1.1.2
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 FileModTime ¶
Returns given filePath modified time in Unix epoch int
func FileNotExist ¶
Returns true is given filePath does not exist. False otherwise.
func FileUsable ¶
Returns true if filepath exists and has some content. False otherwise.
func GetDateInDays ¶ added in v1.1.8
Print yyyy-mm-dd date for given number of +/- days in future or past
func GetDaysBetween ¶ added in v1.1.8
Return number of days between 2 dates
func GetDaysSinceOrTo ¶ added in v1.1.8
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 Int64ToString ¶ added in v1.1.8
Converts int64 number to string
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
Checks if given rune is an alphabetic character (either uppercase or lowercase). Returns true if it is, false otherwise.
func IsHexDigit ¶ added in v1.0.6
Returns true if rune is a hexadeximal digit. False otherwise.
func IsIpPortStrReachable ¶ added in v1.1.12
Checks if IP_Address:Port string is reachable
func IsLeapYear ¶ added in v1.1.8
Returns true if given year is a leap year. False otherwise.
func ItemInList ¶
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
Convert JSON interface object to byte slice, with option to indent spacing
func JsonBytesToJsonObj ¶ added in v1.3.3
Convert JSON byte slice to JSON interface object, with default 2-space indentation
func JsonToBytes ¶ added in v1.1.9
Convert JSON interface object to byte slice, with default 2-space indentation
func JsonToBytesIndent ¶ added in v1.1.9
Convert JSON interface object to byte slice, with option to indent spacing
func LoadFileJson ¶
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
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
Read and recode given filePath as text byte slice. Returns the byte slice and error if any.
func LoadFileYaml ¶
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
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 ¶
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 ¶
Non-recursive merge of first-level attributes in JSON object y onto object x If attribute exists in y, it is overwritten
func PadSpaces ¶
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
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
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 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 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 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
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
Return the map string object's keys sorted
func SortObjStringKeys ¶ added in v1.1.1
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
Recursive function returns True if filter string value is anywhere within jsonObject
func StringToInt64 ¶ added in v1.1.2
Converts string number to int64
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
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
Returns true if given string is a valid IP address. False otherwise.
func YamlToBytes ¶ added in v1.1.9
With default 2 space indent
func YamlToBytesIndent ¶ added in v1.1.9
Convert YAML interface object to byte slice, with option indent spacing
Types ¶
This section is empty.