utils

package
v0.0.0-...-f5a5a56 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractDomain

func ExtractDomain(rawURL string) string

ExtractDomain extracts the domain from a URL

func Hash

func Hash(input string) string

Hash creates a SHA256 hash of the input string

func HashBytes

func HashBytes(input []byte) string

HashBytes creates a SHA256 hash of the input bytes

func IsValidURL

func IsValidURL(rawURL string) bool

IsValidURL checks if a string is a valid URL

func MakeAbsoluteURL

func MakeAbsoluteURL(baseURL, relativeURL string) string

MakeAbsoluteURL converts a relative URL to absolute based on base URL

func NormalizeURL

func NormalizeURL(rawURL string) (string, error)

NormalizeURL normalizes a URL for consistent processing

Types

type ExecOptions

type ExecOptions struct {
	Timeout       time.Duration
	WorkingDir    string
	Env           []string
	Input         string
	IgnoreError   bool
	CaptureOutput bool
	StreamOutput  bool
	OutputHandler func(line string)
	MaxMemoryMB   int
	MaxOutputSize int64
}

ExecOptions provides options for command execution

type ExecResult

type ExecResult struct {
	Command    string        `json:"command"`
	Args       []string      `json:"args"`
	Stdout     string        `json:"stdout"`
	Stderr     string        `json:"stderr"`
	ExitCode   int           `json:"exit_code"`
	Duration   time.Duration `json:"duration"`
	StartTime  time.Time     `json:"start_time"`
	EndTime    time.Time     `json:"end_time"`
	Success    bool          `json:"success"`
	Error      string        `json:"error,omitempty"`
	PID        int           `json:"pid"`
	MemoryUsed int64         `json:"memory_used"`
	CPUUsage   float64       `json:"cpu_usage"`
}

ExecResult contains the results of command execution

type ExecWrapper

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

ExecWrapper provides safe execution of external tools with logging and monitoring

func NewExecWrapper

func NewExecWrapper(logger zerolog.Logger) *ExecWrapper

NewExecWrapper creates a new execution wrapper

func (*ExecWrapper) CheckBinary

func (e *ExecWrapper) CheckBinary(binary string) error

CheckBinary verifies that a binary exists and is executable

func (*ExecWrapper) Execute

func (e *ExecWrapper) Execute(ctx context.Context, command string, args []string, options ...*ExecOptions) (*ExecResult, error)

Execute runs a command and returns the complete result

func (*ExecWrapper) ExecuteSimple

func (e *ExecWrapper) ExecuteSimple(ctx context.Context, command string, args ...string) (*ExecResult, error)

ExecuteSimple runs a command with default options

func (*ExecWrapper) ExecuteStream

func (e *ExecWrapper) ExecuteStream(ctx context.Context, command string, args []string, options ...*ExecOptions) (*StreamingExecResult, error)

ExecuteStream runs a command with streaming output

func (*ExecWrapper) GetProcessInfo

func (e *ExecWrapper) GetProcessInfo(pid int) (*ProcessInfo, error)

GetProcessInfo returns information about a running process

func (*ExecWrapper) GetVersion

func (e *ExecWrapper) GetVersion(ctx context.Context, binary string) (string, error)

GetVersion attempts to get the version of a binary

func (*ExecWrapper) InstallBinary

func (e *ExecWrapper) InstallBinary(ctx context.Context, binary string, installCmd string) error

InstallBinary attempts to install a binary using package managers

func (*ExecWrapper) IsProcessRunning

func (e *ExecWrapper) IsProcessRunning(pid int) bool

IsProcessRunning checks if a process is still running

func (*ExecWrapper) KillProcess

func (e *ExecWrapper) KillProcess(pid int) error

KillProcess kills a process by PID

func (*ExecWrapper) SafeExecute

func (e *ExecWrapper) SafeExecute(ctx context.Context, command string, args []string, limits ResourceLimits) (*ExecResult, error)

SafeExecute provides additional safety checks and resource limits

func (*ExecWrapper) SetEnv

func (e *ExecWrapper) SetEnv(env []string)

SetEnv sets the default environment variables

func (*ExecWrapper) SetTimeout

func (e *ExecWrapper) SetTimeout(timeout time.Duration)

SetTimeout sets the default execution timeout

func (*ExecWrapper) SetWorkingDir

func (e *ExecWrapper) SetWorkingDir(dir string)

SetWorkingDir sets the default working directory

type ProcessInfo

type ProcessInfo struct {
	PID         int         `json:"pid"`
	Running     bool        `json:"running"`
	MemoryUsage int64       `json:"memory_usage"`
	CPUUsage    float64     `json:"cpu_usage"`
	StartTime   time.Time   `json:"start_time"`
	Process     *os.Process `json:"-"`
}

ProcessInfo contains information about a process

type ResourceLimits

type ResourceLimits struct {
	Timeout       time.Duration
	MaxMemoryMB   int
	MaxCPUPercent float64
	MaxProcesses  int
}

ResourceLimits defines resource constraints for command execution

type StreamingExecResult

type StreamingExecResult struct {
	Command   string
	Args      []string
	PID       int
	StartTime time.Time
	Stdout    chan string
	Stderr    chan string
	Done      chan ExecResult
	Cancel    context.CancelFunc
}

StreamingExecResult provides real-time output streaming

Jump to

Keyboard shortcuts

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