goutils

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

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

Go to latest
Published: Nov 10, 2024 License: AGPL-3.0 Imports: 12 Imported by: 10

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

func WriteJSON(filename string, data interface{}) error

WriteJSON writes data to a file in JSON format

func WriteText

func WriteText(filename, content string) error

Types

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

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

Jump to

Keyboard shortcuts

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