utils

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 20 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LoggingHttpTransport = &loghttp.Transport{
		LogRequest: func(req *http.Request) {
			log.Trace().Msgf("[%p] %s %s %v", req, req.Method, req.URL, req.Header)
		},
		LogResponse: func(resp *http.Response) {
			log.Trace().Msgf("[%p] %d %s", resp.Request, resp.StatusCode, resp.Request.URL)
		},
	}
)

Functions

func Chain added in v0.1.0

func Chain[T any](slices ...[]T) []T

Chain is an alias for MultiAppend

func CopyFile added in v0.1.0

func CopyFile(dst, src string) error

func Count added in v0.1.0

func Count(v ...bool) int

Count returns the count of true values

func DeepCopy added in v0.1.0

func DeepCopy[T any](to T, from T, opts ...options.MustOption[copier.Option]) error

ensure from and to are the same type

func DoneOrSleep

func DoneOrSleep(ctx context.Context, duration time.Duration) bool

func FilterSliceToMap added in v0.1.0

func FilterSliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (bool, K, V)) map[K]V

FilterSliceToMap iterates over a slice, turning T items into (K, V) associations, with possible filtering iteratee returns (bool, K, V): when the bool is false, K=>V is not included in the return value adapted from samber/lo

func FindOrError added in v0.1.0

func FindOrError[T any](collection []T, err error, predicate func(item T) bool) (zero T, errNil error)

FindOrError returns the first T in collection which matches the predicate, if none match, err is returned

func Htpasswd

func Htpasswd(user, pass string) (string, error)

func IsEmailValid

func IsEmailValid(e string) bool

IsEmailValid checks if the email provided passes the required structure and length. https://golangcode.com/validate-an-email-address/

func MakeHTTPS

func MakeHTTPS(url string) string

func MapPtr added in v0.1.0

func MapPtr[I, O any](in *I, f func(I) O) *O

MapPtr runs f on the pointer value if it is not nil, or returns nil

func MultiAppend added in v0.1.0

func MultiAppend[T any](slices ...[]T) []T

func Must added in v0.1.0

func Must[T any](v T, err error) T

please only use this for testing

func MustOk added in v0.1.0

func MustOk[T any](v T, ok bool) T

please only use this for testing

func NewExecRunner added in v0.1.0

func NewExecRunner() execRunner

TODO: refactor RunWithOptV2 to this

func Ptr added in v0.1.0

func Ptr[T any](in T) *T

func PtrSlice added in v0.1.0

func PtrSlice[T any](in []T) []*T

func ReverseSlice

func ReverseSlice(s interface{})

func Run added in v0.1.0

func Run(cmds []string, dir string, env []string) error

func RunWithCommandOption added in v0.1.0

func RunWithCommandOption(cmds []string, dir string, env []string) zoptions.Option[RunOpt]

func RunWithOpt added in v0.1.0

func RunWithOpt(opt RunOpt) error

func RunWithOptV2 added in v0.1.0

func RunWithOptV2(options ...zoptions.Option[RunOpt]) error

TODO: refactor RunWithOpt to this

func RunWithParentEnv added in v0.1.0

func RunWithParentEnv(cmds []string, dir string, env []string, options ...func(opt *RunOpt)) error

RunWithParentEnv runs cmds in dir with additional environment variables supplied by env, in addition to the current process' environment (from os.Environ)

func RunWithParentEnvOption added in v0.1.0

func RunWithParentEnvOption(opt *RunOpt) error

RunWithParentEnv runs cmds in dir with additional environment variables supplied by env, in addition to the current process' environment (from os.Environ)

func RunWithStdoutOption added in v0.1.0

func RunWithStdoutOption(stdout io.Writer) zoptions.Option[RunOpt]

func SliceContains added in v0.1.0

func SliceContains[S ~[]T, T comparable](items S, el T) bool

func SliceFindUniques added in v0.1.0

func SliceFindUniques[T comparable](collection []T) []T

func SliceOfPointersToValue added in v0.1.0

func SliceOfPointersToValue[S []*E, E any](s S) []E

func SliceOfValuesToPointer added in v0.1.0

func SliceOfValuesToPointer[S []E, E any](s S) []*E

func TrimURLScheme added in v0.1.0

func TrimURLScheme(url string) string

TrimURLScheme removes https:// or http:// scheme from the start of a url string

func TryFilter added in v0.1.0

func TryFilter[T any, R any](collection []T, iteratee func(item T, index int) (bool, R, error)) ([]R, error)

TryFilter filters a collection, but can abort when an error is returned by the iteratee when an error is returned, the collection return value will be empty

func TryMap added in v0.1.0

func TryMap[T any, R any](collection []T, mapper func(item T) (R, error)) ([]R, error)

TryMap maps collection through a fallible mapper, returning the mapped collection and any error when an error is returned, the failed inputs will have a corresponding zero value in the returned collection

func URLJoin

func URLJoin(base string, paths ...string) string

func ValueSlice added in v0.1.0

func ValueSlice[T any](in []*T) []T

func WriteLinesToFile added in v0.1.0

func WriteLinesToFile(filename string, lines []string) error

Types

type CommandRunner added in v0.1.0

type CommandRunner interface {
	RunWithOpt(options ...zoptions.Option[RunOpt]) error
}

type RunOpt added in v0.1.0

type RunOpt struct {
	Name string
	Args []string
	Dir  string
	// nil runs with current process environment
	Env    []string
	Stdin  io.Reader
	Stdout io.Writer

	// If true, RunWithOpt will return error as-is without wrapping it
	ExpectsError bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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