osutil

package
v0.22.10 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PermissionOwnerReadWriteOthersRead   os.FileMode = 0644
	PermissionOnlyOwnerReadWrite         os.FileMode = 0600
	PermissionOnlyOwnerReadWriteTraverse os.FileMode = 0700 // For directories
	PermissionDirectoryOthersRead        os.FileMode = 0755 // For shared directories
	PermissionOnlyOwnerReadWriteExecute  os.FileMode = 0700 // For files
	PermissionCheckEveryoneRead          os.FileMode = 0004 // Everyone can read, used for checking permissions
	PermissionCheckEveryoneWrite         os.FileMode = 0002 // Everyone can write, used for checking permissions
	PermissionCheckEveryoneExecute       os.FileMode = 0001 // Everyone can execute, used for checking permissions

	// Bitmask combined with umask for creating directories. This represents the maximum possible default permissions
	// for a folder; the final default permissions are determined by masking (subtracting) the umask bits from these
	// bits.
	DefaultFolderBitmask os.FileMode = 0777

	// Bitmask combined with umask for creating files. This represents the maximum possible default permissions for a
	// file; the final default permissions are determined by masking (subtracting) the umask bits from these bits.
	DefaultFileBitmask os.FileMode = 0666
	// Default umask for creating files and directories. The umask represents permissions that should be denied
	// when using the default file and directory permissions. The umask is subtracted from the maximum possible
	// permissions, so a umask of 022 would result in default folder permissions of 0755 (0777 - 022) and default file
	// permissions of 0644 (0666 - 022).
	DefaultUmaskBitmask os.FileMode = 022
)
View Source
const (
	// RFC3339 with millisecond precision, fixed width.
	// This is a template, not a literal example. In particular, "Z" stands for timezone designator,
	// which is either "Z" or "+hh:mm" or "-hh:mm". See below for the regex that defines the actual format.
	RFC3339MiliTimestampFormat = "2006-01-02T15:04:05.000Z07:00"

	RFC33339YearRegex                     = `\d{4}`
	RFC3339MonthRegex                     = `(0[1-9]|1[0-2])`
	RFC3339DayRegex                       = `(0[1-9]|[12][0-9]|3[01])`
	RFC3339HourRegex                      = `([01][0-9]|2[0-3])`
	RFC3339TimestampMinutesOrSecondsRegex = `([0-5][0-9])`
	RFC3339MillisecondRegex               = `\d{3}`
)
View Source
const (
	MaxCopyFileSize = 50 * 1024 * 1024 // 50MB

)

Variables

View Source
var (
	RFC3339TimezoneRegex = fmt.Sprintf(
		`(Z|([+-]?%s:%s))`,
		RFC3339HourRegex,
		RFC3339TimestampMinutesOrSecondsRegex,
	)

	// Matches a timestamp in RFC3339 format with millisecond precision.
	// See https://www.rfc-editor.org/rfc/rfc3339#section-5.6 for more details.
	RFC3339MiliTimestampRegex = fmt.Sprintf(
		`%s-%s-%sT%s:%s:%s.%s%s`,
		RFC33339YearRegex,
		RFC3339MonthRegex,
		RFC3339DayRegex,
		RFC3339HourRegex,
		RFC3339TimestampMinutesOrSecondsRegex,
		RFC3339TimestampMinutesOrSecondsRegex,
		RFC3339MillisecondRegex,
		RFC3339TimezoneRegex,
	)
)

Functions

func CRLF

func CRLF() []byte

func EnvVarDurationValWithDefault

func EnvVarDurationValWithDefault(varName string, defaultVal time.Duration) time.Duration

func EnvVarIntVal

func EnvVarIntVal(varName string) (int, bool)

func EnvVarIntValWithDefault

func EnvVarIntValWithDefault(varName string, defaultVal int) int

func EnvVarStringWithDefault

func EnvVarStringWithDefault(varName string, defaultVal string) string

func EnvVarSwitchEnabled

func EnvVarSwitchEnabled(varName string) bool

Returns true if the environment variable "switch" is enabled. The environment variable is considered enabled if it is set to one of the "truthy" values: "1", "true", "on", or "yes".

func FindRootFor

func FindRootFor(target PathFindTarget, tailElem ...string) (string, error)

Starting from current working directory, tries to walk up the directory hierarchy and find the root that ends with given path to file or directory. Returns the root path found, or error.

func FormatDuration

func FormatDuration(duration time.Duration) string

Formats a duration into a human readable string. If this proves not enough, consider https://github.com/hako/durafmt

func HasOnlyValidFilenameChars added in v0.22.2

func HasOnlyValidFilenameChars(name string) bool

HasOnlyValidFilenameChars checks whether name is a simple, OS-agnostic valid filename according to the following rules: - must not be empty - must not contain control characters (ASCII < 32) - must not contain any of: < > : " / \\ | ? * - must not end with a space or a dot This is a deliberately simple check. It does NOT guarantee the filename is accepted by the OS or filesystem. Should NOT be used for security-sensitive validation.

func IsAdmin

func IsAdmin() (bool, error)

func IsWindows

func IsWindows() bool

func LF

func LF() []byte

func LineSep

func LineSep() []byte

func MicroEqual

func MicroEqual(a, b metav1.MicroTime) bool

Checks whether two metav1.MicroTime values can be considered equal. Due to serialization/deserialization, the values may have different represenation, and thus Equal() may return false even if they represent essentially the same time.

func ThisExecutablePath

func ThisExecutablePath() (string, error)

Returns the full path to the current executable.

func WithNewline

func WithNewline(b []byte) []byte

func Within

func Within(a, b time.Time, max time.Duration) bool

Ensures two given timestamps are within a given duration of each other.

Types

type PathFindTarget

type PathFindTarget int
const (
	FileTarget PathFindTarget = 1
	DirTarget  PathFindTarget = 2
)

Jump to

Keyboard shortcuts

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