helper

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 11 Imported by: 11

README

go-helper Paypal donate

Collections of Golang helper functions.

Table Of Content
Features
File Description
common.go Debug flag
crypto.go Crypto function
debug.go Debug functions
err.go Basic error type
file.go File/directory functions
gitCmd.go Git functions
myCmd.go exec.Command shell wrapper
report.go report/log functions auto detect and apply json marshal indent
string.go string/array functions
warning.go warning type
common.go
  • var Debug bool = false
  • var DebugReport bool = false
  • var Errs ErrsT
  • var Warns Warnings
crypto.go
  • func BoxSealAnonymous(base64Pubkey, msg *string) *string
debug.go
  • func DebugEnv() bool
  • func DebugLog(msg ...interface{})
  • func ErrCheck(e error)
err.go
  • type Err string
  • type ErrsT []error
  • func (self Err) Error() string
  • func (self *Err) String() string
  • func (self *Err) StringP() *string
  • func (self *ErrsT) NotEmpty() bool
file.go
  • func CurrentPath() *string
  • func CurrentDirBase() *string
  • func FullPath(workPathP *string) *string
  • func IsRegularFile(workPath string) bool
  • func IsDir(workPath string) bool
  • func SameDir(path1, path2 string) bool
gitCmd.go
  • func Git(workpathP *string, optionsP *[]string) *MyCmd
  • func GitClone(workpathP *string, optionsP *[]string) *MyCmd
  • func GitExecExist() bool
  • func GitExecPath() string
  • func GitInit(workpathP *string) *MyCmd
  • func GitBranchCurrent(workpathP *string) *MyCmd
  • func GitPull(workpathP *string, optionsP *[]string) *MyCmd
  • func GitPush(workpathP *string, optionsP *[]string) *MyCmd
  • func GitRemote(workpathP *string, v bool) *[]string
  • func GitRemoteAdd(workpathP *string, name string, git string) *MyCmd
  • func GitRemoteExist(workpathP *string, name string) bool
  • func GitRemoteRemove(workpathP *string, name string) *MyCmd
  • func GitRemoteRemoveAll(workpathP *string)
  • func GitRoot(workpathP *string) string
  • func GitRootSubmodule(workpathP *string) string
myCmd.go
  • type MyCmd struct
  • func MyCmdRun(cmdName string, argsP *[]string, workpathP *string) *MyCmd
  • func MyCmdRunWg(cmdName string, argsP *[]string, workpathP *string, title *string, wgP *sync.WaitGroup, output bool) *MyCmd
  • func MyCmdInit(name string, argsP *[]string, workpathP *string) *MyCmd
  • func (self *MyCmd) Run() error
  • func (self *MyCmd) ExitCode() int
report.go
  • type ReportT struct
  • func Report(data any, title string, skipEmpty bool, singleLine bool)
  • func ReportDebug(data any, title string, skipEmpty bool, singleLine bool)
  • func ReportSp(data any, title string, skipEmpty bool, singleLine bool) *string
  • func ReportSpDebug(data any, title string, skipEmpty bool, singleLine bool) *string
  • func ReportStatus(data bool, title string, singleLine bool)
  • func ReportStatusSp(data bool, title string, singleLine bool) *string
  • func ReportNew(data any, title string, skipEmpty bool, singleLine bool) *ReportT
  • func (self *ReportT) String() string
  • func (self *ReportT) StringDebug() string
  • func (self *ReportT) StringP() *string
  • func (self *ReportT) StringPDebug() *string
string.go
  • func BoolString(b bool) string
  • func BoolStatus(b bool) string
  • func BoolYesNo(b bool) string
  • func JsonIndentSp(baP *[]byte, endLn bool) *string
  • func StrArrayPtrContain(aP *[]string, sP *string) bool
  • func StrArrayPtrRemoveEmpty(saP *[]string) *[]string
  • func StrArrayPtrPrintln(saP *[]string)
  • func StrArrayPtrPrintlnSp(saP *[]string) *string
  • func StrPtrToArrayPtr(sP *string) *[]string
  • func StrPtrToJsonIndentSp(strP *string, endLn bool) *string
  • func StrToJsonIndentSp(str string, endLn bool) *string
  • func AnyToJsonIndentSp(data any, endLn bool) *string
warning.go
  • type Warning string
  • type Warnings []Warning
  • func (self Warning) Error() string
  • func (self *Warning) String() string
  • func (self *Warning) StringP() *string
  • func (self *Warnings) NotEmpty() bool
Doc
Usage
import	"github.com/J-Siu/go-helper"

func main() {
	helper.Debug = true
	helper.DebugLog("debug msg")
}
Test
cd test
# All
go test
# Individual
go test file_test.go
go test gitCmd_test.go
go test myCmd_test.go
go test report_test.go
Used By Project
Repository
Contributors
Change Log
  • 0.0.1
    • Initial Commit
  • 0.9.0
    • Function update
  • 0.9.1
    • Fix git command args
  • v0.9.1
    • Add prefix v for version number
  • v0.9.2
    • Fix MyCmdRunWg() missing wgP.Done()
  • v0.9.3
    • ReportTStringP():
      • fix using wrong var when handling *[]byte
      • add []byte case
  • v0.9.4
    • Report support SingleLine mode
  • v0.9.5
    • Fix ReportT.SpringP() output
  • v0.9.6
    • Fix ReportT.SpringP() skip empty output
  • v0.9.8
    • Fix ReportT.SpringP() logical bug
    • Add report_test.go
  • v0.9.9
    • Add workpath support for gitCmd and myCmd
    • Add GitRoot(), GitRootSubmodule(), GitExecExist(), GitExecPath()
    • Add test
  • v1.0.0
    • file.go
      • Add FullPath()
      • All func return *string
    • gitCmd.go
      • GitPush() correct optionP param type
    • myCmd.go
      • MyCmd.Run() improve debug output
      • MyCmdInit() use fullpath for WorkDir
    • report.go
      • ReportT.StringP()
        • Add *[]string case
        • case []string, *[]string
          • fix bug only print last line
          • no longer remove empty line
    • string.go
      • StrArrayPtrRemoveEmpty() return new array
      • StrPToArrayP no longer remove empty line
      • func name StrPToArrayP -> StrPtrToArrayPtr
  • v1.0.1
    • Improve comment for godoc
  • v1.1.1
    • Add Git(), GitBranchCurrent(), GitClone(), GitPull()
  • v1.1.2
    • Add IsRegularFile(), IsDir(), SameDir()
    • Add StrPtrToJsonIndentSp(), StrToJsonIndentSp(), AnyToJsonIndentSp()
    • Add basic error type
    • Add warning type
    • If !Debug, short circuit ReportDebug() ReportSpDebug()
    • Report() support error type
License

The MIT License

Copyright (c) 2022 John Siu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug bool = false

Debug flag General debug flag to enable/disable ReportDebug*

View Source
var DebugReport bool = false

Enable/Disable debug printout in report.go

Functions

func AnyToJsonIndentSp added in v1.1.2

func AnyToJsonIndentSp(data any, endLn bool) *string

Json indent any to *string. If <endLn> is true, add new line at end of string if not exist. Return string pointer.

func BoolStatus

func BoolStatus(b bool) string

bool to "OK"/"Fail"

func BoolString

func BoolString(b bool) string

bool to "true"/"false"

func BoolYesNo

func BoolYesNo(b bool) string

bool to "Yes/No"

func BoxSealAnonymous

func BoxSealAnonymous(base64Pubkey, msg *string) *string

Encrypt msg with public key using nacl box seal anonymous.

Parameter "base64Pubkey" and returning string are base64 encoded.

func CurrentDirBase

func CurrentDirBase() *string

Get current directory name. Not full path. Return string pointer.

func CurrentPath added in v0.9.9

func CurrentPath() *string

Get full path of current directory. Return string pointer.

func DebugEnv

func DebugEnv() bool

DebugEnv - set debug flag from env

func DebugLog

func DebugLog(msg ...interface{})

DebugLog - msg

func ErrCheck

func ErrCheck(e error)

ErrCheck - check error

func FullPath added in v1.0.0

func FullPath(workPathP *string) *string

Return full path of path provided. If <workPathP> is empty/nil, use current path. <workPathP> not modified. Return string pointer.

func GitExecExist added in v0.9.9

func GitExecExist() bool

Check git executable exist.

func GitExecPath added in v0.9.9

func GitExecPath() string

Get git executable path. Return empty string if not found.

func GitRemote

func GitRemote(workpathP *string, v bool) *[]string

Run "git remote". If <workpathP> is empty/nil, current directory is used. Return remotes in string array.

func GitRemoteExist

func GitRemoteExist(workpathP *string, name string) bool

Check if a git remote(by name) exist in workpath. If <workpathP> is empty/nil, current directory is used.

func GitRemoteRemoveAll

func GitRemoteRemoveAll(workpathP *string)

Run "git remote remove" all git remotes If <workpathP> is empty/nil, current directory is used.

func GitRoot added in v0.9.9

func GitRoot(workpathP *string) string

Get git root from current directory. If <workpathP> is empty/nil, current directory is used. Return empty string if not a git dir.

func GitRootSubmodule added in v0.9.9

func GitRootSubmodule(workpathP *string) string

Get git submodule root from `workpath`. If <workpathP> is empty/nil, current directory is used. Return empty string if not a submodule dir.

func IsDir added in v1.1.2

func IsDir(workPath string) bool

func IsRegularFile added in v1.1.2

func IsRegularFile(workPath string) bool

func JsonIndentSp

func JsonIndentSp(baP *[]byte, endLn bool) *string

Json indent *[]byte to *string. If <endLn> is true, add new line at end of string if not exist. Return string pointer.

func Report

func Report(data any, title string, skipEmpty bool, singleLine bool)

Print any data(optional) with title(optional). If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line.

func ReportDebug

func ReportDebug(data any, title string, skipEmpty bool, singleLine bool)

Only print if helper.Debug is true. Print any data(optional) with title(optional). If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line.

func ReportSp

func ReportSp(data any, title string, skipEmpty bool, singleLine bool) *string

Print any data(optional) with title(optional) into a string. If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line. Return a string pointer.

func ReportSpDebug added in v0.9.8

func ReportSpDebug(data any, title string, skipEmpty bool, singleLine bool) *string

Only print if helper.Debug is true. Print any data(optional) with title(optional) into a string. If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line. Return a string pointer.

func ReportStatus

func ReportStatus(data bool, title string, singleLine bool)

Print bool into true/false, with title(optional). If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line.

func ReportStatusSp added in v1.0.1

func ReportStatusSp(data bool, title string, singleLine bool) *string

Only print if helper.Debug is true. Print bool into true/false, with title(optional), into a string. If <skipEmpty> is true, will not print title if <data> is empty. If <singleLine> is true, <data> will not start on new line. Return a string pointer.

func SameDir added in v1.1.2

func SameDir(path1, path2 string) bool

func StrArrayPtrContain

func StrArrayPtrContain(aP *[]string, sP *string) bool

Check if string array contain a string.

func StrArrayPtrPrintln

func StrArrayPtrPrintln(saP *[]string)

*[]string output

func StrArrayPtrPrintlnSp added in v1.0.1

func StrArrayPtrPrintlnSp(saP *[]string) *string

*string output

func StrArrayPtrRemoveEmpty

func StrArrayPtrRemoveEmpty(saP *[]string) *[]string

Return a new *[]string with empty lines removed from *[]string. Original []string not modified.

func StrPtrToArrayPtr added in v1.0.0

func StrPtrToArrayPtr(sP *string) *[]string

Split *string to *[]string by new line("\n")

func StrPtrToJsonIndentSp added in v1.1.2

func StrPtrToJsonIndentSp(strP *string, endLn bool) *string

Json indent *string to *string. If <endLn> is true, add new line at end of string if not exist. Return string pointer.

func StrToJsonIndentSp added in v1.1.2

func StrToJsonIndentSp(str string, endLn bool) *string

Json indent *string to *string. If <endLn> is true, add new line at end of string if not exist. Return string pointer.

Types

type Err added in v1.1.2

type Err string

Err is a string with error interface

func (Err) Error added in v1.1.2

func (self Err) Error() string

`error` interface`

func (*Err) String added in v1.1.2

func (self *Err) String() string

`stringer` interface

func (*Err) StringP added in v1.1.2

func (self *Err) StringP() *string

type ErrsT added in v1.1.2

type ErrsT []error

ErrArrayT is array of errors

var Errs ErrsT

error list

func (*ErrsT) NotEmpty added in v1.1.2

func (self *ErrsT) NotEmpty() bool

type MyCmd

type MyCmd struct {
	ArgsP   *[]string    `json:"ArgsP"`  // Command args
	CmdName string       `json:"Name"`   // Command name
	WorkDir string       `json:"Dir"`    // Command working dir
	Ran     bool         `json:"Ran"`    // Out: Set to true by Run()
	CmdLn   string       `json:"CmdLn"`  // Out: Command Line
	Err     error        `json:"Err"`    // Out: run error
	Stdout  bytes.Buffer `json:"Stdout"` // Out: Stdout
	Stderr  bytes.Buffer `json:"Stderr"` // Out: Stderr
}

A exec.Cmd wrapper

func Git added in v1.1.1

func Git(workpathP *string, optionsP *[]string) *MyCmd

Run "git <optionsP>". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitBranchCurrent added in v1.1.1

func GitBranchCurrent(workpathP *string) *MyCmd

Run "git branch --show-current". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitClone added in v1.1.1

func GitClone(workpathP *string, optionsP *[]string) *MyCmd

Run "git clone <optionsP>". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitInit

func GitInit(workpathP *string) *MyCmd

Run "git init". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitPull added in v1.1.1

func GitPull(workpathP *string, optionsP *[]string) *MyCmd

Run "git pull <optionsP>". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitPush

func GitPush(workpathP *string, optionsP *[]string) *MyCmd

Run "git push <optionsP>". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitRemoteAdd

func GitRemoteAdd(workpathP *string, name string, git string) *MyCmd

Run "git remote add <name> <git>". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func GitRemoteRemove

func GitRemoteRemove(workpathP *string, name string) *MyCmd

Run "git remote remove". If <workpathP> is empty/nil, current directory is used. Return a MyCmd pointer for execution information.

func MyCmdInit added in v0.9.9

func MyCmdInit(name string, argsP *[]string, workpathP *string) *MyCmd

Setup and return MyCmd pointer. If <workpathP> is empty/nil, current directory is used.

func MyCmdRun

func MyCmdRun(cmdName string, argsP *[]string, workpathP *string) *MyCmd

MyCmd run func wrapper. If <workpathP> is empty/nil, current directory is used.

func MyCmdRunWg

func MyCmdRunWg(cmdName string, argsP *[]string, workpathP *string, title *string, wgP *sync.WaitGroup, output bool) *MyCmd

MyCmd run func wrapper with sync.WaitGroup support. If <workpathP> is empty/nil, current directory is used. Print if <output> is true

func (*MyCmd) ExitCode added in v1.0.1

func (self *MyCmd) ExitCode() int

Return exit code

func (*MyCmd) Run

func (self *MyCmd) Run() error

A exec.Cmd.Run() wrapper.

type ReportT

type ReportT struct {
	Data       any    `json:"Data"`       // Data to be printed
	Title      string `json:"Title"`      // Title of print out
	ModeStatus bool   `json:"ModeStatus"` // bool to "OK/Failed"
	SkipEmpty  bool   `json:"SkipEmpty"`  // Return empty string if Data is empty
	SingleLine bool   `json:"SingleLine"` // No need line after title
}

func ReportNew

func ReportNew(data any, title string, skipEmpty bool, singleLine bool) *ReportT

Setup ReportT with data(optional/nil), title(optional/""), <skipEmpty>, <singleLine>. Return the ReportT pointer.

func (*ReportT) String

func (self *ReportT) String() string

Print self.Data, self.Title to string If self.SkipEmpty is true, will not print self.Title if self.Data is empty. If self.SingleLine is true, self.Data will not start on new line.

func (*ReportT) StringDebug

func (self *ReportT) StringDebug() string

Only print if helper.Debug is true. Print self.Data, self.Title to string If self.SkipEmpty is true, will not print self.Title if self.Data is empty. If self.SingleLine is true, self.Data will not start on new line.

func (*ReportT) StringP

func (self *ReportT) StringP() *string

Print self.Data, self.Title to string pointer If self.SkipEmpty is true, will not print self.Title if self.Data is empty. If self.SingleLine is true, self.Data will not start on new line.

func (*ReportT) StringPDebug

func (self *ReportT) StringPDebug() *string

Only print if helper.Debug is true. Print self.Data, self.Title to string pointer If self.SkipEmpty is true, will not print self.Title if self.Data is empty. If self.SingleLine is true, self.Data will not start on new line.

type Warning added in v1.1.2

type Warning string

func (Warning) Error added in v1.1.2

func (self Warning) Error() string

`error` interface

func (*Warning) String added in v1.1.2

func (self *Warning) String() string

func (*Warning) StringP added in v1.1.2

func (self *Warning) StringP() *string

type Warnings added in v1.1.2

type Warnings []Warning
var Warns Warnings

warning list

func (*Warnings) NotEmpty added in v1.1.2

func (self *Warnings) NotEmpty() bool

Jump to

Keyboard shortcuts

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