util

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Copyright 2023 The Gitea Authors. All rights reserved. SPDX-License-Identifier: MIT

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidArgument  = errors.New("invalid argument")
	ErrPermissionDenied = errors.New("permission denied")
	ErrAlreadyExist     = errors.New("resource already exists")
	ErrNotExist         = errors.New("resource does not exist")
)

Common Errors forming the base of our error system

Many Errors returned by Gitea can be tested against these errors using errors.Is.

View Source
var ErrNotEmpty = errors.New("not-empty")

ErrNotEmpty is an error reported when there is a non-empty reader

Functions

func AESGCMDecrypt

func AESGCMDecrypt(key, ciphertext []byte) ([]byte, error)

AESGCMDecrypt (from legacy package): decrypts ciphertext with the given key using AES in GCM mode. should be replaced.

func AESGCMEncrypt

func AESGCMEncrypt(key, plaintext []byte) ([]byte, error)

AESGCMEncrypt (from legacy package): encrypts plaintext with the given key using AES in GCM mode. should be replaced.

func ApplyUmask

func ApplyUmask(f string, newMode os.FileMode) error

func CommonSkip

func CommonSkip(name string) bool

CommonSkip will check a provided name to see if it represents file or directory that should not be watched

func CopyFile

func CopyFile(src, dest string) error

CopyFile copies file from source to target path.

func CreatePublicKeyFingerprint

func CreatePublicKeyFingerprint(key crypto.PublicKey) ([]byte, error)

CreatePublicKeyFingerprint creates a fingerprint of the given key. The fingerprint is the sha256 sum of the PKIX structure of the key.

func CryptoRandomBytes

func CryptoRandomBytes(length int64) ([]byte, error)

CryptoRandomBytes generates `length` crypto bytes This differs from CryptoRandomString, as each byte in CryptoRandomString is generated by [0,61] range This function generates totally random bytes, each byte is generated by [0,255] range

func CryptoRandomInt

func CryptoRandomInt(limit int64) (int64, error)

CryptoRandomInt returns a crypto random integer between 0 and limit, inclusive

func CryptoRandomString

func CryptoRandomString(length int64) (string, error)

CryptoRandomString generates a crypto random alphanumerical string, each byte is generated by [0,61] range

func Debounce

func Debounce(d time.Duration) func(f func())

func FilePathJoinAbs

func FilePathJoinAbs(base string, sub ...string) string

FilePathJoinAbs joins the path elements into a single file path, each element is cleaned by filepath.Clean separately. All slashes/backslashes are converted to path separators before cleaning, the result only contains path separators. The first element must be an absolute path, caller should prepare the base path. It's caller's duty to make every element not bypass its own directly level, to avoid security issues. Like PathJoinRel, any redundant part (empty, relative dots, slashes) is removed.

{`/foo`, ``, `bar`} => `/foo/bar`
{`/foo`, `..`, `bar`} => `/foo/bar`

func FileURLToPath

func FileURLToPath(u *url.URL) (string, error)

FileURLToPath extracts the path information from a file://... url.

func GenerateKeyPair

func GenerateKeyPair(bits int) (string, string, error)

GenerateKeyPair generates a public and private keypair

func GetLuminance

func GetLuminance(r, g, b float64) float64

return luminance given RGB channels Reference from: https://www.w3.org/WAI/GL/wiki/Relative_luminance

func HexToRBGColor

func HexToRBGColor(colorString string) (float64, float64, float64)

Get color as RGB values in 0..255 range from the hex color string (with or without #)

func HomeDir

func HomeDir() (home string, err error)

HomeDir returns path of '~'(in Linux) on Windows, it returns error when the variable does not exist.

func IsDir

func IsDir(dir string) (bool, error)

IsDir returns true if given path is a directory, or returns false when it's a file or does not exist.

func IsEmptyReader

func IsEmptyReader(r io.Reader) (err error)

IsEmptyReader reads a reader and ensures it is empty

func IsEmptyString

func IsEmptyString(s string) bool

IsEmptyString checks if the provided string is empty

func IsExist

func IsExist(path string) (bool, error)

IsExist checks whether a file or directory exists. It returns false when the file or directory does not exist.

func IsFile

func IsFile(filePath string) (bool, error)

IsFile returns true if given path is a file, or returns false when it's a directory or does not exist.

func IsReadmeFileExtension

func IsReadmeFileExtension(name string, ext ...string) (int, bool)

IsReadmeFileExtension reports whether name looks like a README file based on its name. It will look through the provided extensions and check if the file matches one of the extensions and provide the index in the extension list. If the filename is `readme.` with an unmatched extension it will match with the index equaling the length of the provided extension list. Note that the '.' should be provided in ext, e.g ".md"

func IsReadmeFileName

func IsReadmeFileName(name string) bool

IsReadmeFileName reports whether name looks like a README file based on its name.

func Max

func Max(a, b int) int

Max max of two ints

func Min

func Min(a, b int) int

Min min of two ints

func NewAlreadyExistErrorf

func NewAlreadyExistErrorf(message string, args ...interface{}) error

NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist

func NewInvalidArgumentErrorf

func NewInvalidArgumentErrorf(message string, args ...interface{}) error

NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument

func NewNotExistErrorf

func NewNotExistErrorf(message string, args ...interface{}) error

NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist

func NewPermissionDeniedErrorf

func NewPermissionDeniedErrorf(message string, args ...interface{}) error

NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied

func NewSilentWrapErrorf

func NewSilentWrapErrorf(unwrap error, message string, args ...interface{}) error

NewSilentWrapErrorf returns an error that formats as the given text but unwraps as the provided error

func NormalizeEOL

func NormalizeEOL(input []byte) []byte

NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)

func PackData

func PackData(data ...interface{}) ([]byte, error)

PackData uses gob to encode the given data in sequence

func PaginateSlice

func PaginateSlice(list interface{}, page, pageSize int) interface{}

PaginateSlice cut a slice as per pagination options if page = 0 it do not paginate

func PathEscapeSegments

func PathEscapeSegments(path string) string

PathEscapeSegments escapes segments of a path while not escaping forward slash

func PathJoinRel

func PathJoinRel(elem ...string) string

PathJoinRel joins the path elements into a single path, each element is cleaned by path.Clean separately. It only returns the following values (like path.Join), any redundant part (empty, relative dots, slashes) is removed. It's caller's duty to make every element not bypass its own directly level, to avoid security issues.

empty => ``
`` => ``
`..` => `.`
`dir` => `dir`
`/dir/` => `dir`
`foo\..\bar` => `foo\..\bar`
{`foo`, ``, `bar`} => `foo/bar`
{`foo`, `..`, `bar`} => `foo/bar`

func PathJoinRelX

func PathJoinRelX(elem ...string) string

PathJoinRelX joins the path elements into a single path like PathJoinRel, and covert all backslashes to slashes. (X means "extended", also means the combination of `\` and `/`). It's caller's duty to make every element not bypass its own directly level, to avoid security issues. It returns similar results as PathJoinRel except:

`foo\..\bar` => `bar`  (because it's processed as `foo/../bar`)

All backslashes are handled as slashes, the result only contains slashes.

func ReadAtMost

func ReadAtMost(r io.Reader, buf []byte) (n int, err error)

ReadAtMost reads at most len(buf) bytes from r into buf. It returns the number of bytes copied. n is only less than len(buf) if r provides fewer bytes. If EOF occurs while reading, err will be nil.

func Remove

func Remove(name string) error

Remove removes the named file or (empty) directory with at most 5 attempts.

func RemoveAll

func RemoveAll(name string) error

RemoveAll removes the named file or (empty) directory with at most 5 attempts.

func Rename

func Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath with at most 5 attempts.

func SanitizeCredentialURLs

func SanitizeCredentialURLs(s string) string

SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:pass@domain.com" => "https://sanitized-credential@domain.com"

func SanitizeErrorCredentialURLs

func SanitizeErrorCredentialURLs(err error) error

SanitizeErrorCredentialURLs wraps the error and make sure the returned error message doesn't contain sensitive credentials in URLs

func SecToTime

func SecToTime(duration int64) string

SecToTime converts an amount of seconds to a human-readable string. E.g. 66s -> 1 minute 6 seconds 52410s -> 14 hours 33 minutes 563418 -> 6 days 12 hours 1563418 -> 2 weeks 4 days 3937125s -> 1 month 2 weeks 45677465s -> 1 year 6 months

func ShellEscape

func ShellEscape(toEscape string) string

ShellEscape will escape the provided string. We can't just use go-shellquote here because our preferences for escaping differ from those in that we want:

* If the string doesn't require any escaping just leave it as it is. * If the string requires any escaping prefer double quote escaping * If we have ! or newlines then we need to use single quote escaping

func SliceContains

func SliceContains[T comparable](slice []T, target T) bool

SliceContains returns true if the target exists in the slice.

func SliceContainsFunc

func SliceContainsFunc[T any](slice []T, targetFunc func(T) bool) bool

SliceContainsFunc returns true if any element in the slice satisfies the targetFunc.

func SliceContainsString

func SliceContainsString(slice []string, target string, insensitive ...bool) bool

SliceContainsString sequential searches if string exists in slice.

func SliceEqual

func SliceEqual[T comparable](s1, s2 []T) bool

SliceEqual returns true if the two slices are equal.

func SliceRemoveAll

func SliceRemoveAll[T comparable](slice []T, target T) []T

SliceRemoveAll removes all the target elements from the slice.

func SliceRemoveAllFunc

func SliceRemoveAllFunc[T comparable](slice []T, targetFunc func(T) bool) []T

SliceRemoveAllFunc removes all elements which satisfy the targetFunc from the slice.

func SliceSortedEqual

func SliceSortedEqual[T comparable](s1, s2 []T) bool

SliceSortedEqual returns true if the two slices will be equal when they get sorted. It doesn't require that the slices have been sorted, and it doesn't sort them either.

func SplitStringAtByteN

func SplitStringAtByteN(input string, n int) (left, right string)

SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)

func StatDir

func StatDir(rootPath string, includeDir ...bool) ([]string, error)

StatDir gathers information of given directory by depth-first. It returns slice of file list and includes subdirectories if enabled; it returns error and nil slice when error occurs in underlying functions, or given path is not a directory or does not exist.

Slice does not include given path itself. If subdirectories is enabled, they will have suffix '/'.

func ToFloat64

func ToFloat64(number interface{}) (float64, error)

ToFloat64 transform a given int into float64.

func ToInt64

func ToInt64(number interface{}) (int64, error)

ToInt64 transform a given int into int64.

func ToSnakeCase

func ToSnakeCase(input string) string

ToSnakeCase convert the input string to snake_case format.

Some samples.

"FirstName"  => "first_name"
"HTTPServer" => "http_server"
"NoHTTPS"    => "no_https"
"GO_PATH"    => "go_path"
"GO PATH"    => "go_path"      // space is converted to underscore.
"GO-PATH"    => "go_path"      // hyphen is converted to underscore.

func ToTitleCase

func ToTitleCase(s string) string

ToTitleCase returns s with all english words capitalized

func ToTitleCaseNoLower

func ToTitleCaseNoLower(s string) string

ToTitleCaseNoLower returns s with all english words capitalized without lower-casing

func ToUpperASCII

func ToUpperASCII(s string) string

ToUpperASCII returns s with all ASCII letters mapped to their upper case.

func URLJoin

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

URLJoin joins url components, like path.Join, but preserving contents

func UnpackData

func UnpackData(buf []byte, data ...interface{}) error

UnpackData uses gob to decode the given data in sequence

func UseLightTextOnBackground

func UseLightTextOnBackground(r, g, b float64) bool

Reference from: https://firsching.ch/github_labels.html In the future WCAG 3 APCA may be a better solution. Check if text should use light color based on RGB of background

Types

type OptionalBool

type OptionalBool byte

OptionalBool a boolean that can be "null"

const (
	// OptionalBoolNone a "null" boolean value
	OptionalBoolNone OptionalBool = iota
	// OptionalBoolTrue a "true" boolean value
	OptionalBoolTrue
	// OptionalBoolFalse a "false" boolean value
	OptionalBoolFalse
)

func OptionalBoolOf

func OptionalBoolOf(b bool) OptionalBool

OptionalBoolOf get the corresponding OptionalBool of a bool

func OptionalBoolParse

func OptionalBoolParse(s string) OptionalBool

OptionalBoolParse get the corresponding OptionalBool of a string using strconv.ParseBool

func (OptionalBool) IsFalse

func (o OptionalBool) IsFalse() bool

IsFalse return true if equal to OptionalBoolFalse

func (OptionalBool) IsNone

func (o OptionalBool) IsNone() bool

IsNone return true if equal to OptionalBoolNone

func (OptionalBool) IsTrue

func (o OptionalBool) IsTrue() bool

IsTrue return true if equal to OptionalBoolTrue

type SilentWrap

type SilentWrap struct {
	Message string
	Err     error
}

SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist'

func (SilentWrap) Error

func (w SilentWrap) Error() string

Error returns the message

func (SilentWrap) Unwrap

func (w SilentWrap) Unwrap() error

Unwrap returns the underlying error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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