goutils

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

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

Go to latest
Published: Sep 20, 2024 License: AGPL-3.0 Imports: 11 Imported by: 7

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 FindGitRepoRoot

func FindGitRepoRoot() (string, error)

FindGitRepoRoot

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 ReadJSON

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

ReadJSON with generic type

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

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

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

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