osutil

package
v0.3.29-beta Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package osutil provides common utility functions for kodelet including content formatting with line numbers, process management, language detection, domain filtering, and various helper functions used across the application.

Index

Constants

View Source
const (
	// DomainRefreshInterval defines how often to reload the domains file
	DomainRefreshInterval = 30 * time.Second
)
View Source
const GracefulShutdownDelay = 2 * time.Second

GracefulShutdownDelay is the time to wait for graceful shutdown before sending SIGKILL. 2 seconds provides enough time for most processes to flush buffers, close connections, and perform cleanup, while not adding excessive delay after a timeout.

Variables

View Source
var DetachSysProcAttr = syscall.SysProcAttr{
	Setpgid: true,
	Pgid:    0,
}

DetachSysProcAttr provides syscall attributes for detaching processes on Unix systems

Functions

func ContentWithLineNumber

func ContentWithLineNumber(lines []string, offset int) string

ContentWithLineNumber formats a slice of strings by prefixing each line with its line number starting from the given offset, with appropriate padding for alignment.

func DetectLanguageFromPath

func DetectLanguageFromPath(filePath string) string

DetectLanguageFromPath detects the programming language from a file path based on its extension. Returns an empty string if the language cannot be determined.

func IsBinaryFile

func IsBinaryFile(filePath string) bool

IsBinaryFile checks if a file is binary by reading the first 512 bytes and looking for NULL bytes which indicate binary content

func IsGHCLIAuthenticated

func IsGHCLIAuthenticated() bool

IsGHCLIAuthenticated checks if GitHub CLI (gh) is authenticated

func IsGHCLIInstalled

func IsGHCLIInstalled() bool

IsGHCLIInstalled checks if GitHub CLI (gh) is installed

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowser attempts to open the default browser with the given URL

func SetProcessGroup

func SetProcessGroup(cmd *exec.Cmd)

SetProcessGroup configures the command to run in its own process group. This allows killing the entire process tree on timeout.

func SetProcessGroupKill

func SetProcessGroupKill(cmd *exec.Cmd)

SetProcessGroupKill sets up a cancel function that gracefully terminates the entire process group. It first sends SIGTERM to allow cleanup, waits briefly, then sends SIGKILL if processes are still running.

func ValidateGHCLI

func ValidateGHCLI() error

ValidateGHCLI validates that GitHub CLI is installed and authenticated

func WaitForCondition

func WaitForCondition(timeout, interval time.Duration, condition func() bool) bool

WaitForCondition polls a condition function until it returns true or times out. It returns true if the condition was met, false if it timed out.

Parameters: - timeout: maximum time to wait - interval: how often to check the condition - condition: function that returns true when the desired state is reached

Example usage:

success := WaitForCondition(5*time.Second, 100*time.Millisecond, func() bool {
    _, err := os.Stat(expectedFile)
    return err == nil  // file exists
})

func WaitForFileContent

func WaitForFileContent(timeout, interval time.Duration, filePath string, expectedContent []string) bool

WaitForFileContent waits for a file to exist and contain specific content. Returns true if file contains all expected content, false if timeout reached.

func WaitForFiles

func WaitForFiles(timeout, interval time.Duration, filePaths ...string) bool

WaitForFiles waits for multiple files to exist within the timeout period. Returns true if all files exist, false if timeout reached.

Types

type DomainFilter

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

DomainFilter manages allowed domains loaded from a file

func NewDomainFilter

func NewDomainFilter(filePath string) *DomainFilter

NewDomainFilter creates a new domain filter with the specified file path

func (*DomainFilter) GetAllowedDomains

func (df *DomainFilter) GetAllowedDomains() []string

GetAllowedDomains returns a copy of the current allowed domains and patterns (for testing/debugging)

func (*DomainFilter) IsAllowed

func (df *DomainFilter) IsAllowed(urlStr string) (bool, error)

IsAllowed checks if a domain is allowed, reloading the file if necessary

Jump to

Keyboard shortcuts

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