utils

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 15 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateExcl

func CreateExcl(path string, perm os.FileMode) (*os.File, error)

CreateExcl creates a new file at the given path with the specified permissions, failing if the file exists or if the path is a symbolic or hard link.

func CreateNoLinks(path string, perm os.FileMode) (*os.File, error)

CreateNoLinks creates or truncates the file at the given path with the specified permissions, failing if the path is a symbolic or hard link.

func GetAuthConfig

func GetAuthConfig(_ context.Context, optionalCert *x509.Certificate) (*tls.Config, error)

GetAuthConfig returns a TLS configuration for secure communication with the server.

func GetAuthContext

func GetAuthContext(ctx context.Context, tokenPath string) context.Context

GetAuthContext creates a new context with an authorization header.

func GetPerRPCCreds

func GetPerRPCCreds(tokenPath string) oauth.TokenSource

GetPerRPCCreds returns a PerRPCCredentials object that can be used for gRPC authentication.

func NoNullByteString

func NoNullByteString(s string) bool

NoNullByteString checks if the provided string does not contain any null bytes.

func NoNullByteURL

func NoNullByteURL(u string) bool

NoNullByteURL checks if the provided URL string does not contain any null bytes.

func OpenNoLinks(path string) (*os.File, error)

OpenNoLinks opens the file at the given path for reading, failing if the path is a symbolic or hard link.

func ParseSizeToMB added in v1.7.1

func ParseSizeToMB(sizeStr string) (uint64, error)

ParseSizeToMB converts a size string (e.g., "16 GB", "1024 MB", "1 TB") to MB.

func ReadFileNoLinks(path string) ([]byte, error)

ReadFileNoLinks reads and returns the contents of the file at the given path, failing if the path is a symbolic or hard link.

func ReadFileTrimmed added in v1.7.1

func ReadFileTrimmed(path string) (string, error)

ReadFileTrimmed reads a file and returns its content as a string with whitespace trimmed.

func ReadFromCommand added in v1.7.1

func ReadFromCommand(executor CmdExecutor, command string, args ...string) (stdout []byte, err error)

ReadFromCommand executes a command in the operating system and returns the output, It returns the output from stdout and error if exists.

func TrimPrefix added in v1.7.1

func TrimPrefix(s, prefix string) string

TrimPrefix removes the specified prefix from the string and trims any leading or trailing whitespace.

func TrimSpace added in v1.7.1

func TrimSpace(s string) string

TrimSpace removes leading and trailing whitespace from the string.

func TrimSpaceInBytes added in v1.7.1

func TrimSpaceInBytes(b []byte) string

TrimSpaceInBytes converts a byte slice to a string and trims leading and trailing whitespace.

Types

type CmdExecutor added in v1.7.1

type CmdExecutor = func(name string, args ...string) Command

CmdExecutor is a function type that takes a command name and its arguments, and returns a Command interface instance that can execute the command.

type Command added in v1.7.1

type Command interface {
	// Output executes the command and returns its standard output as a byte slice.
	Output() ([]byte, error)
	// SetStderr sets the standard error output for the command to a provided strings.Builder.
	SetStderr(stderr *strings.Builder)
}

Command interface defines the methods required for executing a command and capturing its output.

func ExecCmdExecutor added in v1.7.1

func ExecCmdExecutor(name string, args ...string) Command

ExecCmdExecutor is a function that creates an ExecCmd instance for executing commands.

type ExecCmd added in v1.7.1

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

ExecCmd is a struct that implements the Command interface using the exec package to run commands.

func (*ExecCmd) Output added in v1.7.1

func (e *ExecCmd) Output() ([]byte, error)

Output executes the command and returns its standard output as a byte slice.

func (*ExecCmd) SetStderr added in v1.7.1

func (e *ExecCmd) SetStderr(stderr *strings.Builder)

SetStderr sets the standard error output for the command to a provided strings.Builder.

Jump to

Keyboard shortcuts

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