golang_utils

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 26 Imported by: 0

README

golang-utils

A go module containing various utility functions and capabilities

Documentation

Index

Constants

View Source
const (
	// Starting - The application is in the process of bootstrapping and is not fully operational
	Starting = iota
	// Running - The application is fully loaded/bootstrapped and is executing normally
	Running
	// Stopping - The application is stopping or shutting down
	Stopping
	// Stopped - The application is fully stopped or shutdown. Are you ever going to actually be able to see this state?
	Stopped
	// Paused - The application is in a state of suspended animation. It is up and can be restarted instantly but is not consuming CPU
	Paused
	// Errored - The application has suffered an error or exception. It is not running normally.
	Errored
)
View Source
const (
	QUOTE_FUNC_NAME      = "quote"
	EMPTY_FUNC_NAME      = "empty"
	NOT_EMPTY_FUNC_NAME  = "notEmpty"
	OR_DEFAULT_FUNC_NAME = "orDefault"
	OR_EMPTY_FUNC_NAME   = "orEmpty"
	TERNARY_FUNC_NAME    = "ternary"
)
View Source
const JSON string = "json"
View Source
const PathSeparator = string(os.PathSeparator)
View Source
const YAML string = "yaml"
View Source
const YML string = "yml"

Variables

This section is empty.

Functions

func AddAll

func AddAll[K comparable, V any](toMap map[K]V, fromMap map[K]V)

func AddIfNotExist

func AddIfNotExist[K comparable, V any](key K, value V, target map[K]V)

func CheckAndCreateDir

func CheckAndCreateDir(directoryPath string)

func CheckError

func CheckError(err error, msg string, args ...any)

func Contains

func Contains[T comparable](list []T, value T) bool

func ContainsElement

func ContainsElement[T Comparable](list []T, value T) bool

func ContainsItemsWithPrefix

func ContainsItemsWithPrefix(list []string, value string) bool

func Count

func Count[K comparable, V any](target map[K]V) (cnt int)

func CreateDirIfNotExist

func CreateDirIfNotExist(directoryPath string) (existed bool, err error)

func CreateFile

func CreateFile(filename string, dir string) (file *os.File)

func DeleteFileOrDir

func DeleteFileOrDir(path string)

func Empty

func Empty(input any) bool

func Escape

func Escape(value string) string

func EscapeSpecials

func EscapeSpecials(target string) string

func FileIsJson

func FileIsJson(filename string) bool

func FileIsYaml

func FileIsYaml(filename string) bool

func GenMarkdownCustom

func GenMarkdownCustom(
	cmd *cobra.Command,
	w io.Writer,
	filename string,
	linkHandler func(string, string) string,
) error

GenMarkdownCustom creates custom markdown output.

func GenMarkdownTreeCustom

func GenMarkdownTreeCustom(
	cmd *cobra.Command,
	dir string,
	filePrepender func(string) string,
	linkHandler func(string, string) string,
) error

GenMarkdownTreeCustom is the same as GenMarkdownTree, but with custom filePrepender and linkHandler.

func Get

func Get[K comparable, V any](key K, target map[K]V) V

func GetAbsPath

func GetAbsPath(path string) string

func GetConfigBool

func GetConfigBool(key string) bool

func GetConfigFloat

func GetConfigFloat(key string) float64

func GetConfigIntWithDefault

func GetConfigIntWithDefault(key string, defaultValue int) int

func GetConfigList

func GetConfigList(key string) []string

func GetConfigString

func GetConfigString(key string) string

func GetFullCmdName

func GetFullCmdName(cmd *cobra.Command) string

func GetPointer

func GetPointer[V any](value V) *V

func GetString

func GetString(key string, target map[string]any) string

func HasKey

func HasKey[K comparable, V any](key K, target map[K]V) bool

func InitCommands

func InitCommands(initFunction ...func())

func IsDash

func IsDash(character rune) bool

func IsEmpty

func IsEmpty(value string) bool

func IsInt

func IsInt(value string) bool

func IsNotEmpty

func IsNotEmpty(value string) bool

func LineWrap

func LineWrap(s string, length int) string

func LoadPropertiesMapYamlToMap

func LoadPropertiesMapYamlToMap(filename, path string, properties *Properties)

func LoadYamlFileToStruct

func LoadYamlFileToStruct[T any](path string) (*T, error)

func LogError

func LogError(err error, msg string, args ...any)

func LogErrorTrace

func LogErrorTrace(err error, msg string, args ...any)

func MakeFlagRequired

func MakeFlagRequired(cmd *cobra.Command, flagName string)

func Merge

func Merge[K comparable, V any](baseMap map[K]V, overridingMap map[K]V) map[K]V

func NotEmpty

func NotEmpty(input any) bool

func OpenFileAtPath

func OpenFileAtPath(path string, homepath string) (reader *os.File, err error)

func OrDefault

func OrDefault(defaulVal string, input any) any

func OrEmpty

func OrEmpty(input any) any

func PathExists

func PathExists(filePath string) (exists bool)

func Print

func Print[K comparable, V any](target map[K]V, msg string, args ...any)

func PrintPadded

func PrintPadded[K comparable, V any](target map[K]V, depth int, msg string, args ...any)

func Quote

func Quote(input any) string

func RandomWaitMillis

func RandomWaitMillis(max int)

func Remove

func Remove[K comparable, V any](key K, target map[K]V) bool

func RemoveDir

func RemoveDir(path string)

func RemoveLineEndings

func RemoveLineEndings(value string) string

func RemoveNonPrintingUnicode

func RemoveNonPrintingUnicode(value string) string

func SliceIsEmpty

func SliceIsEmpty[T comparable](list []T) bool

func StripPathSeparators

func StripPathSeparators(value string) string

func Ternary

func Ternary(condition bool, trueOut any, falseOut any) any

func ThrowError

func ThrowError(msg string, args ...any)

func ToString

func ToString(value int) string

func WriteStringContentsToFile

func WriteStringContentsToFile(path string, contents string, force bool) error

func WriteStructToFile

func WriteStructToFile(target any, filename string, path string)

func WriteStructsToFile

func WriteStructsToFile(target any, filename string, path string, createDir bool) (exportCnt int)

Types

type ApplicationState

type ApplicationState int

ApplicationState - States of an application.

type CmdConfig

type CmdConfig struct {
	// contains filtered or unexported fields
}

func CommandBuilder

func CommandBuilder(use string) *CmdConfig

func (*CmdConfig) Build

func (cc *CmdConfig) Build() *cobra.Command

func (*CmdConfig) DisableTracking

func (cc *CmdConfig) DisableTracking() *CmdConfig

func (*CmdConfig) EnableTracking

func (cc *CmdConfig) EnableTracking() *CmdConfig

func (*CmdConfig) SetAliases

func (cc *CmdConfig) SetAliases(alias ...string) *CmdConfig

func (*CmdConfig) SetArgValidations

func (cc *CmdConfig) SetArgValidations(argValidations cobra.PositionalArgs) *CmdConfig

func (*CmdConfig) SetLongDescription

func (cc *CmdConfig) SetLongDescription(desc string) *CmdConfig

func (*CmdConfig) SetPersistentPostRun

func (cc *CmdConfig) SetPersistentPostRun(cmdFunc CmdFunc) *CmdConfig

func (*CmdConfig) SetPersistentPreRun

func (cc *CmdConfig) SetPersistentPreRun(cmdFunc CmdFunc) *CmdConfig

func (*CmdConfig) SetPostRun

func (cc *CmdConfig) SetPostRun(cmdFunc CmdFunc) *CmdConfig

func (*CmdConfig) SetPreRun

func (cc *CmdConfig) SetPreRun(cmdFunc CmdFunc) *CmdConfig

func (*CmdConfig) SetRun

func (cc *CmdConfig) SetRun(cmdFunc CmdFunc) *CmdConfig

func (*CmdConfig) SetShortDescription

func (cc *CmdConfig) SetShortDescription(desc string) *CmdConfig

func (*CmdConfig) SetVersion

func (cc *CmdConfig) SetVersion(version string) *CmdConfig

type CmdFunc

type CmdFunc func(cmd *cobra.Command, args []string)

type Comparable

type Comparable interface {
	Compare(value any) bool
}

type Configuration

type Configuration struct {
	LoadedFrom string

	RunID uint
	// contains filtered or unexported fields
}

func NewConfiguration

func NewConfiguration(filename string, writeInHome bool, defaults Properties) *Configuration

func (*Configuration) Default

func (c *Configuration) Default()

func (*Configuration) DeleteFromMap

func (c *Configuration) DeleteFromMap(propertyName string, key string)

func (*Configuration) Get

func (c *Configuration) Get(propertyName string) string

func (*Configuration) GetBool

func (c *Configuration) GetBool(propertyName string) bool

func (*Configuration) GetFloat

func (c *Configuration) GetFloat(propertyName string) float64

func (*Configuration) GetInt

func (c *Configuration) GetInt(propertyName string) int

func (*Configuration) GetIntWithDefault

func (c *Configuration) GetIntWithDefault(propertyName string, defaultValue int) int

func (*Configuration) GetList

func (c *Configuration) GetList(propertyName string) []string

func (*Configuration) GetMap

func (c *Configuration) GetMap(propertyName string) map[string]string

func (*Configuration) HasResource

func (c *Configuration) HasResource(name string, property string) bool

func (*Configuration) Load

func (c *Configuration) Load(cfgFile string)

func (*Configuration) Print

func (c *Configuration) Print()

func (*Configuration) PrintMapProperty

func (c *Configuration) PrintMapProperty(propertyName string)

func (*Configuration) Update

func (c *Configuration) Update(propertyName string, value string)

func (*Configuration) UpdateList

func (c *Configuration) UpdateList(propertyName string, value string)

func (*Configuration) UpdateMap

func (c *Configuration) UpdateMap(propertyName string, key string, value string)

func (*Configuration) Write

func (c *Configuration) Write(msg string, args ...string)

type Event

type Event struct {
	Name     string
	Type     string
	Category string
	Data     Properties
}

type FileDecoder

type FileDecoder interface {
	Decode(v any) error
}

type FileEncoder

type FileEncoder interface {
	Encode(v any) (err error)
}

type FileInfo

type FileInfo struct {
	Name        string
	BaseAbsPath string
	Info        os.FileInfo
	IsDir       bool
	FileHandle  *os.File
}

func CreateDir

func CreateDir(path string) *FileInfo

func CreateTempFile

func CreateTempFile(dir string, filename string) *FileInfo

func CreateUniqueTempDir

func CreateUniqueTempDir(parentDir string) *FileInfo

func GetDirsAtPath

func GetDirsAtPath(path string, createIfNotExit bool) (results []*FileInfo)

func GetFilesAtPath

func GetFilesAtPath(path string, excludeDirs bool) (results []*FileInfo)

func GetWorkingDir

func GetWorkingDir() *FileInfo

func NewFileInfo

func NewFileInfo(name string, absPath string) *FileInfo

func NewFileInfoFromPath

func NewFileInfoFromPath(path string) *FileInfo

func (*FileInfo) AbsFilePath

func (fi *FileInfo) AbsFilePath() string

func (*FileInfo) Close

func (fi *FileInfo) Close()

func (*FileInfo) Create

func (fi *FileInfo) Create() *FileInfo

func (*FileInfo) Exists

func (fi *FileInfo) Exists() bool

func (*FileInfo) GetFileInfo

func (fi *FileInfo) GetFileInfo() os.FileInfo

func (*FileInfo) MoveToPath

func (fi *FileInfo) MoveToPath(path string)

func (*FileInfo) Open

func (fi *FileInfo) Open()

func (*FileInfo) OpenForWriting

func (fi *FileInfo) OpenForWriting(truncate bool)

func (*FileInfo) ReadFully

func (fi *FileInfo) ReadFully() []byte

func (*FileInfo) WriteFile

func (fi *FileInfo) WriteFile(data []byte)

type OverridableProperties

type OverridableProperties struct {
	FileName string
	KeyPath  string
	Path     string
	Contents Properties
	Children []*OverridableProperties
	Parent   *OverridableProperties
}

func NewOverridableProperties

func NewOverridableProperties(filename string, path string) *OverridableProperties

func NewOverriddenProperties

func NewOverriddenProperties(properties *OverridableProperties) *OverridableProperties

func (*OverridableProperties) Add

Build Relationships

func (*OverridableProperties) GetGrandestParent

func (c *OverridableProperties) GetGrandestParent() *OverridableProperties

func (*OverridableProperties) GetKeyPath

func (c *OverridableProperties) GetKeyPath() string

func (*OverridableProperties) GetOverriddenProperties

func (c *OverridableProperties) GetOverriddenProperties() Properties

type PersistenceConfig

type PersistenceConfig struct {
	Name     string
	Path     string
	Entities []any
}

func NewPersistenceConfig

func NewPersistenceConfig(dbName, path string, entities []any) *PersistenceConfig

type PersistenceContext

type PersistenceContext struct {
	DB     *gorm.DB //Do I really need this?
	DBFile *FileInfo
	// contains filtered or unexported fields
}

func NewPersistenceContext

func NewPersistenceContext(config *PersistenceConfig) *PersistenceContext

func (*PersistenceContext) Create

func (c *PersistenceContext) Create(value any)

func (*PersistenceContext) InitDB

func (c *PersistenceContext) InitDB()

func (*PersistenceContext) OmitFields

func (c *PersistenceContext) OmitFields(omitted ...string) *gorm.DB

func (*PersistenceContext) OpenDB

func (c *PersistenceContext) OpenDB()

func (*PersistenceContext) PopulateReferenceData

func (c *PersistenceContext) PopulateReferenceData()

func (*PersistenceContext) Save

func (c *PersistenceContext) Save(value any)

func (*PersistenceContext) SaveFull

func (c *PersistenceContext) SaveFull(value any)

func (*PersistenceContext) SaveOmitting

func (c *PersistenceContext) SaveOmitting(value any, omitted ...string)

type Properties

type Properties map[string]any

func (Properties) Remove

func (p Properties) Remove(key string) bool

type PropertyManager

type PropertyManager struct {
	// contains filtered or unexported fields
}

func NewPropertyManager

func NewPropertyManager() *PropertyManager

func (*PropertyManager) Add

func (p *PropertyManager) Add(path string, props *OverridableProperties)

func (*PropertyManager) Get

func (*PropertyManager) GetFirstSetWithSuffix

func (p *PropertyManager) GetFirstSetWithSuffix(suffix string) *OverridableProperties

func (*PropertyManager) GetSetsWithSuffix

func (p *PropertyManager) GetSetsWithSuffix(suffix string) (found []*OverridableProperties)

func (*PropertyManager) Has

func (p *PropertyManager) Has(path string) bool

func (*PropertyManager) SetCount

func (p *PropertyManager) SetCount() int

type Regex

type Regex struct {
	Pattern string
	Regex   *regexp.Regexp
	// contains filtered or unexported fields
}

func NewRegex

func NewRegex(pattern string) *Regex

func (*Regex) Init

func (r *Regex) Init()

func (*Regex) IsValid added in v0.0.5

func (r *Regex) IsValid() bool

func (*Regex) Matches

func (r *Regex) Matches(target string) bool

type State

type State struct {
	sync.Mutex //Used for locking state to ensure safe concurrent access

	PersistenceContext *PersistenceContext
	// contains filtered or unexported fields
}

func CurrentState

func CurrentState() *State

func (*State) AppendDataDir

func (s *State) AppendDataDir(appendPath string) *State

func (*State) CheckError

func (s *State) CheckError(terminate bool, err error, msg string, args ...any)

func (*State) Config

func (s *State) Config() *Configuration

func (*State) DataDir

func (s *State) DataDir() string

func (*State) DefaultConfig

func (s *State) DefaultConfig() *State

func (*State) Duration

func (s *State) Duration() time.Duration

func (*State) EnableBaseDataDir

func (s *State) EnableBaseDataDir(path string) *State

func (*State) EnablePersistence

func (s *State) EnablePersistence(config *PersistenceConfig) *State

func (*State) EnableTempDir

func (s *State) EnableTempDir() *State

func (*State) Errored

func (s *State) Errored() bool

func (*State) FullCommand

func (s *State) FullCommand() string

func (*State) HomeDir

func (s *State) HomeDir() string

func (*State) InitConfig

func (s *State) InitConfig(defaults Properties) *State

func (*State) PrintConfig

func (s *State) PrintConfig() *State

func (*State) PrintVersion

func (s *State) PrintVersion()

func (*State) RemoveFromState

func (s *State) RemoveFromState(name string) *State

func (*State) SetAppName

func (s *State) SetAppName(name string) *State

func (*State) SetBuildDate

func (s *State) SetBuildDate(date string) *State

func (*State) SetCmd

func (s *State) SetCmd(cmd *cobra.Command) *State

func (*State) SetCommitSha

func (s *State) SetCommitSha(sha string) *State

func (*State) SetConfigFileName

func (s *State) SetConfigFileName(name string) *State

func (*State) SetLogging

func (s *State) SetLogging(verbose bool) *State

func (*State) SetState

func (s *State) SetState(newState ApplicationState) *State

func (*State) SetTempDir

func (s *State) SetTempDir(path string) *State

func (*State) SetVersion

func (s *State) SetVersion(version string) *State

func (*State) StartTime

func (s *State) StartTime() time.Time

func (*State) StopTime

func (s *State) StopTime() time.Time

func (*State) TempDir

func (s *State) TempDir() string

func (*State) TrackCmd

func (s *State) TrackCmd(cmd *cobra.Command, _ []string)

func (*State) UpdateConfigProperty

func (s *State) UpdateConfigProperty(property, value string) *State

func (*State) UpdateState

func (s *State) UpdateState(name string, value any) *State

func (*State) UseHomeDir

func (s *State) UseHomeDir(uhd bool) *State

func (*State) User

func (s *State) User() string

func (*State) Verbose

func (s *State) Verbose() bool

func (*State) WorkDir

func (s *State) WorkDir() string

Jump to

Keyboard shortcuts

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