cruncy

package module
v0.0.0-...-f5ecbed Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 23 Imported by: 0

README

Simple to use go rutines

This is a collection of the most often used go rutines in my commandline applications and micro services.

Changelog

  • 0.14.1 Added debug waitgroup
  • 0.12.3 Added Lincence dependencies and information
  • 0.11.1 Added error returns to CliOption.ReadConfig
  • 0.10.7 Added setif env operators
  • 0.10.3 Added back foreach
  • 0.10.2 Added list bucket
  • 0.10.1 Adding list buckets
  • 0.9.4 Added foreach to store,Store fix
  • 0.9.2 Addin support for batchtick, Work on store, Updated makefile, Added more common file functions
  • 0.9.1 Added common file operations and easy to use WriteHeapFile, Updated log statements
  • 0.8.2 Adding minute flows
  • 0.8.1 Adding batch size to timer
  • 0.6.3 Added cli tests for app options
  • 0.7.2 Added sep logger
  • 0.7.1 Added output of fields
  • 0.6.7 Added public get on counter
  • 0.6.6 Adding concurrency guards
  • 0.6.5 Added locks on BatchShow and TotalShow
  • 0.6.4 Cleanup in timer
  • 0.6.3 Updated timer with dock and fixed ref to struct
  • 0.6.2 Updated readme, Adding app option helpers
  • 0.6.1 Removed dep info. Changed short uuid to ksuid
  • 0.5.2 Added readconfigfile, Sirupsen update
  • 0.5.1 Added parly_db to the lib, incremented version to 0.5 but still backward comptible
  • 0.4.8 Adding convinience methods
  • 0.4.5 Adding cli options to standard lib, Adding dep info
  • 0.4.3 Moving to lowercase sirupsen, Adding env ovveride flag, Updated makefile

Data

  • Author: leffen
  • Date: 2017 and onward
  • Licence: Check LICENSE AND NOTICE.txt file

Documentation

Index

Constants

View Source
const VERSION = "0.14.5"

VERSION of the application

Variables

View Source
var (
	// ErrNotFound error key not found
	ErrNotFound = errors.New("store: key not found")
	// ErrBadValue error bad value
	ErrBadValue = errors.New("store: bad value")
)

Functions

func CreateDirUnlessExists

func CreateDirUnlessExists(path string)

CreateDirUnlessExists creates a directory if to do not exist

func Decompress

func Decompress(fileName string) (string, error)

Decompress decompresses a file using os utility gzip

func DeleteFile

func DeleteFile(fileName string) error

DeleteFile deletes a file

func DiskUsage

func DiskUsage(path string, checkSize uint64) float64

DiskUsage disk usage of path/disk

func DoCmd

func DoCmd(command string, arg ...string) error

DoCmd Runs a os command

func EnsureFileSave

func EnsureFileSave(fileName string)

EnsureFileSave creates directory unless exists for a given file

func EnvSetBoolIfSet

func EnvSetBoolIfSet(name string, value *bool)

EnvSetBoolIfSet sets boolean variable if environment variable is set and contains a valid entry

func EnvSetIfSet

func EnvSetIfSet(name string, value *string)

EnvSetIfSet updates variable with value if os env exists

func EnvSetIntIfSet

func EnvSetIntIfSet(name string, value *int64)

EnvSetIntIfSet updates int variable if environment variable exists

func Exists

func Exists(path string) bool

Exists returns true if file/path exists

func FileToReader

func FileToReader(fileName string) (io.Reader, error)

FileToReader gets a buffered reader from a fileName

func GetEnvBoolOverrideFlag

func GetEnvBoolOverrideFlag(currValue bool, flagName string) bool

GetEnvBoolOverrideFlag if environment flag is set to 1 it returns true

func GetEnvIntUnlessFlagIsNotDefault

func GetEnvIntUnlessFlagIsNotDefault(envVar string, defaultValue, flagValue, flagDefaultValue int) int

GetEnvIntUnlessFlagIsNotDefault Getting INT env variable unless flagvalue differs from defaultvalue

func GetEnvUnlessFlagIsNotDefault

func GetEnvUnlessFlagIsNotDefault(envVar, defaultValue, flagValue, flagDefaultValue string) string

GetEnvUnlessFlagIsNotDefault Getting String env variable unless flagvalue differs from defaultvalue

func Hostname

func Hostname() string

Hostname returns primary env HOST_HOSTNAME secondary os.Hostname

func IsDirectory

func IsDirectory(path string) bool

IsDirectory is path a directory

func IsTextData

func IsTextData(data []byte) bool

IsTextData checks if a byte stream is text

func IsTextFile

func IsTextFile(fileName string) (bool, error)

IsTextFile tells if a file is a text file

func ParseNames

func ParseNames(input string) []string

ParseNames splits a input string on , trimsspaces and removes all prefixed with # Makes it easier to use list values as (multipline) input params

func WriteHeapFile

func WriteHeapFile(fileName string) error

WriteHeapFile dumps memory allocation to disc for analytics

Types

type AtomicIntCounter

type AtomicIntCounter int64

AtomicIntCounter uses an int64 internally.

func (*AtomicIntCounter) Get

func (c *AtomicIntCounter) Get() int64

Get returns current counter

type CliOption

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

CliOption encapsulates viper flag and values

func NewCliOption

func NewCliOption(prefix string) *CliOption

NewCliOption creates a new cli option object

func (*CliOption) GetBool

func (c *CliOption) GetBool(name string) bool

GetBool returns bool value for param

func (*CliOption) GetInt

func (c *CliOption) GetInt(name string) int

GetInt returns int value for param

func (*CliOption) GetString

func (c *CliOption) GetString(name string) string

GetString returns string value for param

func (*CliOption) MakeBool

func (c *CliOption) MakeBool(key, short, envName string, defaultValue bool, description string)

MakeBool creates a application bool variable both for env and command line

func (*CliOption) MakeInt

func (c *CliOption) MakeInt(key, short, envName string, defaultValue int, description string)

MakeInt creates a application int variable both for env and command line

func (*CliOption) MakeString

func (c *CliOption) MakeString(key, short, envName, defaultValue, description string)

MakeString creates a application string variable both for env and command line

func (*CliOption) ReadConfig

func (c *CliOption) ReadConfig() error

ReadConfig reads and parses the config options set

func (*CliOption) ReadTomlConfigFile

func (c *CliOption) ReadTomlConfigFile(configFolder, fileName string) error

ReadTomlConfigFile reads config file from a given folder

func (*CliOption) Viper

func (c *CliOption) Viper() *viper.Viper

Viper returns viper object

type ConfigCheck

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

func (*ConfigCheck) AddIfEmptyStr

func (c *ConfigCheck) AddIfEmptyStr(title, value string)

func (*ConfigCheck) TerminateIfErrors

func (c *ConfigCheck) TerminateIfErrors()

type FsWatcher

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

FsWatcher monitors a given monPath, checks for file updates every pollDur and declares them stable if no updates are detected after stableDur.

func NewFsWatcher

func NewFsWatcher(monPath, pattern string, pollDur time.Duration, stableDur time.Duration) *FsWatcher

NewFsWatcher watches a folder for filesystem events.

func (*FsWatcher) Watch

func (w *FsWatcher) Watch() <-chan string

Watch returns stable filenames on the channel.

type ParlyDb

type ParlyDb struct {
	sync.RWMutex
	Name  string
	Lines []string
}

ParlyDb handles prosessed files

func NewParlyDb

func NewParlyDb(name string) *ParlyDb

NewParlyDb constructs a new struct to handle file proessesing

func (*ParlyDb) Add

func (db *ParlyDb) Add(fileName string)

Add - Adding file to is Prossesed list

func (*ParlyDb) IsProsessed

func (db *ParlyDb) IsProsessed(fileName string) bool

IsProsessed - Checks if a file is already prosessed

func (*ParlyDb) ReadLines

func (db *ParlyDb) ReadLines() error

ReadLines reads a whole file into memory and returns a slice of its lines.

func (*ParlyDb) WriteLines

func (db *ParlyDb) WriteLines() error

WriteLines writes the lines to the given file.

type Store

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

Store is db abstarction on top of bolt db

func Open

func Open(path string) (*Store, error)

Open a database file

func (*Store) Close

func (store *Store) Close() error

Close the database

func (*Store) CreateBucket

func (store *Store) CreateBucket(bucket string) error

CreateBucket creates a buck

func (*Store) Delete

func (store *Store) Delete(bucket string, key string) error

Delete the entry with the given key. If no such key is present in the store, it returns ErrNotFound.

store.Delete("key42")

func (*Store) ForEach

func (store *Store) ForEach(bucket string, fn func(k, v []byte) error) error

ForEach iterates over a given bucket

func (*Store) Get

func (store *Store) Get(bucket, key string, value *string) error

Get a key/value from a given bucket

func (*Store) GetDB

func (store *Store) GetDB() *bolt.DB

GetDB returns pointer to underlaying DB.

func (*Store) ListBucket

func (store *Store) ListBucket(bucket string, filter func(k, v string) (bool, error)) ([]string, error)

ListBucket lists all buckets

func (*Store) ListBuckets

func (store *Store) ListBuckets() ([]string, error)

ListBuckets lists all buckets

func (*Store) Put

func (store *Store) Put(bucket string, key string, value string) error

Put a key/value into a given bucket

type TimerData

type TimerData struct {
	Title          string
	Uuid           string
	StartTimeRun   time.Time
	StartTimeBatch time.Time
	EndTimeRun     time.Time

	BatchSize  int64
	PrevRows   int64
	Index      AtomicIntCounter
	ErrorCount AtomicIntCounter

	Logger *log.Entry
	// contains filtered or unexported fields
}

TimerData containes run time data

func NewTimer

func NewTimer(title string) *TimerData

NewTimer creates a new timer struct

func (*TimerData) BatchDuractionSeconds

func (timer *TimerData) BatchDuractionSeconds() int64

BatchDuractionSeconds returns durection in seconds

func (*TimerData) IncError

func (timer *TimerData) IncError() int64

IncError adds one to number of errors

func (*TimerData) LogFields

func (timer *TimerData) LogFields() log.Fields

LogFields returns summary data as fieldmap

func (*TimerData) ShowBatchTime

func (timer *TimerData) ShowBatchTime()

ShowBatchTime show averages to now

func (*TimerData) ShowTotalDuration

func (timer *TimerData) ShowTotalDuration()

ShowTotalDuration outputs duration to log with fields

func (*TimerData) Start

func (timer *TimerData) Start()

Start the timer

func (*TimerData) Stop

func (timer *TimerData) Stop() time.Time

Stop stops the timer

func (*TimerData) Tick

func (timer *TimerData) Tick()

Tick increases tick with one

func (*TimerData) TickBatch

func (timer *TimerData) TickBatch(cnt int)

TickBatch increases tick with one

func (*TimerData) TotalDuractionSeconds

func (timer *TimerData) TotalDuractionSeconds() int64

TotalDuractionSeconds returns total duration in seconds

func (*TimerData) TotalDuration

func (timer *TimerData) TotalDuration() time.Duration

TotalDuration returns duration as a time.Duration

type WaitGroup

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

func (*WaitGroup) Add

func (r *WaitGroup) Add(delta int, info string)

func (*WaitGroup) Done

func (r *WaitGroup) Done(info string)

func (*WaitGroup) TryWait

func (r *WaitGroup) TryWait() bool

func (*WaitGroup) Wait

func (r *WaitGroup) Wait()

Jump to

Keyboard shortcuts

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