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
- Variables
- func ContentWithLineNumber(lines []string, offset int) string
- func DetectLanguageFromPath(filePath string) string
- func IsBinaryFile(filePath string) bool
- func IsGHCLIAuthenticated() bool
- func IsGHCLIInstalled() bool
- func OpenBrowser(url string) error
- func SetProcessGroup(cmd *exec.Cmd)
- func SetProcessGroupKill(cmd *exec.Cmd)
- func ValidateGHCLI() error
- func WaitForCondition(timeout, interval time.Duration, condition func() bool) bool
- func WaitForFileContent(timeout, interval time.Duration, filePath string, expectedContent []string) bool
- func WaitForFiles(timeout, interval time.Duration, filePaths ...string) bool
- type DomainFilter
Constants ¶
const ( // DomainRefreshInterval defines how often to reload the domains file DomainRefreshInterval = 30 * time.Second )
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 ¶
var DetachSysProcAttr = syscall.SysProcAttr{ Setpgid: true, Pgid: 0, }
DetachSysProcAttr provides syscall attributes for detaching processes on Unix systems
Functions ¶
func ContentWithLineNumber ¶
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 ¶
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 ¶
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 ¶
OpenBrowser attempts to open the default browser with the given URL
func SetProcessGroup ¶
SetProcessGroup configures the command to run in its own process group. This allows killing the entire process tree on timeout.
func SetProcessGroupKill ¶
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 ¶
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
})
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)