fileops

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 17 Imported by: 0

README

fileops

fileops is a Go package for various file operations, especially related to configuration files.

Documentation

Overview

fileops is a Go package for various file operations, especially related to configuration files, command line operations, and configuration management.

Index

Constants

This section is empty.

Variables

View Source
var DryRun bool = false

Package wide variable instructing functions whether to actually write to files or run commands.

View Source
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

func Escape(s string) string

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

func Exists(path string) bool

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

func HomeDir(username string) (string, error)

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

func ListFiles(fsys fs.FS, root string) ([]string, error)

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

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

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

func PutFile(destination, content string, filePerm os.FileMode, dirPerm ...os.FileMode) error

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 ReplaceLineInFile(textfile, lineToReplace, replaceWithLine string, n int, matchFullStringNotJustPrefix, matchWithLeadingAndTrailingSpaces bool) error

func ReplaceLineInLines

func ReplaceLineInLines(lines *[]string, lineToReplace string, replaceWithLine string, n int, matchFullStringNotJustPrefix, matchWithLeadingAndTrailingSpaces bool) error

func Run

func Run(command string) error

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

func UserExists(username string) bool

UserExists is a frontend for user.Lookup(username) returning true if user exists, false if not.

func Wait added in v0.0.12

func Wait(seconds int)

Wait waits specified number of seconds or interactively until return/enter is pressed if seconds is less than 0 (e.g -1). 0 seconds returns immediately.

Types

This section is empty.

Jump to

Keyboard shortcuts

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