eztools

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// DefID is the default ID
	DefID = 0
	// AllID stands for all items
	AllID = DefID - 1
	// InvalidID better to be negative to be different from a normal ID
	//	it is used as indexes into slices, too
	InvalidID = DefID - 2 //pairs defined some related
)
View Source
const (
	AUTH_NONE = iota
	AUTH_PLAIN
	AUTH_BASIC
	AUTH_DIGEST
	METHOD_GET  = "GET"
	METHOD_PUT  = "PUT"
	METHOD_POST = "POST"
	METHOD_DEL  = "DELETE"
)
View Source
const (
	// TblCHORE contains misc items
	TblCHORE = "chore"
	// TblTOOL contains xTS names
	TblTOOL = "tool"
	// TblVER contains versions of xTS
	TblVER = "ver"
	// TblANDROID contains android versions
	TblANDROID = "android"
	// TblGOOGLE contains xTS requirements
	TblGOOGLE = "google"
	// TblPRODUCT contains product names
	TblPRODUCT = "product"
	// TblPRODGLE contains matches of products and requirements
	TblPRODGLE = "prodgle"
	// TblPRODFO contains matches of products and info
	TblPRODFO = "prodfo"
	// TblBIT contains bit info
	TblBIT = "bit"
	// TblPHASE contains phase info
	TblPHASE = "phase"
	// TblTEAM contains team names
	TblTEAM = "team"
	// TblCONTACTS contains contact info
	TblCONTACTS = "contacts"
	// TblWEEKLYTASKBARS contains bars in report
	TblWEEKLYTASKBARS = "weeklyTaskBars"
	// TblWEEKLYTASKTITLES contains titles in report
	TblWEEKLYTASKTITLES = "weeklyTaskTitles"
	// TblWEEKLYTASKCURR contains titles of current week
	TblWEEKLYTASKCURR = "weeklyTaskCurr"
	// TblWEEKLYTASKNEXT contains titles of next week
	TblWEEKLYTASKNEXT = "weeklyTaskNext"
	// TblWEEKLYTASKDESC contains descriptions of tasks
	TblWEEKLYTASKDESC = "weeklyTaskDesc"
	// TblWEEKLYTASKWORK contains items of tasks
	TblWEEKLYTASKWORK = "weeklyTaskWork"

	// FldID is the name of field id
	FldID = "id"
	// FldSTR is the name of field str
	FldSTR = "str"

	// FldPHASE is the name of field phase
	FldPHASE = "phase"
	// FldTOOL is the name of field tool
	FldTOOL = "tool"
	// FldANDROID is the name of field android
	FldANDROID = "android"
	// FldPRODUCT is the name of field product
	FldPRODUCT = "product"
	// FldGOOGLE is the name of field google
	FldGOOGLE = "google"
	// FldVER is the name of field versions
	FldVER = "ver"
	// FldREQ is the name of field requirements
	FldREQ = "req"
	// FldEXP is the name of field expiry
	FldEXP = "exp"
	// FldBIT is the name of field bit
	FldBIT = "bit"

	// contacts
	// FldLEADER is the name of field leader
	FldLEADER = "leader"
	// FldNUMBER is the name of field number
	FldNUMBER = "number"
	// FldNAME is the name of field name
	FldNAME = "name"
	// FldTEAM is the name of field team
	FldTEAM = "team"
	// FldEXT is the name of field ext
	FldEXT = "ext"
	// FldPHONE is the name of field phone
	FldPHONE = "phone"
	// FldMAIL is the name of field mail
	FldMAIL = "mail"
	// FldLDAP is the name of field ldap
	FldLDAP = "ldap"
	// FldUID is the name of field uid
	FldUID = "uid"
	// FldNICK is the name of field nick
	FldNICK = "nick"

	// FldSECTION is the name of field section
	FldSECTION = "section"
)

Variables

View Source
var (
	// ErrNoValidResults stands for no valid results
	ErrNoValidResults = errors.New("No Valid results")
	// ErrOutOfBound stands for out of bound
	ErrOutOfBound = errors.New("Out of bound")
	// ErrInvalidInput stands for invalid input
	ErrInvalidInput = errors.New("Invalid input")
	// ErrInExistence stands for result from input already in existence
	ErrInExistence = errors.New("In Existence")
	// ErrAccess stands for access failure, such as privilege, collision
	ErrAccess = errors.New("Access failure")
)
View Source
var (
	// Debugging marks debugging mode
	Debugging bool // whether more debug procedures
	// Verbose marse debugging output level
	Verbose = 0
	// ChoiceNotification is printed when user needs to choose from choices
	ChoiceNotification = "Your choice is: "
)

Functions

func AddPair

func AddPair(db *sql.DB, table string, id int, str string) (res int, err error)

AddPair adds an item with ID and value

func AddPairNoID

func AddPairNoID(db *sql.DB, table string, str string) (int, error)

AddPairNoID adds an item with value, where ID should be auto generated

func AddWtParams

func AddWtParams(db *sql.DB, table string, fields []string, values []string, yes2all bool) (int, error)

AddWtParams adds "values" to "fields", no matter whether duplicate records in existence. Parameter yes2all = no confirmation in debug mode. Always no confirmation in non-debug mode.

func AddWtParamsUniq

func AddWtParamsUniq(db *sql.DB, table string, fields []string, values []string, yes2all bool) (int, error)

AddWtParamsUniq adds "values" to "fields", if no duplicate records in existence. Parameter yes2all = no confirmation in debug mode. Always no confirmation in non-debug mode.

func AppUpgrade

func AppUpgrade(db *sql.DB, prefix string, ver string, server *chan string, ch chan bool)

AppUpgrade checks for updates and applies the update automatically, which will work next time the app is run. ch: 1st=false, if wrong server URL configured.

2nd=false, if other configurations wrong, or update check fails.

func Authenticate

func Authenticate(db *sql.DB, username, password string) error

Authenticate checks whether authenticated. Return value: nil if true.

func ChkCfmNPrompt

func ChkCfmNPrompt(noti, exception string) bool

ChkCfmNPrompt checks defaults and return false only when user replied exception program exits when user replied 'q' or 'e' no more confirmations when user replied 'a' or 'c' verbose set when user replied a number, in which case the prompt will show again

func ChooseInts

func ChooseInts(arr [][]string, notif string) (id int)

ChooseInts asks user to choose from a slice Parameters. arr[][0]=id. arr[][1]=string

func ChooseMaps

func ChooseMaps(choices []map[string]string, sep string, indI ...string) int

ChooseMaps asks user to choose from a slice of map of string to string parameters: slice.

separator between two piece of information.
index(-es) into the map (to be contacted) to be information of each item,
	or separators between two indexes.

example: (c, ";", "a", "c") with

c	index	name	value
	0	a	A
	0	c	C
	1	a	B
	1	c	D
will print
	0: A;C
	1: B;D

func ChoosePair

func ChoosePair(db *sql.DB, table string) (int, error)

ChoosePair asks user to choose from a table by ID - value pairs.

func ChoosePairNAddAssociated

func ChoosePairNAddAssociated(db *sql.DB, idTable, strTable string, uniq bool) (int, error)

ChoosePairNAddAssociated asks user to choose by idTable - strTable pairs,

where idTable.str == strTable.id (return value)

Parameter uniq is of no use

func ChoosePairOrAdd

func ChoosePairOrAdd(db *sql.DB, table string, uniq bool) (int, error)

ChoosePairOrAdd asks user to choose from a table by ID - value pairs,

allowing to add one new.

func ChooseStrings

func ChooseStrings(choices []string) (int, string)

ChooseStrings asks user to choose from a slice return values: index (InvalidID if not a valid one) and string

func ChooseStringsWtIDs

func ChooseStringsWtIDs(fL func() int, fI func(int) int,
	fV func(int) string, notif string) (res int)

ChooseStringsWtIDs is for general usage to ask user to choose from a slice or anything parameters.

fL=quantity of elements
fI=get index to match user's input
fV=get message to show for each index
notif=notification string for user

func Connect

func Connect() (*sql.DB, error)

Connect connects to the database using parameters from eztools.xml root element is named "root", elements include "ip", "db", "user" and "pass"

func ConnectWtCfg

func ConnectWtCfg(file string) (*sql.DB, error)

ConnectWtCfg connects to the database using parameters from an xml file root element is named "root", elements include "ip", "db", "user" and "pass"

func ConnectWtParam

func ConnectWtParam(user, pass, ip, database string) (db *sql.DB, err error)

ConnectWtParam connects to the database using parameters.

func ConnectWtPath

func ConnectWtPath(path string) (*sql.DB, error)

ConnectWtPath connects to the database using path as config file

func DeleteWtID

func DeleteWtID(db *sql.DB, table, id string) error

DeleteWtID by ID

func DeleteWtParams

func DeleteWtParams(db *sql.DB, table, where string) error

DeleteWtParams deletes items with specified WHERE clause

func DiffDate

func DiffDate(dateO, dateN string) (diff int, ok bool)

DiffDate = dateN - dateO return values: difference in days, whether calculated

func FileWrite added in v2.1.0

func FileWrite(file string, bytes []byte, createIfNeeded bool) error

FileWrite writes input bytes to file

func FindStrMap

func FindStrMap(obj interface{}, key string) (interface{}, bool)
FindStrMap find string key in map[string]interface{} obj,

returning the value and true or nil and false.

func GetDate

func GetDate(info string) string

GetDate asks user to input a date string

func GetPair

func GetPair(db *sql.DB, table, id, from, to string) (string, error)

GetPair gets "to" field from "from" field in "table" When multiple results got, the first one will be taken. return value error = from db.Query;

ErrNoValidResults when no valid results got,
                  and LogErrPrint will be called.

func GetPairID

func GetPairID(db *sql.DB, table string, str string) (int, error)

GetPairID gets ID (int) from value (string) When multiple results got, the first one will be taken.

func GetPairIDFromInt

func GetPairIDFromInt(db *sql.DB, table string, val int) (int, error)

GetPairIDFromInt gets ID (int) from value (int) When multiple results got, the first one will be taken.

func GetPairInt

func GetPairInt(db *sql.DB, table string, id string) (int, error)

GetPairInt gets value (int) from ID (string)

func GetPairStr

func GetPairStr(db *sql.DB, table string, id string) (string, error)

GetPairStr gets value (string) from ID (string)

func GetPairStrFromInt

func GetPairStrFromInt(db *sql.DB, table string, id int) (string, error)

GetPairStrFromInt gets value (string) from ID (int)

func InitLogger

func InitLogger(out *os.File) error

InitLogger opens log file

func Locate

func Locate(db *sql.DB, table string, str string) (id int, err error)

Locate gets ID (int) from value (string) Deprecated: Use GetPairID instead.

func Log

func Log(out ...interface{})

Log logs it

func LogErr

func LogErr(err error)

LogErr logs error

func LogErrFatal

func LogErrFatal(err error)

LogErrFatal logs and prints error and exits

func LogErrPrint

func LogErrPrint(err error)

LogErrPrint logs and prints error

func LogErrPrintWtInfo

func LogErrPrintWtInfo(info string, err error)

LogErrPrintWtInfo logs and prints error with information string

func LogErrWtInfo

func LogErrWtInfo(info string, err error)

LogErrWtInfo logs error with information string

func LogFatal

func LogFatal(out ...interface{})

LogFatal logs and prints it and exits

func LogPrint

func LogPrint(out ...interface{})

LogPrint logs and prints it

func LogPrintWtTime

func LogPrintWtTime(out ...interface{})

LogPrintWtTime logs and prints a string with time

func LogWtTime

func LogWtTime(out ...interface{})

LogWtTime logs a string with time

func PromptInt

func PromptInt(pi string) (res int, err error)

PromptInt prompts user and gets input Return values. zero values are default

func PromptIntStr

func PromptIntStr(pi string, ps string) (i int, s string)

PromptIntStr prompts user and gets two inputs Return values. zero values are default

func PromptPwd

func PromptPwd(ps string) string

PromptPwd prompts user and get password

func PromptStr

func PromptStr(ps string) string

PromptStr prompts user and get input

func RangeStrMap

func RangeStrMap(obj interface{}, fun func(k string, v interface{}) bool) bool
RangeStrMap iterate through map[string]interface{} obj, calling fun for

each element recursively. When fun returns true, it stops. false is returned if no element found.

func RestGet

func RestGet(url string, authInfo AuthInfo, bodyReq io.Reader) (bodyMap interface{}, errNo int, err error)

RestGet sends Restful API request and returns the result.

func RestGetOrPostSaveFile

func RestGetOrPostSaveFile(method, url string, authInfo AuthInfo,
	magic []byte, file string) (errNo int, err error)
RestGetOrPostBodyBytes sends Restful API request as a method and returns the result.

A magic string can be listed to be stripped in the beginning of the result.

func RestGetOrPostWtMagic

func RestGetOrPostWtMagic(method, url string, authInfo AuthInfo, bodyReq io.Reader, magic []byte) (bodyMap interface{}, errNo int, err error)
RestGetOrPostWtMagic sends Restful API request as a method and returns the result.

A magic string can be listed to be stripped in the beginning of the result. If magic string is assigned but not found in the result, the result will not be parsed. If failure response received, bodyMap is slice of byte, copying the failure body.

func RestGetOrPostWtMagicNFileNHdr

func RestGetOrPostWtMagicNFileNHdr(method, url string, authInfo AuthInfo, magic []byte,
	fType, fName string, hdrs map[string]string) (bodyMap interface{}, errNo int, err error)
RestGetOrPostWtMagicNFileNHdr sends Restful API request as a method and returns the result.

Use this to specify a file with name and type to be sent as body, and/or extra headers. If something wrong with the file, a request will be sent without it anyway. A magic string can be listed to be stripped in the beginning of the result. If magic string is assigned but not found in the result, the result will not be parsed. If failure response received, bodyMap is slice of byte, copying the failure body.

func RestGetWtMagic

func RestGetWtMagic(url string, authInfo AuthInfo, bodyReq io.Reader, magic []byte) (bodyMap interface{}, errNo int, err error)
RestGetWtMagic sends Restful API request as GET and returns the result.

A magic string can be listed to be stripped in the beginning of the result. If magic string is assigned but not found in the result, the result will not be parsed.

func RestPostWtMagic

func RestPostWtMagic(url string, authInfo AuthInfo, bodyReq io.Reader, magic []byte) (bodyMap interface{}, errNo int, err error)
RestPostWtMagic sends Restful API request as POST and returns the result.

A magic string can be listed to be stripped in the beginning of the result. If magic string is assigned but not found in the result, the result will not be parsed.

func Rows2Strings

func Rows2Strings(db *sql.DB, rows *sql.Rows) (res [][]string, err error)

Rows2Strings returns arrays from rows Parameter db not used

func Search(db *sql.DB, table string, cri string, sel []string, more string) ([][]string, error)

Search gets values of fields "sel" from "table", using "cri" as "WHERE",

with "more" appended to SQL command.

Parameters: more: will not be prefixed with space automatically

func ShowArrln

func ShowArrln(arr []string)

ShowArrln prints a slice in one line with a line break

func ShowByteln

func ShowByteln(ps []byte)

ShowByteln prints byte slice as string with a line break

func ShowSthln

func ShowSthln(sth interface{})

ShowSthln prints anything with struct names and a line break

func ShowStr

func ShowStr(ps ...interface{})

ShowStr prints anything with no line breaks

func ShowStrln

func ShowStrln(ps ...interface{})

ShowStrln prints anything with a line break

func ShowWtFmt

func ShowWtFmt(fs string, sth ...interface{})

ShowWtFmt prints with format string like printf

func TranDate

func TranDate(date string) string

TranDate removes minuses from date string return current date if empty string as param

func TranSize

func TranSize(b int64, precision int, space bool) string

TranSize shows the number as file size format input params: b=number; precision=how many number to keep lower than point;

space=whether a space is put between number and unit

copied from https://programming.guide/go/formatting-byte-size-to-human-readable-format.html

func UpdatePairID

func UpdatePairID(db *sql.DB, table string, idOld, idNew string) error

UpdatePairID updates ID

func UpdatePairWtParams

func UpdatePairWtParams(db *sql.DB, table string, id, str string) error

UpdatePairWtParams updates value by ID

func UpdateWtParams

func UpdateWtParams(db *sql.DB, table, cri string, fields, values []string, yes2all bool) error

UpdateWtParams updates "fields" in "table" with "values", using "cri" as "WHERE". Parameter yes2all = no confirmation in debug mode. Always no confirmation in non-debug mode.

func XMLRead

func XMLRead(file string, data interface{}, _ bool) error

XMLRead reads file into input structure

func XMLWrite

func XMLWrite(file string, data interface{}, createIfNeeded bool, indent string) error

XMLWrite writes input structure to file this reformats the file with indent provided

func XMLWriteNoCreate

func XMLWriteNoCreate(file string, cfg interface{}, indent string) error

XMLWriteNoCreate writes config file from input structure by a full path (with .xml extension) this reformats the file with indent provided

func XMLsReadDefault

func XMLsReadDefault(path, file string, cfg interface{},
	createIfNeeded bool) (
	pathFound string, err error)

XMLsReadDefault reads a config file

func XMLsReadDefaultNoCreate

func XMLsReadDefaultNoCreate(path, file string, cfg interface{}) (string, error)

XMLsReadDefaultNoCreate reads config file into input structure from given path, or given file name (plus .xml) under current dir or home dir returns full file name with path

Types

type AuthInfo

type AuthInfo struct {
	Type       int
	User, Pass string
}

type PairsInt

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

PairsInt is a collection of pairsI

func GetSortedPairsInt

func GetSortedPairsInt(db *sql.DB, table string) (pi *PairsInt, err error)

GetSortedPairsInt returns all sorted PairsInt from input table

func (*PairsInt) Next

func (pi *PairsInt) Next() (int, int, error)

Next moves to the next item in PairsInt and return the values error=ErrOutOfBound when the end met

func (*PairsInt) Rewind

func (pi *PairsInt) Rewind()

Rewind sets the cursor to the beginning of PairsInt

type PairsStr

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

PairsStr is a collection of pairs

func GetSortedPairsStr

func GetSortedPairsStr(db *sql.DB, table string) (ps *PairsStr, err error)

GetSortedPairsStr returns all sorted PairsStr from input table

func (*PairsStr) FindStr

func (ps *PairsStr) FindStr(s string) (int, error)

FindStr find the first ID where str matches input return value: ErrNoValidResults when none found

func (*PairsStr) Next

func (ps *PairsStr) Next() (int, string, error)

Next moves to the next item in PairsInt and return the values error=ErrOutOfBound when the end met

func (*PairsStr) Rewind

func (ps *PairsStr) Rewind()

Rewind sets the cursor to the beginning of PairsInt

Jump to

Keyboard shortcuts

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