utils

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: BSD-2-Clause Imports: 12 Imported by: 18

Documentation

Index

Constants

View Source
const (
	// FilePerms is the default permissions for new files
	FilePerms = os.FileMode(0644)

	// DirPerms is the default permissions for new directory
	DirPerms = os.ModePerm
)

Variables

This section is empty.

Functions

func DirExists

func DirExists(path string) bool

DirExists checks if the directory at the path exists. It returns false if the directory does not exist or if it is a file.

func Exists

func Exists(path string) bool

Exists checks if a file or directory exists at the specified path.

func ExpandPath

func ExpandPath(path string) (string, error)

ExpandPath replaces the '~' character with the user's home directory and cleans the path using the following rules:

  1. Replace multiple Separator elements with a single one.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path, assuming Separator is '/'.
  5. The returned path ends in a slash only if it represents a root directory.
  6. Any occurrences of slash are replaced by Separator.

func FileExists

func FileExists(path string) bool

FileExists checks if the file at the path exists. It returns false if the file does not exist or if it is a directory.

func GenerateGitVersion

func GenerateGitVersion() string

GenerateGitVersion returns current Git version information.

func GenerateVersionFile

func GenerateVersionFile(version string)

GenerateVersionFile is for version file generation consumed by higher-level repos.

func GetLastModified added in v0.0.4

func GetLastModified(path string) (time.Time, error)

GetLastModified returns the time the file was last modified.

func IsDomainName added in v0.0.4

func IsDomainName(address string) error

IsDomainName returns an error if the domain name is not valid. Ports are allowed; if a port is present, then it is stripped. See https://tools.ietf.org/html/rfc1034#section-3.5 and https://tools.ietf.org/html/rfc1123#section-2. source: https://gist.github.com/chmike/d4126a3247a6d9a70922fc0e8b4f4013

func IsEphemeralPort added in v0.0.4

func IsEphemeralPort(port int) bool

IsEphemeralPort determines if the port is ephemeral. An ephemeral port is any unreserved port, which is any value greater than 1024 (RFC 6056). Note that some ports in this range are still assigned. https://datatracker.ietf.org/doc/html/rfc6056#section-3.2

func IsEphemeralPortString added in v0.0.4

func IsEphemeralPortString(port string) bool

IsEphemeralPortString determines if the string is an ephemeral port.

func IsIP added in v0.0.4

func IsIP(address string) bool

IsIP determines if the given string is a valid IP address. Ports are allowed; if a port is present, then it is stripped.

func IsIPv4 added in v0.0.4

func IsIPv4(address string) bool

IsIPv4 determines if the given string is a valid IPv4 address. Ports are allowed; if a port is present, then it is stripped.

func IsIPv6 added in v0.0.4

func IsIPv6(address string) bool

IsIPv6 determines if the given string is a valid IPv6 address. Ports are allowed; if a port is present, then it is stripped.

func IsPort added in v0.0.4

func IsPort(port int) bool

IsPort determines if the integer is a valid network port.

func IsPortString added in v0.0.4

func IsPortString(port string) bool

IsPortString determines if the string is a valid network port.

func IsPublicAddress added in v0.0.4

func IsPublicAddress(address string) error

IsPublicAddress determines if the given address is a public IP address or domain name. Any strings that are not IP addresses are determined to be valid domain names; the validity of the domain is not checked. Ports are allowed; if a port is present, then it is stripped.

func IsPublicIP added in v0.0.4

func IsPublicIP(address string) error

IsPublicIP determines if the given string is a valid public IP address. The IP address may include a port. If the IP is invalid, then an error is returned specifying the reason. Otherwise, it returns nil. Ports are allowed; if a port is present, then it is stripped.

func IsValidPublicAddress added in v0.0.4

func IsValidPublicAddress(address string) error

IsValidPublicAddress determines if the given address is a public IP address or domain name. It also checks the validity of the domain name. Ports are allowed; if a port is present, then it is stripped.

func MakeDirs

func MakeDirs(path string, perm os.FileMode) error

MakeDirs expands and cleans the path and then creates all the folders in a path that do not exist.

func ParseIP added in v0.0.4

func ParseIP(address string) net.IP

ParseIP returns the IP address as a net.IP object. Expects a valid IPv4 or IPv6 address. Ports are allowed; if a port is present, then it is stripped.

func ReadDir added in v0.0.4

func ReadDir(path string) ([]string, error)

ReadDir reads the named directory, returning all its directory entries sorted by filename.

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile expands and cleans the specified path, reads the file, and returns its contents.

func ReadGoMod

func ReadGoMod() string

ReadGoMod return the go modules file.

func SearchDefaultLocations

func SearchDefaultLocations(
	defaultFileName string, defaultDirectory string) (string, error)

SearchDefaultLocations searches for a file path in a default directory in a number of hard-coded paths, including the user's home folder and /etc/. If the file is found, then its full path is returned. Otherwise, the path is blank and an error is returned.

Note that defaultDirectory MUST be a relative path. By default, when checking the home directory, a "." is prepended to the defaultDirectory.

func WriteFile

func WriteFile(path string, data []byte, filePerm, dirPerm os.FileMode) error

WriteFile creates any directories in the path that do not exist and write the specified data to the file.

func WriteFileDef added in v0.0.4

func WriteFileDef(path string, data []byte) error

WriteFileDef creates any directories in the path that do not exist and write the specified data to the file using the default file and directory permissions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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