utils

package
v0.9.280 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: AGPL-3.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BYTE = 1.0 << (10 * iota)
	KIBIBYTE
	MEBIBYTE
	GIBIBYTE
)
View Source
const MaxTimeAgo = 24 * 365 * 20 * time.Hour // 20 years

Variables

This section is empty.

Functions

func AliasCommandRun added in v0.9.237

func AliasCommandRun(src *cobra.Command) func(cmd *cobra.Command, args []string)

Use this for Run to make a command an alias to another command's Run. Invokes all persistent hooks for all parents as well.

func AliasCommandRunE added in v0.9.237

func AliasCommandRunE(src *cobra.Command) func(cmd *cobra.Command, args []string) error

Use this for RunE to make a command an alias to another command's RunE. Invokes all persistent hooks for all parents as well.

func AliasCommandUse added in v0.9.237

func AliasCommandUse(aliasOf *cobra.Command, name ...string) string

func AliasOf added in v0.9.237

func AliasOf(src *cobra.Command, name ...string) *cobra.Command

AliasOf creates an alias of the provided command, That includes the same flags and hooks. Even the parent command's PersistentPreRunE and PersistentPostRunE hooks are invoked. Provide a name only if it's different from provided command's name

func ChownAll added in v0.9.238

func ChownAll(path string, uid, gid int) error

func CloseCommonFds added in v0.9.237

func CloseCommonFds(ctx context.Context, parentPID, childPID int32) error

CloseCommonFdscloses any common FDs between the parent and child process

func Confirm added in v0.9.237

func Confirm(ctx context.Context, msg string) bool

func CopyFile

func CopyFile(src, dest string) error

func FileMD5Sum added in v0.9.237

func FileMD5Sum(path string) (string, error)

Computes the MD5 checksum of the given files (read in order)

func FillHost added in v0.9.237

func FillHost(ctx context.Context, state *daemon.Host) error

func FillProcessState added in v0.9.237

func FillProcessState(ctx context.Context, pid uint32, state *daemon.ProcessState, tree ...bool) error

Tries to fill as much as possible of the process state. Only returns early if the process does not exist at all.

func FullUse added in v0.9.237

func FullUse(cmd *cobra.Command) string

func FunctionName added in v0.9.237

func FunctionName(pc uintptr) string

FunctionName returns the name of the function pointed to by f

func GRPCError added in v0.9.237

func GRPCError(err error, extra ...string) error

func GRPCErrorColored added in v0.9.237

func GRPCErrorColored(err error, extra ...string) error

func GRPCErrorColoredShort added in v0.9.237

func GRPCErrorColoredShort(err error, extra ...string) error

func GRPCErrorShort added in v0.9.237

func GRPCErrorShort(err error, extra ...string) error

func GetCmd added in v0.9.237

func GetCmd(ctx context.Context, pid uint32) (string, []string, error)

func GetCredentials added in v0.9.237

func GetCredentials() (*syscall.Credential, error)

func GetFreePort added in v0.9.230

func GetFreePort() (int, error)

GetFreePort returns a free random port on the host

func GetHost added in v0.9.237

func GetHost(ctx context.Context) (*daemon.Host, error)

func GetMACAddress added in v0.9.225

func GetMACAddress() (string, error)

func GetProcessState added in v0.9.237

func GetProcessState(ctx context.Context, pid uint32, tree ...bool) (*daemon.ProcessState, error)

func GetRootCredentials added in v0.9.237

func GetRootCredentials() *syscall.Credential

func GetSID added in v0.9.237

func GetSID(pid uint32) (uint32, error)

func GetTag added in v0.9.237

func GetTag(i any, field string, tag string) string

GetTag returns the value of the tag with the given name for the field with the given name. Nested fields can be specified by separating them with a period, and the returned tag will also be separated by periods. If the field does not exist, or the tag does not exist, an empty string will be returned.

func GetValue added in v0.9.237

func GetValue(i any, field string) any

GetValue returns the value of the field with the given name. If the field does not exist, or is not set, the zero value for the field type will be returned. Nested fields can be specified by separating them with a period.

func Getenv added in v0.9.246

func Getenv(env []string, key string, defaultValue ...string) string

Gets the last value of an env var from a list of env vars

func Gibibytes added in v0.9.237

func Gibibytes(bytes int64) int64

func HasActiveTCPConnections added in v0.9.237

func HasActiveTCPConnections(pid uint32) (bool, error)

Checks if the given process has any active tcp connections

func Int32ToIntSlice added in v0.9.268

func Int32ToIntSlice(slice []int32) []int

func Int32ToUint32Slice added in v0.9.237

func Int32ToUint32Slice(slice []int32) []uint32

func IntToInt32Slice added in v0.9.237

func IntToInt32Slice(slice []int) []int32

func IntToUint32Slice added in v0.9.237

func IntToUint32Slice(slice []int) []uint32

func IsCallerSameAsUs added in v0.9.237

func IsCallerSameAsUs(caller string) bool

func IsRootUser added in v0.9.237

func IsRootUser() bool

func IsTTY added in v0.9.237

func IsTTY(path string) (bool, error)

func Kibibytes added in v0.9.237

func Kibibytes(bytes int64) int64

func LastLine added in v0.9.238

func LastLine(s string) string

func ListFields added in v0.9.237

func ListFields(i any, tag ...string) []string

ListFields returns a list of field names for the given struct. If a tag is specified, it will use the tag value instead of the field name.

func ListFilesInDir added in v0.9.237

func ListFilesInDir(dir string) ([]string, error)

func ListLeaves added in v0.9.237

func ListLeaves(i any, tag ...string) []string

LeavesList returns a list of field names for the given struct If a field is a struct, it will recursively call itself to get the fields. If a tag is specified, it will use the tag value instead of the field name. Nested fields are separated by a period.

func LoadJSONFromFile added in v0.9.237

func LoadJSONFromFile(path string, data any, fs ...afero.Fs) error

func LogProtoMessage added in v0.9.237

func LogProtoMessage(msg proto.Message, subject string, level zerolog.Level)

func Mebibytes added in v0.9.237

func Mebibytes(bytes int64) int64

func ParseHttpBody added in v0.9.268

func ParseHttpBody(body io.ReadCloser) (string, error)

func PathExists added in v0.9.237

func PathExists(path string) bool

func PidExists added in v0.9.233

func PidExists(pid uint32) bool

PidExists checks if a process with the given PID exists

func PidRunning added in v0.9.244

func PidRunning(pid uint32) bool

PidRunning checks if a process with the given PID is running

func ProtoToJSON added in v0.9.237

func ProtoToJSON(payload any) string

func SaveJSONToFile added in v0.9.237

func SaveJSONToFile(data any, path string, fs ...afero.Fs) error

func SimplifyFuncName added in v0.9.237

func SimplifyFuncName(f string) (category string, name string)

SimplifyFuncName simplifies the function name to a category and a name. Removes the long package prefix. If the function belongs to a plugin, the plugin name is returned.

func SizeFromPath added in v0.9.237

func SizeFromPath(path string) int64

SizeFromPath returns the size of the file or directory at the provided path.

func SizeStr added in v0.9.237

func SizeStr(bytes int64) string

func StrList added in v0.9.237

func StrList(strs []string) string

Prints a string list as comma separated string.

func StringToUint32Slice added in v0.9.237

func StringToUint32Slice(slice []string) []uint32

func TimeAgo added in v0.9.237

func TimeAgo(t time.Time) string

func Uint32ToStringSlice added in v0.9.237

func Uint32ToStringSlice(slice []uint32) []string

func VSOCKDialer added in v0.9.237

func VSOCKDialer(contextId uint32, port uint32) func(context.Context, string) (net.Conn, error)

func WaitForFile added in v0.9.238

func WaitForFile[T any](ctx context.Context, path string, timeout <-chan T) ([]byte, error)

func WaitForPid added in v0.9.228

func WaitForPid(pid uint32) chan int

Returns a channel that will be closed when a non-child process exits Since, we cannot use the process.Wait() method to wait for a non-child process to exit It waits until the process is a zombie process

func WaitForPidCtx added in v0.9.237

func WaitForPidCtx(ctx context.Context, pid uint32) chan int

Returns a channel that will be closed when a non-child process exits. Since, we cannot use the process.Wait() method to wait for a non-child process to exit. It waits until the process is a zombie process, or the process is not found.

func WalkTree added in v0.9.253

func WalkTree[T any](
	node any,
	valuesField string,
	childrenField string,
	fn func(T) bool,
) bool

WalkTree recursively walks a tree-structured struct using reflection, calling fn for every element in the specified valuesField slice. If fn returns false, the walk stops immediately. If true, it continues. childrenField specifies which struct field contains the slice of child nodes. The field names should match exactly (including capitalization).

Types

type FdInfo added in v0.9.237

type FdInfo struct {
	MntId uint64
	Inode uint64
}

func GetFdInfo added in v0.9.237

func GetFdInfo(pid uint32, fd int) (*FdInfo, error)

FdInfo returns file descriptor information for the provided process and file descriptor.

type HttpBody added in v0.9.268

type HttpBody struct {
	Message string `json:"message"`
}

type LinkDep added in v0.9.237

type LinkDep struct {
	Name         string
	Path         string // Could be a symlink
	ResolvedPath string // Resolved path of the symlink
}

func GetLinkDeps added in v0.9.237

func GetLinkDeps(binary string) ([]LinkDep, error)

GetLinkDeps returns the link dependencies of a binary Also resolves any symlinks in the path

Jump to

Keyboard shortcuts

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