util

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 37 Imported by: 6

README

util

install

go get github.com/sqc157400661/util

Documentation

Index

Constants

View Source
const (
	B   float64 = 1
	KiB         = 1024 * B
	MiB         = 1024 * KiB
	GiB         = 1024 * MiB
)
View Source
const (
	TextBlack = iota + 30
	TextRed
	TextGreen
	TextYellow
	TextBlue
	TextMagenta
	TextCyan
	TextWhite
)
View Source
const FormatTime = "2006-01-02 15:04:05"

Variables

View Source
var DefaultEncryptor = Encryptor{
	// contains filtered or unexported fields
}

Functions

func AbsolutePath

func AbsolutePath(value string) (string, error)

Returns the absolute path of a file

func ArePodsAllReady added in v0.0.5

func ArePodsAllReady(pods []corev1.Pod) bool

func ArePodsAllScheduled added in v0.0.5

func ArePodsAllScheduled(pods []corev1.Pod) bool

func BaseName

func BaseName(filename string) string

Returns the base name of a file

func Bool added in v0.0.6

func Bool(v bool) *bool

Bool returns a pointer to v.

func BuildPodMap added in v0.0.5

func BuildPodMap(pods []corev1.Pod, keyFunc func(pod *corev1.Pod) string) map[string]*corev1.Pod

func ByteMap added in v0.0.6

func ByteMap(m *map[string][]byte)

ByteMap initializes m when it points to nil.

func BytesToString

func BytesToString(b []byte) string

BytesToString casts slice to string without copy

func ContainsOnlyFinalizer added in v0.0.5

func ContainsOnlyFinalizer(obj client.Object, finalizer string) bool

func ConvTimeInLocation

func ConvTimeInLocation(t time.Time, local string) (time.Time, error)

func ConvertToStruct added in v0.0.6

func ConvertToStruct(input map[string]interface{}, dst interface{}) error

func Cyan added in v0.0.4

func Cyan(msg string) string

func DirExists

func DirExists(filename string) bool

returns true if a given directory exists

func DirName

func DirName(filename string) string

Returns the directory name of a file

func ExitSignalHandler

func ExitSignalHandler(onExit func())

func FileExists

func FileExists(filename string) bool

returns true if a given file exists

func FileModTime

func FileModTime(filename string) int64

returns modify time of file

func FilterPodsBy added in v0.0.5

func FilterPodsBy(pods []corev1.Pod, filter func(pod *corev1.Pod) bool) []corev1.Pod

func FuncCaller added in v0.0.4

func FuncCaller() string

func GB added in v0.0.5

func GB(m *resource.Quantity) float64

func GenerateResource added in v0.0.5

func GenerateResource(cpu float64, mem float64) (re map[corev1.ResourceName]resource.Quantity)

func GetContainerFromPod added in v0.0.5

func GetContainerFromPod(pod *corev1.Pod, name string) *corev1.Container

func GetContainerFromPodSpec added in v0.0.5

func GetContainerFromPodSpec(podSpec *corev1.PodSpec, name string) *corev1.Container

func GetEnvVarFromSecret added in v0.0.5

func GetEnvVarFromSecret(sctName, name, key string, opt bool) corev1.EnvVar

func GetVarsFromTemplate added in v0.0.7

func GetVarsFromTemplate(tmpl string) []string

GetVarsFromTemplate Gets a list of all variables mentioned in a template

func GlobalTempDir

func GlobalTempDir() string

func Green added in v0.0.4

func Green(msg string) string

func IPAddress

func IPAddress() (ipAddr string, err error)

func InIntSlice

func InIntSlice(slice []int, value int) bool

func InStringSlice

func InStringSlice(slice []string, value string) bool

func Int32 added in v0.0.6

func Int32(v int32) *int32

Int32 returns a pointer to v.

func Int64 added in v0.0.6

func Int64(v int64) *int64

Int64 returns a pointer to v.

func InterfaceMapHash32 added in v0.0.6

func InterfaceMapHash32(msg map[string]interface{}) (string, error)

func IsAllPodsReady added in v0.0.5

func IsAllPodsReady(pods []corev1.Pod) bool

func IsPodDeleted added in v0.0.5

func IsPodDeleted(po *corev1.Pod) bool

func IsPodDeletedOrFailed added in v0.0.5

func IsPodDeletedOrFailed(po *corev1.Pod) bool

func IsPodFailed added in v0.0.5

func IsPodFailed(po *corev1.Pod) bool

func IsPodReady added in v0.0.5

func IsPodReady(pod *corev1.Pod) bool

func IsPodRunning added in v0.0.5

func IsPodRunning(pod *corev1.Pod) bool

func IsPodScheduled added in v0.0.5

func IsPodScheduled(po *corev1.Pod) bool

func IsRetryFailure

func IsRetryFailure(err error) bool

func IsStringSliceEqual

func IsStringSliceEqual(slice1, slice2 []string) bool

func LOC

func LOC() *time.Location

func LOCWithName

func LOCWithName(name string) *time.Location

func MD5Hash added in v0.0.6

func MD5Hash(input string) string

func Max added in v0.0.6

func Max(a, b uint64) uint64

Max returns the largest int64 that was passed in the arguments.

func Min added in v0.0.6

func Min(a, b uint64) uint64

Min returns the smallest int64 that was passed in the arguments.

func ParseCPUWithUnit added in v0.0.5

func ParseCPUWithUnit(cpu float64, unit string) (re resource.Quantity, err error)

func ParseMemoryWithUnit added in v0.0.5

func ParseMemoryWithUnit(mem float64, unit string) (re resource.Quantity, err error)

func PrintFatalError added in v0.0.4

func PrintFatalError(err error)

func PrintMsg added in v0.0.4

func PrintMsg(msg string, strs ...string)

func Red added in v0.0.4

func Red(msg string) string

func Retry

func Retry(interval time.Duration, maxRetries int, f ConditionFunc) error

Retry retries f every interval until after maxRetries. The interval won't be affected by how long f takes. For example, if interval is 3s, f takes 1s, another f will be called 2s later. However, if f takes longer than interval, it will be delayed.

func SafeTemplateFill added in v0.0.7

func SafeTemplateFill(tmpl string, data map[string]interface{}) (string, error)

SafeTemplateFill passed template string is formatted using its operands and returns the resulting string. It checks that the data was safely initialized

func SetColor added in v0.0.4

func SetColor(msg string, conf, bg, text int) string

func SizeToBytes added in v0.0.6

func SizeToBytes(s string) (float64, error)

SizeToBytes parses a string formatted by ByteSize as bytes.

func SlurpAsBytes

func SlurpAsBytes(filename string) ([]byte, error)

reads a file and returns its contents as a byte slice

func String added in v0.0.6

func String(v string) *string

String returns a pointer to v.

func StringMap added in v0.0.6

func StringMap(m *map[string]string)

StringMap initializes m when it points to nil.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes casts string to slice without copy

func Struct2Map added in v0.0.6

func Struct2Map(obj interface{}) (map[string]interface{}, error)

func TimeParseInLocal

func TimeParseInLocal(timeStr string) (*time.Time, error)

func TimeParseInLocation

func TimeParseInLocation(timeStr string, local string) (*time.Time, error)

func ToUpperStringSlice

func ToUpperStringSlice(slice []string) []string

func TrimmedLines added in v0.0.7

func TrimmedLines(s string) string

TrimmedLines Given a multi-line string, this function removes leading spaces from every line. It also removes the first line, if it is empty

func UnsafeMergeMap added in v0.0.6

func UnsafeMergeMap[K comparable, V any](tgt map[K]V, src map[K]V) map[K]V

func UnsafeMergeStringMap added in v0.0.6

func UnsafeMergeStringMap(tgt map[string]string, src map[string]string) map[string]string

func Unzip

func Unzip(zipFile, dest string) error

解压

func White added in v0.0.4

func White(msg string) string

func WriteRegularFile

func WriteRegularFile(fileName, text, directory string) (string, error)

func WriteString

func WriteString(line string, filename string) error

append a string into an existing file

func WriteStrings

func WriteStrings(lines []string, filename string, termination string) error

Writes a string slice into a file The file is created

func Yellow added in v0.0.4

func Yellow(msg string) string

func Zip

func Zip(files []*os.File, dest string) error

压缩文件 files 文件数组,可以是不同dir下的文件或者文件夹 dest 压缩文件存放地址

Types

type ConditionFunc

type ConditionFunc func() (bool, error)

type Encryptor added in v0.0.3

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

func NewEncryptor added in v0.0.3

func NewEncryptor(opts ...OptionFunc) *Encryptor

func (*Encryptor) Decrypt added in v0.0.3

func (e *Encryptor) Decrypt(ciphertext string) (string, error)

对密文进行多重解密

func (*Encryptor) DecryptSymmetric added in v0.0.3

func (e *Encryptor) DecryptSymmetric(ciphertext string) (string, error)

对密文进行对称解密

func (*Encryptor) Encrypt added in v0.0.3

func (e *Encryptor) Encrypt(plaintext string) (string, error)

对明文进行多重加密

func (*Encryptor) EncryptSymmetric added in v0.0.3

func (e *Encryptor) EncryptSymmetric(plaintext string) (string, error)

对明文进行对称加密

func (*Encryptor) ReplaceCharacters added in v0.0.3

func (e *Encryptor) ReplaceCharacters(plaintext string) string

对明文进行字符替换

func (*Encryptor) RestoreCharacters added in v0.0.3

func (e *Encryptor) RestoreCharacters(ciphertext string) string

对密文进行字符替换

type JSON6902 added in v0.0.5

type JSON6902 []interface{}

JSON6902 represents a JSON Patch according to RFC 6902; the same as k8s.io/apimachinery/pkg/types.JSONPatchType.

func NewJSONPatch added in v0.0.5

func NewJSONPatch() *JSON6902

NewJSONPatch creates a new JSON Patch according to RFC 6902; the same as k8s.io/apimachinery/pkg/types.JSONPatchType.

func (*JSON6902) Add added in v0.0.5

func (patch *JSON6902) Add(path ...string) func(value interface{}) *JSON6902

Add appends an "add" operation to patch.

> The "add" operation performs one of the following functions, > depending upon what the target location references: > > o If the target location specifies an array index, a new value is > inserted into the array at the specified index. > > o If the target location specifies an object member that does not > already exist, a new member is added to the object. > > o If the target location specifies an object member that does exist, > that member's value is replaced.

func (JSON6902) Bytes added in v0.0.5

func (patch JSON6902) Bytes() ([]byte, error)

Bytes returns the JSON representation of patch.

func (JSON6902) Data added in v0.0.5

func (patch JSON6902) Data(client.Object) ([]byte, error)

Data returns the JSON representation of patch.

func (JSON6902) IsEmpty added in v0.0.5

func (patch JSON6902) IsEmpty() bool

IsEmpty returns true when patch has no operations.

func (*JSON6902) Remove added in v0.0.5

func (patch *JSON6902) Remove(path ...string) *JSON6902

Remove appends a "remove" operation to patch.

> The "remove" operation removes the value at the target location. > > The target location MUST exist for the operation to be successful.

func (*JSON6902) Replace added in v0.0.5

func (patch *JSON6902) Replace(path ...string) func(value interface{}) *JSON6902

Replace appends a "replace" operation to patch.

> The "replace" operation replaces the value at the target location > with a new value. > > The target location MUST exist for the operation to be successful.

func (JSON6902) Type added in v0.0.5

func (patch JSON6902) Type() types.PatchType

Type returns k8s.io/apimachinery/pkg/types.JSONPatchType.

type Merge7386 added in v0.0.5

type Merge7386 map[string]interface{}

Merge7386 represents a JSON Merge Patch according to RFC 7386; the same as k8s.io/apimachinery/pkg/types.MergePatchType.

func NewMergePatch added in v0.0.5

func NewMergePatch() *Merge7386

NewMergePatch creates a new JSON Merge Patch according to RFC 7386; the same as k8s.io/apimachinery/pkg/types.MergePatchType.

func (*Merge7386) Add added in v0.0.5

func (patch *Merge7386) Add(path ...string) func(value interface{}) *Merge7386

Add modifies patch to indicate that the member at path should be added or replaced with value.

> If the provided merge patch contains members that do not appear > within the target, those members are added. If the target does > contain the member, the value is replaced. Null values in the merge > patch are given special meaning to indicate the removal of existing > values in the target.

func (Merge7386) Bytes added in v0.0.5

func (patch Merge7386) Bytes() ([]byte, error)

Bytes returns the JSON representation of patch.

func (Merge7386) Data added in v0.0.5

func (patch Merge7386) Data(client.Object) ([]byte, error)

Data returns the JSON representation of patch.

func (Merge7386) IsEmpty added in v0.0.5

func (patch Merge7386) IsEmpty() bool

IsEmpty returns true when patch has no modifications.

func (*Merge7386) PatchAnnos added in v0.0.5

func (patch *Merge7386) PatchAnnos(actual, old map[string]string)

func (*Merge7386) PatchByteMap added in v0.0.5

func (patch *Merge7386) PatchByteMap(path []string, actual, old map[string][]byte)

func (*Merge7386) PatchLabels added in v0.0.5

func (patch *Merge7386) PatchLabels(actual, old map[string]string)

func (*Merge7386) PatchStringMap added in v0.0.5

func (patch *Merge7386) PatchStringMap(path []string, actual, old map[string]string)

func (*Merge7386) Remove added in v0.0.5

func (patch *Merge7386) Remove(path ...string) *Merge7386

Remove modifies patch to indicate that the member at path should be removed if it exists.

func (Merge7386) Type added in v0.0.5

func (patch Merge7386) Type() types.PatchType

Type returns k8s.io/apimachinery/pkg/types.MergePatchType.

type OptionFunc added in v0.0.3

type OptionFunc func(o *Encryptor)

Option is an application option.

func Algorithm added in v0.0.3

func Algorithm(algorithm string) OptionFunc

func Iv added in v0.0.3

func Iv(iv string) OptionFunc

func Key added in v0.0.3

func Key(key string) OptionFunc

func ReplaceTable added in v0.0.3

func ReplaceTable(replaceTable string) OptionFunc

func ReplaceWith added in v0.0.3

func ReplaceWith(replaceWith string) OptionFunc

type RetryError

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

func (*RetryError) Error

func (e *RetryError) Error() string

Jump to

Keyboard shortcuts

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