Documentation
¶
Index ¶
- Variables
- func AtomicWriteFile(path string, reader io.Reader) error
- func CMD(cwd string, command string, args ...string) errordeprecated
- func CopyDir(src, dst string) error
- func CopyFile(src, dst string) error
- func DirExists(path string) bool
- func Download(url string, filePath string) error
- func DurationToStr(d time.Duration) string
- func Extract(ctx context.Context, src string, dst string, config ...ExtractConfig) error
- func FileExists(path string) bool
- func FindGitRepoRoot() (string, error)
- func GetGitRootDir() (string, error)
- func InitZeroLog(options ...logOption)
- func MoveDir(src, dst string) error
- func MoveFile(src, dst string) error
- func PathExists(path string) bool
- func ReadJson[T any](filename string, data *T) error
- func ReadText(filename string) (string, error)
- func ReadToml[T any](filename string, data *T) error
- func ReadYaml[T any](filename string, data *T) error
- func TimeStrMilliSec() string
- func TimeStrSec() string
- func UUID4() string
- func UUID7() string
- func WriteJson(filename string, data any) error
- func WriteText(filename, content string) error
- func WriteToml[T any](filename string, data T) error
- func WriteYaml[T any](filename string, data T) error
- type BuildInfo
- type ExecOptions
- type ExecResult
- type ExecutedHandlerContext
- type ExtractConfig
- type GetBuildInfoParams
- type LatestTaskExecutor
- type PreExecHandlerContext
- type WithCwd
- type WithDumpOutput
- type WithEnv
- type WithExeParentDir
- type WithExecutedHandlerErrorLog
- type WithExecutedHandlerFatalLog
- type WithExecutedHandlerSlient
- type WithLogger
- type WithNoColor
- type WithPreExecLog
- type WithPreExecSlient
- type WithProduction
- type WithWorkDirCmd
Constants ¶
This section is empty.
Variables ¶
var CommandLogger = Logger.With().Str("module", "goutils.command").Logger()
var ExecOpt = &ExecOptions{ Cwd: "", DumpOutput: false, PreExecHandler: preExecHandlerLog, ExecutedHandler: executedHandlerFatalLog, }
ExecOpt is the default options for Exec
var Logger = log.With().Str("module", "goutils").Logger()
Functions ¶
func AtomicWriteFile ¶
AtomicWriteFile writes the content of reader to a file at path atomically.
func DurationToStr ¶
func FileExists ¶
func FindGitRepoRoot ¶
FindGitRepoRoot returns the root directory of the git repository
func GetGitRootDir ¶
GetGitRootDir returns the root directory of the git repository Deprecated: Use FindGitRepoRoot instead
func InitZeroLog ¶
func InitZeroLog(options ...logOption)
func TimeStrMilliSec ¶
func TimeStrMilliSec() string
TimeStrMilliSec returns the time format string with millisecond, like 20240915.221219.123
func TimeStrSec ¶
func TimeStrSec() string
TimeStrSec returns the time format string, like 20240915.221219
Types ¶
type BuildInfo ¶
type BuildInfo struct {
GitCommit string
GitBranch string
GitTag string
GitDirty bool
GitVersion string
BuildTime string
BuildDir string
}
func GetBuildInfo ¶
func GetBuildInfo(ctx context.Context, params ...GetBuildInfoParams) (*BuildInfo, error)
type ExecOptions ¶
type ExecOptions struct {
// Cwd is the working directory of the command. If empty, the current working directory is used.
Cwd string
// DumpOutput indicates whether to dump the output to the standard output.
DumpOutput bool
DumpCompleteOutput bool
PreExecHandler func(*PreExecHandlerContext)
ExecutedHandler func(*ExecutedHandlerContext)
Env map[string]string
}
type ExecResult ¶
type ExecResult struct {
Stdout string
Stderr string
// Output is the combined stdout and stderr
Output string
}
ExecResult is the result of the command
func Exec ¶
func Exec(cmd string, opts ...execOption) (*ExecResult, error)
Exec is a wrapper of exec.Command.
Parameters: - cmd: the command to run, e.g. "ls -l". Spaces are used to split the command and arguments. Shell features like pipes are not supported. - opts: options to customize the behavior of the command
Returns: - *ExecResult: the result of the command. Always not nil. Even if the command fails, the result may contain some output. - error: if the command fails
type ExecutedHandlerContext ¶
type ExecutedHandlerContext struct {
Cmd string
Opt *ExecOptions
Res *ExecResult
Err error
}
type ExtractConfig ¶
type ExtractConfig struct {
SrcType string // "tar" "targz"
}
type GetBuildInfoParams ¶
type GetBuildInfoParams struct {
Dir string
}
type LatestTaskExecutor ¶
type LatestTaskExecutor struct {
// contains filtered or unexported fields
}
func NewLatestTaskExecutor ¶
func NewLatestTaskExecutor() *LatestTaskExecutor
func (*LatestTaskExecutor) AddTask ¶
func (d *LatestTaskExecutor) AddTask(f func())
func (*LatestTaskExecutor) Start ¶
func (d *LatestTaskExecutor) Start()
type PreExecHandlerContext ¶
type PreExecHandlerContext struct {
Cmd string
Opt *ExecOptions
}
type WithDumpOutput ¶
type WithDumpOutput struct {
}
type WithExeParentDir ¶
type WithExeParentDir struct {
}
WithExeParentDir is a option to set the working directory to the parent directory of the executable
type WithExecutedHandlerErrorLog ¶
type WithExecutedHandlerErrorLog struct {
}
type WithExecutedHandlerFatalLog ¶
type WithExecutedHandlerFatalLog struct {
}
type WithExecutedHandlerSlient ¶
type WithExecutedHandlerSlient struct {
}
type WithLogger ¶
type WithNoColor ¶
type WithNoColor struct {
}
type WithPreExecLog ¶
type WithPreExecLog struct {
}
type WithPreExecSlient ¶
type WithPreExecSlient struct {
}
type WithProduction ¶
type WithProduction struct {
DirLog string
Append bool // Append to existing log file, if false, it will overwrite the existing log file.
}
WithProduction is a log option, which is aimed to be used in production environment.
type WithWorkDirCmd ¶
type WithWorkDirCmd struct {
}