file

package
v0.0.0-...-2a3d9f8 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: GPL-3.0 Imports: 15 Imported by: 18

Documentation

Overview

Package file handles simple file related tools for ease of use

Index

Constants

View Source
const (
	// MagicNumber is used to check data validity
	MagicNumber      = 0xdeadbeef
	SmallMagicNumber = uint16(0xdead)
)

Variables

View Source
var DefaultOpenFlags = os.O_RDWR | os.O_CREATE
View Source
var DefaultOpenFlagsAppend = os.O_RDWR | os.O_CREATE | os.O_APPEND

Functions

func AppendToAsciiFile

func AppendToAsciiFile(filename, value string) error

func AsciiFileToLines

func AsciiFileToLines(filename string) []string

func AsciiFileToString

func AsciiFileToString(fileName string) string

func CleanFolder

func CleanFolder(chain, rootFolder string, subFolders []string) error

CleanFolder removes any files that may be partial or incomplete

func Copy

func Copy(destPath, sourcePath string) (int64, error)

func EarliestFileInFolder

func EarliestFileInFolder(path string) (string, error)

EarliestFileInFolder returns the first (alphabetically) file in the folder

func Empty

func Empty(file *os.File) (err error)

Empty is a shortcut function to seek to the beginning of a file and truncate it to 0. It does not call file.Sync though, making all operations in-memory only.

func EstablishFolder

func EstablishFolder(rootPath string) error

EstablishFolder creates folders given a list of folders

func EstablishFolders

func EstablishFolders(rootPath string, folders []string) error

EstablishFolders creates the rootPath and any subfolders

func FileExists

func FileExists(filename string) bool

func FileSize

func FileSize(filename string) int64

func FolderExists

func FolderExists(path string) bool

func GetModTime

func GetModTime(fn string) (time.Time, error)

func GetNewestInDirectory

func GetNewestInDirectory(directory string) (fileInfo os.FileInfo, err error)

func GetPathParts

func GetPathParts(fullPath string) (folder string, filename string, ext string)

func IsFolderEmpty

func IsFolderEmpty(folder string) (bool, error)

func IsLaterThan

func IsLaterThan(fn1, fn2 string) (bool, error)

func IsTestMode

func IsTestMode() bool

func IsValidFolderName

func IsValidFolderName(folderName string) (bool, error)

IsValidFolderName checks if the folder name is valid and does not exist in the current directory.

func LatestFileInFolder

func LatestFileInFolder(path string) (string, error)

LatestFileInFolder returns the latest (alphabetically) file in the folder

func LinesToAsciiFile

func LinesToAsciiFile(filename string, value []string) error

func Lock

func Lock(file *os.File) error

Lock asks the OS to lock a file for the current process

func MustGetLatestFileTime

func MustGetLatestFileTime(folders ...string) time.Time

func NFilesInFolder

func NFilesInFolder(path string) int

func Remove

func Remove(fileName string) bool

func RunWithFileSupport

func RunWithFileSupport(
	mode string,
	run func(cmd *cobra.Command, args []string) error,
	resetOptions func(testMode bool),
) func(cmd *cobra.Command, args []string) error

RunWithFileSupport returns a function to run Cobra command. The command runs in the usual way unless `--file` is specified. If it is specified, this function will parse the file and then run the command in series of independent calls (just like calling `chifra` N times on the command line, but without wasting time and resources for the startup)

func StringToAsciiFile

func StringToAsciiFile(filename, value string) error

func Touch

func Touch(filename string) bool

func Unlock

func Unlock(file *os.File) error

Unlock removes OS-level file lock

func ValidFilename

func ValidFilename(in string, noSpaces bool) string

ValidFilename returns a valid filename from the input string

func WaitOnLock

func WaitOnLock(filePath string, openFlags int) (file *os.File, err error)

WaitOnLock tries to lock a file for maximum `maxSecondsLock`

func WordCount

func WordCount(fileName string, ignoreHeader bool) (int, error)

Types

type BackupFile

type BackupFile struct {
	OrigFn   string
	BackupFn string
}

func MakeBackup

func MakeBackup(tmpPath, origFn string) (BackupFile, error)

MakeBackup creates a backup of the original file and returns a structure that can be used to restore the original file in case of an error.

func (*BackupFile) Clear

func (bf *BackupFile) Clear()

Clear removes the backup file.

func (*BackupFile) Restore

func (bf *BackupFile) Restore()

Restore restores the original file from the backup file.

type CommandFileLine

type CommandFileLine struct {
	LineNumber uint
	Flags      []string
	Args       []string
}

CommandFileLine stores line number for future reference, flag (usually a string that starts with "-" or "--") and args (any other string) information

type CommandsFile

type CommandsFile struct {
	Lines []CommandFileLine
}

CommandsFile is data structure representing the text file with commands (flags and arguments) on each line. Lines trigger independent invocations of given chifra subcommand.

func ParseCommandsFile

func ParseCommandsFile(cmd *cobra.Command, filePath string) (cf CommandsFile, err error)

ParseCommandsFile parses a text file into `CommandsFile` struct. While parsing, the function validates flags present on the current line.

Jump to

Keyboard shortcuts

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