Documentation
¶
Overview ¶
fileops is a Go package for various file operations, especially related to configuration files, command line operations, and configuration management.
Index ¶
- Variables
- func EnsureLineInFile(textfile, line string, before, after *string, ...) error
- func EnsureLineInLines(lines *[]string, line string, before, after *string, ...) error
- func Escape(s string) string
- func Exists(path string) bool
- func HomeDir(username string) (string, error)
- func ListFiles(fsys fs.FS, root string) ([]string, error)
- func MkdirAll(path string, perm ...os.FileMode) error
- func PutFile(destination, content string, filePerm os.FileMode, dirPerm ...os.FileMode) error
- func PutFileFromFS(fsys fs.FS, source string, destination string, filePerm os.FileMode, ...) error
- func PutFileIfNotExists(destination, content string, filePerm os.FileMode, dirPerm ...os.FileMode) error
- func RemoveLineFromFile(textfile, line string, n int, before, after *string, ...) error
- func ReplaceLineInFile(textfile, lineToReplace, replaceWithLine string, n int, ...) error
- func ReplaceLineInLines(lines *[]string, lineToReplace string, replaceWithLine string, n int, ...) error
- func Run(command string) error
- func SetDryRun(state bool)
- func SetExitOnError(state bool)
- func UserExists(username string) bool
- func Wait(seconds int)
Constants ¶
This section is empty.
Variables ¶
var DryRun bool = false
Package wide variable instructing functions whether to actually write to files or run commands.
var ExitOnError bool = false
Package wide variable instructing functions to call os.Exit(1) on error instead of return err. The error message will be printed to os.Stdout before terminating.
Functions ¶
func EnsureLineInFile ¶
func EnsureLineInFile(textfile, line string, before, after *string, matchFullStringNotJustPrefix, matchWithLeadingAndTrailingSpaces bool, filePerm ...os.FileMode) error
EnsureLineInFile ensures line is in textfile, optionally before and/or after string(s) unless before/after is/are nil. Will trim leading and trailing spaces from line read from textfile before matching unless matchWithLeadingAndTrailingSpaces is true. Will treat after and before as prefix unless matchFullStringNotJustPrefix. If optional filePerm is specified, the first item in the slice is used as file mode if textfile does not exist. Returns error on failure.
func EnsureLineInLines ¶
func EnsureLineInLines(lines *[]string, line string, before, after *string, matchFullStringNotJustPrefix, matchWithLeadingAndTrailingSpaces bool) error
EnsureLineInLines ensures line is in lines string pointer slice, optionally before and/or after string(s) unless before/after is/are nil. Will trim leading and trailing spaces from line read from lines before matching unless matchWithLeadingAndTrailingSpaces is true. Will treat after and before as prefix unless matchFullStringNotJustPrefix. Returns error on failure.
func Escape ¶
Escape is an alias for shellescape.Quote(s) used to escape a variable for use in shell command. Returns s escaped.
func Exists ¶ added in v0.0.17
Exists checks if a path (e.g file or directory) exists. Returns true if the file or directory exists, false otherwise.
func HomeDir ¶ added in v0.0.19
HomeDir returns the home directory of the specified user or an error if the user does not exist.
func ListFiles ¶ added in v0.0.11
ListFiles recursively lists all files in the given fs.FS starting from the root directory. If fsys is an embed.FS, be sure to use root `.` and not `/`. Returns a string slice of paths to non-directory items (files) or error is something failed.
func MkdirAll ¶ added in v0.0.6
MkdirAll creates path directory including all parent directories (similar to mkdir -p). If a sub directory does not exist, it will be created with mode 0755 by default or the value of the first item in the optional perm slice. Returns error on failure.
func PutFile ¶
PutFile writes content into local file destination with mode filePerm. If destination is a path with directories they will be created using optional dirPerm or mode 0755 by default. Returns error if something failed.
func PutFileFromFS ¶ added in v0.0.3
func PutFileFromFS(fsys fs.FS, source string, destination string, filePerm os.FileMode, dirPerm ...os.FileMode) error
PutFileFromFS copies a file or recursively copies a directory from an fs.FS interface to a target path on the local filesystem. Returns error in case of failure.
func PutFileIfNotExists ¶ added in v0.0.17
func PutFileIfNotExists(destination, content string, filePerm os.FileMode, dirPerm ...os.FileMode) error
PutFileIfNotExists does not overwrite destination file if it already exists, otherwise it does and returns what PutFile does.
func RemoveLineFromFile ¶ added in v0.0.14
func RemoveLineFromFile(textfile, line string, n int, before, after *string, matchFullStringNotJustPrefix, matchWithLeadingAndTrailingSpaces bool) error
RemoveLineFromFile removes line n number of times (or all of them if n is -1) from textfile. If before and/or after are not nil, the line before and/or after line to be removed must contain the after/before string respectively. If both before and after are nil, line is removed from anywhere in the file.
func ReplaceLineInFile ¶
func ReplaceLineInLines ¶
func SetDryRun ¶ added in v0.0.4
func SetDryRun(state bool)
SetDryRun can be used to toggle package-wide dry-run-mode on or off. If state is true, no files or content will be persisted/written to disk and no commands will run. Instead, verbose output will be printed to stderr.
func SetExitOnError ¶ added in v0.0.6
func SetExitOnError(state bool)
SetExitOnError can be used to set whether functions should exit on error instead of return err. See ExitOnError variable.
func UserExists ¶ added in v0.0.19
UserExists is a frontend for user.Lookup(username) returning true if user exists, false if not.
Types ¶
This section is empty.