utils

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package utils implements helper functions

Index

Constants

View Source
const (
	IgnoreRepeats = iota
	IgnoreOrdering
	ConsiderOrdering
)

Variables

View Source
var DevMode = strings.ToUpper(os.Getenv("ENVIRONMENT")) == "DEV"

Functions

func Capture

func Capture(err error, _panic bool) sentry.EventID

Handles an error by capturing it on Sentry and logging the same on STDOUT

func CaptureWithContext

func CaptureWithContext(c context.Context, err error, _panic bool) sentry.EventID

Handles an error by capturing it on Sentry and logging the same on STDOUT

func CloseAnythingSafely

func CloseAnythingSafely(toClose interface{})

Closes an interface which implements io.Closer safely

func CloseSafely

func CloseSafely(closeable io.Closer)

Closes a struct which implements io.Closer safely

func Concat

func Concat(slice ...interface{}) string

Concatenates a variable slice of strings

func ConvertInterfaceToString

func ConvertInterfaceToString(val interface{}) (string, bool)

ConvertInterfaceToString takes an interface as input and tries to assert it as a string if not nil. It returns the string value after the said assertion. False is returned if assertion fails

func Distinct

func Distinct(slice []string) []string

Gets a slice of distince strings from a slice of strings

func DownloadFile

func DownloadFile(ctx context.Context, url, filepath string) (err error)

DownloadFile - Download from url to a local file

func Evaluate

func Evaluate(templateText string, metadata map[string]interface{}) string

Evaluate ...

func EvaluateAugmentedFuncMap

func EvaluateAugmentedFuncMap(templateText string, metadata map[string]interface{}, additionalFuncs template.FuncMap) string

func EvaluateAugmentedFuncMapSafely

func EvaluateAugmentedFuncMapSafely(templateText string, metadata map[string]interface{}, additionalFuncs template.FuncMap) (string, error)

func FlattenStringSlice

func FlattenStringSlice(nestedList []interface{}) (slice []string)

flattens a slice of []interface{} to a []string. The flattening is done only for one level of nesting currently

func GetEnv

func GetEnv(key, fallback string) string

GetEnv - Return the fallback env if not present

func GetFile

func GetFile(fileURLPath, toFile string) (err error)

GetFile - Download file from URL, create directories/file and write to it TODO: Allow cancelable requests using contexts https://github.com/hashicorp/go-getter/issues/102

func HTTPGet

func HTTPGet(url string) (payloadBytes []byte, err error)

HTTPGet - Download from url to a bytes array

func HTTPGetWithCACrt

func HTTPGetWithCACrt(pathToCaCertFile string, url string) (payloadBytes []byte, err error)

HTTPGetWithCACrt - Download from url to a bytes array using CA crt pathToCaCertFile = Mount path of the CAcrt file

func IntInSlice

func IntInSlice(a int, list []int) bool

StringInSlice - Returns True when two strings have one element in common, False otherwise

func IsDeepEqual

func IsDeepEqual(x, y interface{}) bool

IsDeepEqual - Returns True when the two passed values are same, else False

func IsEqual

func IsEqual(x, y interface{}) bool

IsEqual - Returns True when the two passed values are same, else False

func IsStringSliceEqual

func IsStringSliceEqual(a, b []string, option int) bool

Checks if a string slice is equal or not.

func IsZeroOfUnderlyingType

func IsZeroOfUnderlyingType(x interface{}) bool

IsZeroOfUnderlyingType - Returns True when the passed value is zero

func Join

func Join(slice []string, delimiter string) string

Concatenates a slice of strings with the delimiter in question

func JoinInt

func JoinInt(delimiter string, slice []int) string

Concatenates a variable slice of strings

func MD5

func MD5(str string) string

MD5 - Generate MD5 hash for the given string

func ReadCsvFile

func ReadCsvFile(filePath string) ([][]string, error)

ReadCsvFile - Read from csv file and return results as [][]string

func ReadYamlFile

func ReadYamlFile(filePath string, out interface{}) (err error)

ReadYamlFile - Read from YAML file and return results

func Reverse

func Reverse(s string) string

Reverse - To reverse any string

func SliceInSlice

func SliceInSlice(a []string, list []string) bool

SliceInSlice - Returns True when two []string have one element in common, False otherwise

func StringInSlice

func StringInSlice(a string, list []string) bool

StringInSlice - Returns True when two strings have one element in common, False otherwise

func StringListify

func StringListify(slice []string, delimiter, finalDelimiter string) string

Concatenates a slice of strings with the delimiter in question Uses the final delimiter to concatenate the final string in the slice

func StringToTimestamp

func StringToTimestamp(unixTimestamp string) (time.Time, error)

StringToTimestamp - Converts a unix timestamp string to unix timestamp format

func StringifyToJson

func StringifyToJson(i interface{}) (stringifiedJson string)

func TimeTrack

func TimeTrack(start time.Time)

Prints the time taken to run a function. Should be used to measure performance. Usage: Add "defer TimeTrack(time.Now())" as a statement at the beginning of any function

func ToCamelCase

func ToCamelCase(str string) string

func ToString

func ToString(toConvert interface{}) string

func TrimSuffix

func TrimSuffix(s, suffix string) string

TrimSuffix - To trim a suffix string

func WriteToFile

func WriteToFile(stream []byte, toFile string) (file *os.File, err error)

WriteToFile - Create directories/file and write to it

Types

This section is empty.

Jump to

Keyboard shortcuts

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