helper

package module
v1.0.1 Latest Latest
Warning

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

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

README

go-helper

Simple helper functions for GO program.

Features

File Description
common.go Debug flag
crypto.go Crypto function
debug.go Debug functions
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
common.go
  • var Debug bool = false
crypto.go
  • func BoxSealAnonymous(base64Pubkey, msg *string) *string
debug.go
  • func DebugEnv() bool
  • func DebugLog(msg ...interface{})
  • func ErrCheck(e error)
file.go
  • func CurrentPath() *string
  • func CurrentDirBase() *string
  • func FullPath(workpathP *string) *string
gitCmd.go
  • func GitExecExist() bool
  • func GitExecPath() string
  • func GitInit(workpathP *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

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

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 for helper package.

Functions

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 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 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")

Types

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 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 GitPush

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

Run "git push". 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.

Jump to

Keyboard shortcuts

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