utils

package
v0.114.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 23 Imported by: 175

Documentation

Overview

Package utils ...

Index

Constants

This section is empty.

Variables

View Source
var InContainer = FileExists("/.dockerenv") || FileExists("/.containerenv")

InContainer will be true if is inside container environment, such as docker

View Source
var Panic = func(v interface{}) { panic(v) }

Panic is the same as the built-in panic

View Source
var TestEnvs = map[string]string{
	"GODEBUG": "tracebackancestors=100",
}

TestEnvs for testing

Functions

func AbsolutePaths added in v0.109.0

func AbsolutePaths(paths []string) []string

AbsolutePaths returns absolute paths of files in current working directory

func All added in v0.52.0

func All(actions ...func()) func()

All runs all actions concurrently, returns the wait function for all actions.

func CropImage added in v0.88.5

func CropImage(bin []byte, quality, x, y, width, height int) ([]byte, error)

CropImage by the specified box, quality is only for jpeg bin.

func DefaultBackoff added in v0.52.0

func DefaultBackoff(interval time.Duration) time.Duration

DefaultBackoff algorithm: A(n) = A(n-1) * random[1.9, 2.1)

func Dump added in v0.54.0

func Dump(list ...interface{}) string

Dump values for debugging

func E

func E(args ...interface{}) []interface{}

E if the last arg is error, panic it

func EscapeGoString added in v0.62.0

func EscapeGoString(s string) string

EscapeGoString not using encoding like base64 or gzip because of they will make git diff every large for small change

func Exec added in v0.52.0

func Exec(line string, rest ...string) string

Exec command

func ExecLine added in v0.97.13

func ExecLine(std bool, line string, rest ...string) string

ExecLine of command

func FileExists added in v0.52.0

func FileExists(path string) bool

FileExists checks if file exists, only for file, not for dir

func FormatCLIArgs added in v0.106.0

func FormatCLIArgs(args []string) string

FormatCLIArgs into one line string

func Mkdir added in v0.52.0

func Mkdir(path string) error

Mkdir makes dir recursively

func MustToJSON added in v0.52.0

func MustToJSON(data interface{}) string

MustToJSON encode data to json string

func MustToJSONBytes added in v0.52.0

func MustToJSONBytes(data interface{}) []byte

MustToJSONBytes encode data to json bytes

func Noop added in v0.112.9

func Noop()

Noop does nothing

func OutputFile added in v0.52.0

func OutputFile(p string, data interface{}) error

OutputFile auto creates file if not exists, it will try to detect the data type and auto output binary, string or json

func Pause added in v0.52.0

func Pause()

Pause the goroutine forever

func RandString added in v0.52.0

func RandString(len int) string

RandString generate random string with specified string length

func ReadString added in v0.52.0

func ReadString(p string) (string, error)

ReadString reads file as string

func Retry added in v0.52.0

func Retry(ctx context.Context, s Sleeper, fn func() (stop bool, err error)) error

Retry fn and sleeper until fn returns true or s returns error

func S added in v0.52.0

func S(tpl string, params ...interface{}) string

S Template render, the params is key-value pairs

func Sleep added in v0.52.0

func Sleep(seconds float64)

Sleep the goroutine for specified seconds, such as 2.3 seconds

Types

type ErrMaxSleepCount added in v0.52.0

type ErrMaxSleepCount struct {
	// Max count
	Max int
}

ErrMaxSleepCount type

func (*ErrMaxSleepCount) Error added in v0.92.0

func (e *ErrMaxSleepCount) Error() string

Error interface

func (*ErrMaxSleepCount) Is added in v0.92.0

func (e *ErrMaxSleepCount) Is(err error) bool

Is interface

type IdleCounter added in v0.69.0

type IdleCounter struct {
	// contains filtered or unexported fields
}

IdleCounter is similar to sync.WaitGroup but it only resolves if no jobs for specified duration.

func NewIdleCounter added in v0.69.0

func NewIdleCounter(d time.Duration) *IdleCounter

NewIdleCounter ...

func (*IdleCounter) Add added in v0.69.0

func (de *IdleCounter) Add()

Add ...

func (*IdleCounter) Done added in v0.69.0

func (de *IdleCounter) Done()

Done ...

func (*IdleCounter) Wait added in v0.69.0

func (de *IdleCounter) Wait(ctx context.Context)

Wait ...

type Log added in v0.70.0

type Log func(msg ...interface{})

Log type for Println

func MultiLogger added in v0.74.0

func MultiLogger(list ...Logger) Log

MultiLogger is similar to https://golang.org/pkg/io/#MultiWriter

func (Log) Println added in v0.70.0

func (l Log) Println(msg ...interface{})

Println interface

type Logger added in v0.70.0

type Logger interface {
	// Same as fmt.Printf
	Println(...interface{})
}

Logger interface

var LoggerQuiet Logger = Log(func(_ ...interface{}) {})

LoggerQuiet does nothing

type Sleeper added in v0.52.0

type Sleeper func(context.Context) error

Sleeper sleeps the current goroutine for sometime, returns the reason to wake, if ctx is done release resource

func BackoffSleeper added in v0.52.0

func BackoffSleeper(initInterval, maxInterval time.Duration, algorithm func(time.Duration) time.Duration) Sleeper

BackoffSleeper returns a sleeper that sleeps in a backoff manner every time get called. The sleep interval of the sleeper will grow from initInterval to maxInterval by the specified algorithm, then use maxInterval as the interval. If maxInterval is not greater than 0, the sleeper will wake immediately. If algorithm is nil, DefaultBackoff will be used.

func CountSleeper added in v0.52.0

func CountSleeper(max int) Sleeper

CountSleeper wakes immediately. When counts to the max returns *ErrMaxSleepCount

func EachSleepers added in v0.92.0

func EachSleepers(list ...Sleeper) Sleeper

EachSleepers returns a sleeper wakes up when each sleeper is awake. If a sleeper returns error, it will wake up immediately.

func RaceSleepers added in v0.92.0

func RaceSleepers(list ...Sleeper) Sleeper

RaceSleepers returns a sleeper wakes up when one of the sleepers wakes.

Directories

Path Synopsis
check-issue module
Package main A helper to run go test on CI with the right environment variables.
Package main A helper to run go test on CI with the right environment variables.
Package main ...
Package main ...
Package main ...
Package main ...
Package main ...
Package main ...
Package main ...
Package main ...
Package main It helps to launcher a transparent shell under the current shell with some extra environment variables that are required by rod testing.
Package main It helps to launcher a transparent shell under the current shell with some extra environment variables that are required by rod testing.
Package main ...
Package main ...

Jump to

Keyboard shortcuts

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