utils

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package utils provides utility functions for INIQ

Package utils provides utility functions for INIQ

Index

Constants

View Source
const (
	// Basic colors
	ColorReset   = "\033[0m"
	ColorGray    = "\033[90m"
	ColorGreen   = "\033[32m"
	ColorYellow  = "\033[33m"
	ColorBlue    = "\033[34m"
	ColorRed     = "\033[31m"
	ColorCyan    = "\033[36m"
	ColorMagenta = "\033[35m"

	// Bright colors
	ColorBrightGreen  = "\033[92m"
	ColorBrightYellow = "\033[93m"
	ColorBrightBlue   = "\033[94m"
	ColorBrightRed    = "\033[91m"
	ColorBrightCyan   = "\033[96m"

	// Text styles
	ColorBold      = "\033[1m"
	ColorUnderline = "\033[4m"

	// Combined styles
	ColorHeaderBlue = "\033[1;34m" // Bold Blue
	ColorHeaderCyan = "\033[1;36m" // Bold Cyan
	ColorSuccess    = "\033[32m"   // Green
	ColorWarning    = "\033[33m"   // Yellow
	ColorError      = "\033[31m"   // Red
	ColorInfo       = "\033[34m"   // Blue
	ColorDebug      = "\033[36m"   // Cyan
	ColorListItem   = "\033[90m"   // Gray
)

Colors for terminal output

Variables

This section is empty.

Functions

func BackupFile

func BackupFile(filePath string, backupEnabled bool) (string, error)

BackupFile creates a backup of a file with a timestamp If the file doesn't exist, it returns nil without creating a backup Returns the path to the backup file if successful The backup file will preserve the original file's permissions and ownership

func BoldText

func BoldText(text string) string

BoldText returns the text in bold

func ColorText

func ColorText(text string, colorCode string) string

ColorText wraps text with the specified color code and resets the color afterward

func CopyFilePermissions added in v0.3.1

func CopyFilePermissions(srcPath, dstPath string) error

CopyFilePermissions copies permissions and ownership from source to destination file

func EnsureSystemFilePermissions added in v0.3.1

func EnsureSystemFilePermissions(filePath string, mode os.FileMode) error

EnsureSystemFilePermissions ensures a system file has the correct ownership and permissions This is typically used for files like sudoers, SSH config, etc.

func EnsureUserFilePermissions added in v0.3.1

func EnsureUserFilePermissions(filePath, username string, mode os.FileMode) error

EnsureUserFilePermissions ensures a user file has the correct ownership and permissions

func ErrorText

func ErrorText(text string) string

ErrorText returns the text in error color (red)

func FixFileOwnership added in v0.3.1

func FixFileOwnership(filePath string, targetUID, targetGID int) error

FixFileOwnership sets the correct ownership for a file If running as root, sets ownership directly If not running as root but the current user owns the file, sets ownership directly Otherwise, uses sudo to set ownership

func FixFilePermissions added in v0.3.1

func FixFilePermissions(filePath string, mode os.FileMode) error

FixFilePermissions sets the correct permissions for a file If running as root, sets permissions directly If not running as root, uses sudo to set permissions

func InfoText

func InfoText(text string) string

InfoText returns the text in info color (blue)

func IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal checks if the given file is a terminal

func ParseBoolValue added in v0.2.2

func ParseBoolValue(value string) (bool, error)

ParseBoolValue parses various boolean value formats Supports: yes|enable|true|1|y|t|on for true values

no|disable|false|0|n|f|off for false values

func PromptThreeState added in v0.2.2

func PromptThreeState(question string, featureName string, currentState bool) string

PromptThreeState asks the user a three-state question: enable/disable/keep-current Returns "enable", "disable", or "keep"

func PromptWithDefault

func PromptWithDefault(question string, defaultValue string) string

PromptWithDefault asks the user for input with a default value and returns the user's input or the default if the user just presses Enter.

func PromptYesNo

func PromptYesNo(question string, defaultValue bool) bool

PromptYesNo asks the user a yes/no question and returns true for yes, false for no. The defaultValue parameter determines the default answer if the user just presses Enter. Shows gray placeholder text for the default value that gets replaced when user types.

func PromptYesNoWithFeedback added in v0.2.2

func PromptYesNoWithFeedback(question string, defaultValue bool, featureName string, currentState bool) bool

PromptYesNoWithFeedback asks the user a yes/no question and provides feedback when using defaults featureName and currentState are used to provide meaningful feedback about the current setting

func SuccessText

func SuccessText(text string) string

SuccessText returns the text in success color (green)

func WarningText

func WarningText(text string) string

WarningText returns the text in warning color (yellow)

Types

type FilePermissions added in v0.3.1

type FilePermissions struct {
	UID  int
	GID  int
	Mode os.FileMode
}

FilePermissions represents file ownership and permission information

func GetFilePermissions added in v0.3.1

func GetFilePermissions(filePath string) (*FilePermissions, error)

GetFilePermissions retrieves the current permissions and ownership of a file

type StateToggleAction added in v0.2.2

type StateToggleAction string

StateToggleAction represents the action to take for a state toggle

const (
	StateToggleKeep    StateToggleAction = "keep"
	StateToggleEnable  StateToggleAction = "enable"
	StateToggleDisable StateToggleAction = "disable"
)

type StateToggleConfig added in v0.2.2

type StateToggleConfig struct {
	// Feature name (e.g., "SSH root login", "SSH password authentication")
	FeatureName string
	// Current state (true = enabled, false = disabled)
	CurrentState bool
	// Custom prompt (optional)
	AllowPrompt string // Default: "Allow {FeatureName}?"
}

StateToggleConfig holds configuration for a state toggle prompt

type StateToggleResult added in v0.2.2

type StateToggleResult struct {
	Action    StateToggleAction
	HasChange bool
}

StateToggleResult holds the result of a state toggle interaction

func PromptStateToggle added in v0.2.2

func PromptStateToggle(config StateToggleConfig) StateToggleResult

PromptStateToggle provides a user-friendly state toggle interaction using "Allow" syntax Returns the action to take and whether there's a change from current state

Jump to

Keyboard shortcuts

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