goutils

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

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

Go to latest
Published: Feb 13, 2026 License: AGPL-3.0 Imports: 21 Imported by: 24

README

goutils

install

go get -u github.com/117503445/goutils

usage

package main

import (
    "github.com/117503445/goutils"
    "github.com/rs/zerolog/log"
)

func main() {
    // init zerolog
    goutils.InitZeroLog()
    log.Info().Msg("hello world")

    // run `ls -l` in /tmp
    if _, err := goutils.Exec("ls -l"); err != nil {
        log.Error().Err(err).Msg("run cmd failed")
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandLogger = Logger.With().Str("module", "goutils.command").Logger()
View Source
var ExecOpt = &ExecOptions{
	Cwd:             "",
	DumpOutput:      false,
	PreExecHandler:  preExecHandlerLog,
	ExecutedHandler: executedHandlerFatalLog,
}

ExecOpt is the default options for Exec

View Source
var Logger = log.With().Str("module", "goutils").Logger()

Functions

func AtomicWriteFile

func AtomicWriteFile(path string, reader io.Reader) error

AtomicWriteFile writes the content of reader to a file at path atomically.

func CMD deprecated

func CMD(cwd string, command string, args ...string) error

Deprecated: Use Exec instead

func CopyDir

func CopyDir(src, dst string) error

CopyDir copies a directory from src to dst

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst

func DirExists

func DirExists(path string) bool

func Download

func Download(url string, filePath string) error

func DurationToStr

func DurationToStr(d time.Duration) string

func Extract

func Extract(ctx context.Context, src string, dst string, config ...ExtractConfig) error

func FileExists

func FileExists(path string) bool

func FindGitRepoRoot

func FindGitRepoRoot() (string, error)

FindGitRepoRoot returns the root directory of the git repository

func GetGitRootDir

func GetGitRootDir() (string, error)

GetGitRootDir returns the root directory of the git repository Deprecated: Use FindGitRepoRoot instead

func InitZeroLog

func InitZeroLog(options ...logOption)

func MoveDir

func MoveDir(src, dst string) error

MoveDir moves a directory from src to dst

func MoveFile

func MoveFile(src, dst string) error

MoveFile moves a file from src to dst

func PathExists

func PathExists(path string) bool

PathExists returns true if the path exists

func ReadJson

func ReadJson[T any](filename string, data *T) error

ReadJson with generic type

func ReadText

func ReadText(filename string) (string, error)

func ReadToml

func ReadToml[T any](filename string, data *T) error

func ReadYaml

func ReadYaml[T any](filename string, data *T) error

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

func UUID4

func UUID4() string

func UUID7

func UUID7() string

func WriteJson

func WriteJson(filename string, data any) error

WriteJson writes data to a file in JSON format

func WriteText

func WriteText(filename, content string) error

func WriteToml

func WriteToml[T any](filename string, data T) error

func WriteYaml

func WriteYaml[T any](filename string, data T) error

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 WithCwd

type WithCwd string

type WithDumpOutput

type WithDumpOutput struct {
}

type WithEnv

type WithEnv map[string]string

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 WithLogger struct {
	Logger *zerolog.Logger
}

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 {
}

Directories

Path Synopsis
gclient
dingtalk
Package alert 提供钉钉机器人消息推送功能
Package alert 提供钉钉机器人消息推送功能

Jump to

Keyboard shortcuts

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