utils

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Byte = 1
	KB   = 1024 * Byte
	MB   = 1024 * KB
	GB   = 1024 * MB
	TB   = 1024 * GB
)

Size constants in bytes

View Source
const (
	Microsecond = time.Microsecond
	Millisecond = time.Millisecond
	Second      = time.Second
	Minute      = 60 * Second
	Hour        = 60 * Minute
	Day         = 24 * Hour
)

Time constants

Variables

This section is empty.

Functions

func BytesToGB

func BytesToGB(bytes uint64) uint64

BytesToGB converts bytes to gigabytes

func BytesToKB

func BytesToKB(bytes uint64) uint64

BytesToKB converts bytes to kilobytes

func BytesToMB

func BytesToMB(bytes uint64) uint64

BytesToMB converts bytes to megabytes

func CheckPGDATAPermissions

func CheckPGDATAPermissions(pgdataPath string) error

CheckPGDATAPermissions performs comprehensive permission checks on PGDATA

func DetectBinDir

func DetectBinDir() (string, error)

DetectBinDir detects PostgreSQL binary directory

func DetectDataDir

func DetectDataDir() (string, error)

DetectDataDir detects PostgreSQL data directory

func FileEnv added in v1.0.4

func FileEnv(key, defaultVal string) (string, error)

FileEnv retrieves an environment variable value or reads from a file if *_FILE variant exists. This implements Docker secrets pattern where sensitive values can be read from files. Returns error if both env var and file variant are set (exclusive use required).

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration to a human-readable string

func FormatDurationPostgreSQL

func FormatDurationPostgreSQL(d time.Duration) string

FormatDurationPostgreSQL formats a duration as PostgreSQL expects it

func FormatSize

func FormatSize(bytes uint64) string

FormatSize formats a size in bytes to a human-readable string

func FormatSizePostgreSQL

func FormatSizePostgreSQL(bytes uint64) string

FormatSizePostgreSQL formats a size in bytes as PostgreSQL expects it

func FormatSizePostgreSQLMB

func FormatSizePostgreSQLMB(bytes uint64) string

FormatSizePostgreSQLMB formats a size in bytes as PostgreSQL MB units only This ensures consistent MB units for all PostgreSQL memory settings

func GBToBytes

func GBToBytes(gb uint64) uint64

GBToBytes converts gigabytes to bytes

func GenerateSchemaDefaultsFromDescribeConfig

func GenerateSchemaDefaultsFromDescribeConfig(params []schemas.Param) map[string]string

GenerateSchemaDefaultsFromDescribeConfig creates schema defaults from postgres --describe-config output This function bridges the gap between PostgreSQL's native parameter metadata and our configuration schema

func GetCurrentUserInfo

func GetCurrentUserInfo() (uid int, gid int, username string, err error)

GetCurrentUserInfo returns information about the current user

func GetPostgreSQLVersion

func GetPostgreSQLVersion(dataDir string) (string, error)

GetPostgreSQLVersion reads the PostgreSQL version from PG_VERSION file

func GetSchemaDefaults

func GetSchemaDefaults() map[string]string

GetSchemaDefaults extracts default values from JSON schema metadata This would be used by configuration loaders to set proper defaults

func GetSchemaDefaultsFromPostgres

func GetSchemaDefaultsFromPostgres(version string) (map[string]string, error)

GetSchemaDefaultsFromPostgres generates schema defaults using embedded PostgreSQL's describe-config This function provides the most accurate and up-to-date defaults based on the actual PostgreSQL version

func IsRunningAsPostgres

func IsRunningAsPostgres() bool

IsRunningAsPostgres checks if the current process is running as postgres user (UID 999)

func IsRunningAsRoot

func IsRunningAsRoot() bool

IsRunningAsRoot checks if the current process is running as root

func KBToBytes

func KBToBytes(kb uint64) uint64

KBToBytes converts kilobytes to bytes

func MBToBytes

func MBToBytes(mb uint64) uint64

MBToBytes converts megabytes to bytes

func MergeSchemaDefaults

func MergeSchemaDefaults(manualDefaults, autoDefaults map[string]string) map[string]string

MergeSchemaDefaults merges manually curated defaults with auto-generated ones Manual defaults take precedence over auto-generated ones

func MustFileEnv added in v1.0.4

func MustFileEnv(key, defaultVal string) string

MustFileEnv is like FileEnv but panics on error

func ParseDuration

func ParseDuration(durationStr string) (time.Duration, error)

ParseDuration parses a duration string and returns the duration

func ParseSize

func ParseSize(sizeStr string) (uint64, error)

ParseSize parses a size string and returns the size in bytes

func ResolveBoolDefault

func ResolveBoolDefault(value string, fallback bool) bool

ResolveBoolDefault resolves a boolean default value with environment variable support

func ResolveDefault

func ResolveDefault(value string) string

ResolveDefault resolves environment variable syntax in default values Supports patterns like: - ${VAR} - returns environment variable VAR or empty string - ${VAR:-default} - returns environment variable VAR or "default" - plain text - returns as-is

func ResolveFloat64Default

func ResolveFloat64Default(value string, fallback float64) float64

ResolveFloat64Default resolves a float64 default value with environment variable support

func ResolveIntDefault

func ResolveIntDefault(value string, fallback int) int

ResolveIntDefault resolves an integer default value with environment variable support

func ValidateWithBinary

func ValidateWithBinary(validator BinaryValidator, configPath string) error

ValidateWithBinary validates a configuration file using a binary tool

Types

type BinaryValidator

type BinaryValidator interface {
	GetBinaryPath() string
	GetValidationArgs(configPath string) []string
	ParseValidationError(output string) (*ValidationError, error)
}

BinaryValidator defines the interface for validating configurations with binaries

type PermissionCheckResult

type PermissionCheckResult struct {
	Path        string
	Exists      bool
	Readable    bool
	Writable    bool
	OwnerUID    int
	OwnerGID    int
	CurrentUID  int
	CurrentGID  int
	ErrorMsg    string
	FixCommands []string
}

PermissionCheckResult represents the result of a permission check

func CheckDirectoryPermissions

func CheckDirectoryPermissions(path string) (*PermissionCheckResult, error)

CheckDirectoryPermissions checks if a directory has correct permissions for the current user

type PermissionError

type PermissionError struct {
	Path        string
	Message     string
	CurrentUID  int
	OwnerUID    int
	FixCommands []string
}

PermissionError represents a permission-related error with fix suggestions

func (*PermissionError) Error

func (e *PermissionError) Error() string

type PostgreSQLDirs

type PostgreSQLDirs struct {
	BinDir  string
	DataDir string
}

PostgreSQLDirs holds detected PostgreSQL directories

func DetectPostgreSQLDirs

func DetectPostgreSQLDirs() (*PostgreSQLDirs, error)

DetectPostgreSQLDirs detects both binary and data directories

type SensitiveString

type SensitiveString string

SensitiveString is a string type that hides sensitive information in logs and JSON output

func NewSensitiveString

func NewSensitiveString(value string) SensitiveString

NewSensitiveString creates a new SensitiveString from a regular string

func SensitiveStringFromEnv

func SensitiveStringFromEnv(key string) SensitiveString

SensitiveStringFromEnv creates a SensitiveString from an environment variable Returns empty SensitiveString if the environment variable doesn't exist

func (SensitiveString) Equals

func (s SensitiveString) Equals(other SensitiveString) bool

Equals compares two sensitive strings for equality (constant-time comparison)

func (SensitiveString) Format

func (s SensitiveString) Format(f fmt.State, verb rune)

Format implements fmt.Formatter to ensure sensitive values are redacted in all format verbs

func (SensitiveString) GoString

func (s SensitiveString) GoString() string

GoString implements fmt.GoStringer to prevent accidental logging in %#v format

func (SensitiveString) IsEmpty

func (s SensitiveString) IsEmpty() bool

IsEmpty returns true if the sensitive string is empty

func (SensitiveString) MarshalJSON

func (s SensitiveString) MarshalJSON() ([]byte, error)

MarshalJSON redacts the value in JSON output

func (SensitiveString) MarshalText

func (s SensitiveString) MarshalText() ([]byte, error)

MarshalText redacts the value in text marshaling (used by many serializers)

func (SensitiveString) String

func (s SensitiveString) String() string

String implements fmt.Stringer to prevent accidental logging of sensitive values

func (SensitiveString) Value

func (s SensitiveString) Value() string

Value returns the actual string value - use with caution and only when necessary

type ValidationError

type ValidationError struct {
	Line    int
	Column  int
	Message string
	Raw     string
}

ValidationError represents a configuration validation error

func ParseErrorWithRegex

func ParseErrorWithRegex(output string, patterns map[string]*regexp.Regexp) (*ValidationError, error)

ParseErrorWithRegex is a helper function to parse validation errors using regex patterns

Jump to

Keyboard shortcuts

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