fs

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package fs implements filesystem utilities as well as a Paths Object which holds references, to important directories like the platform-specific logging for configuration directories. These paths are either built for the generic 'gopskit' application name (to ensure our files don't crowd global OS directories) or for custom names, which is configurable with Opt arguments for NewPaths.

Index

Constants

This section is empty.

Variables

View Source
var (
	KnownGitDir     = ".git"
	KnownGitMarkers = []string{"config", "HEAD", "branches", "objects", "refs"}
)
View Source
var (
	// OSReleaseRegex is to be used somewhere...
	OSReleaseRegex = `^(?<variable>[A-Za-z\d_-]+)=(?<startQuote>['"]?)(?<value>.[^"']*)(?<endQuote>['"]?)$`

	// KnownConfigTypes are the only file formats we u
	KnownConfigTypes = []string{"yaml", "json"}

	// DefaultConfigTypes is an alias for KnownConfigTypes
	DefaultConfigTypes = KnownConfigTypes
	DefaultAppName     = "gopskit"
	DefaultConfigPaths = []string{
		fmt.Sprintf("/etc/%s", DefaultAppName),
		".",
	}
)

Functions

func CheckIfExists

func CheckIfExists(path string) bool

CheckIfExists checks if the specified path exists and returns the boolean result

func CreateFile

func CreateFile(path string) (*os.File, error)

func ParseGitRoot

func ParseGitRoot(path string) (string, error)

ParseGitRoot traverses the filesystem upwards from the given path and checks each new directory whether it contains a KnownGitDir (.git subdirectory). If the subdirectory is found we check if it contains the KnownGitMarkers. If so the path is returned with a nil error.

Otherwise, the first errors that occurs is returned alongside an empty string.

func Read

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

func Remove

func Remove(path string) error

Remove removes all files and directories at the specified path. Before running delete operations the function checks if the path actually exists, if it does not, it exits silently. If an error occurs during the delete operation, that isn't a fs.PathError, we panic.

func TempDir

func TempDir(pattern string) (string, error)

TempDir creates a temporary directory within the default temporary directory configured for your OS. On Unix it's likely '/tmp'.

func TempFile

func TempFile(pattern string) (*os.File, error)

TempFile

func Write

func Write(path string, content []byte) error

func WriteFile

func WriteFile(file *os.File, content []byte) error

Types

type Opt

type Opt func(p *PlatformPaths)

Opt is a configuration option for the Paths object

func WithAppName

func WithAppName(app string) Opt

WithAppName configures the PlatformPaths object with a custom application name

func WithConfigPath

func WithConfigPath(path ...string) Opt

WithConfigPath configures the PlatformPaths object with extra custom configuration paths By default we'll use the KnownConfigPaths

func WithConfigType

func WithConfigType(filetype string) Opt

WithConfigType configures the PlatformPaths object with a custom configuration type By default we assume support JSON and YAML files but this function has the ability to lock that to a single value. Only "yaml" or "json" are supported.

type PlatformPaths

type PlatformPaths struct {
	// AppName the name of the application we're building paths for is this field
	// isn't set during initialization we will set it to DefaultAppName
	AppName string

	// Home is the platform-specific home directory for the current user
	Home string

	// Config is the platform-specific path for configuration files and as such
	// serves as a (one of) source location for waltr configuration files
	Config string

	// Data is the platform-specific path for generic data to be written to and
	// is mainly used a default output directory since it's user-writable
	Data string

	// Cache is the platform-specific path for cache files, which will be read
	// from or written to quite frequently, as such it is also the location for
	// waltr's persistent key-value database
	Cache string

	// Log is the platform-specific path for log files. This path will be used
	// during initialization of the application's logger
	Log string

	// ConfigTypes is a slice of supported file extensions for the app-specific
	// configuration file. If unset during initialization we will set the value
	// to DefaultConfigTypes
	ConfigTypes []string

	// ConfigPaths is a slice of possible source paths for the app-specific
	// configuration file. Each of these paths will be searched for a matching file
	ConfigPaths []string

	// Exists denotes which of the files within ConfigPaths exists by mapping the
	// slices values to map keys and checking for existence of the files
	Exists map[string]bool
	// contains filtered or unexported fields
}

PlatformPaths represents the platform-specific paths for Config, Data, Cache or Log files or directories. If AppName isn't set during initialization we will re-use the generic 'gopskit' name to ensure we're writing to a subdirectory of the respective paths. This is largely in line with native Platform behaviors.

On Linux we'll use the XDG directories, whereas on Windows and macOS we use system variables like %APPDATA% for Windows or $HOME for macOS.

func DefaultPaths

func DefaultPaths() (*PlatformPaths, error)

DefaultPaths returns a fully initialized PlatformPaths object, using the DefaultAppName as a name instead of an actual application name.

Another way of obtaining a Platform object is by using New with the corresponding options.

func Paths

func Paths(opts ...Opt) (*PlatformPaths, error)

Paths creates a fully initialized PlatformPaths object with (optional) custom Options to configure the final Paths object. If no Options are applied the output will be akin to Default, except for the Config... fields which are now set.

Jump to

Keyboard shortcuts

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