fileIO

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 11 Imported by: 2

README

fileIO

This package provides basic functions for fileIO, but also a FileList object. It can be used to first recursively retrieve all existing filepaths at a specific location and then filter them based on different rules.

Installation

go get github.com/thetillhoff/fileIO@v1.0.0 // Change version if needed

Basic functions

  • CopyFile(src string, dst string) error
  • ReadFile(filePath string) ([]byte, error)
  • ReadFileLineByLine(filePath string) ([]string, error)
  • WriteFile(filePath string, []byte content) error

The cool stuff

  • Watch(inclusionPaths []string, exclusionPaths []string, verbose bool, interval time.Duration, actionEvent func(watcher.Event) error) error Watch the specified paths for file changes and run the specified actionEvent on every change.
  • FileList
    • Generate(fileListPath string, verbose bool) (FileList, error) Recursively traverse the tree below the specified path and return a FileList object with all the found filepaths. Will only add files, not folders.
    • GenerateWithIgnoreLines Recursively traverse the tree below the specified path and return a FileList object with all the found filepaths. Allows to ignore paths during the tree traversal. IgnoreLines work like gitignore.
    • FileList.Filter* Returns the FilelList after applying the filter. Can be a custom filter (see FileList.Filter) or a predefined one (see FileList.Filter*, for example ByFilename)
    • Add or remove prefixes to all paths. If the whole tree is moved to a differend folder, it is not necessary to reread the whole tree.

Development

Releases

All releases follow the semantic versioning schema with a v prefix. Example: v1.2.3

Releases are made via git-tags.

Testing

All internal functions and FileList operations are unit tested. Actual fileIO functions are not yet tested.

TODO
  • Implement test functions for actual fileIO operations.
  • Add fileList.FilterByExtension

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(sourcePath string, destinationPath string) error

Copies the specified sourcePath to the destinationPath Will NOT create the required folders. If the specified sourcePath is a folder, it will create the folder at the destination, but not copy the contents (== non-recursive). This copy process respects permissions of the sourcePaths while creating the new files/folders. The destinationPath must not exist.

func ReadFile

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

Returns the contents of the file at the provided path

func ReadFileLineByLine

func ReadFileLineByLine(filePath string) ([]string, error)

Returns the contents of the file at the provided path

func Watch

func Watch(inclusionPaths []string, exclusionPaths []string, verbose bool, interval time.Duration, actionEvent func(watcher.Event) error) error

Will watch all included paths recusively for changes regularly and run the provided actionEvent

func WriteFile

func WriteFile(filePath string, content []byte) error

Creates a file at filePath and writes the content to it Will create all necessary parent folders

Types

type FileList

type FileList struct {
	Path  string
	Files []string
}

func GenerateFileList

func GenerateFileList(fileListPath string, verbose bool) (FileList, error)

Returns a fileList with the paths of all files within the provided path (recursive traversal)

func GenerateFileListWithIgnoreLines

func GenerateFileListWithIgnoreLines(fileListPath string, ignoreLines []string, verbose bool) (FileList, error)

Returns a fileList with the paths of all files within the provided path (recursive traversal) Will remove the ignored paths before returning the list.

func (FileList) Add

func (fileList FileList) Add(filePathsToAdd ...string) FileList

Adds the provided filePaths to the fileList.Files

func (FileList) AddPrefixToAllPaths

func (fileList FileList) AddPrefixToAllPaths(prefix string) FileList

Adds the specified prefix to every filePath in fileList.Files

func (FileList) Filter

func (fileList FileList) Filter(filterFunc func(string) bool) FileList

Filters the fileList based on the provided function. The provided function gets each filePath as argument and should return whether it should be included or not. Inclusion means it is still contained in the returned fileList Implicitly tested by all the other included Filters

func (FileList) FilterByFilename

func (fileList FileList) FilterByFilename(name string) FileList

Retain those filePaths that have the specified fileName

func (FileList) FilterByFilenameContains

func (fileList FileList) FilterByFilenameContains(nameSubstring string) FileList

Retain only those filePaths, where the fileName contains the specified subString

func (FileList) FilterByFolderPath

func (fileList FileList) FilterByFolderPath(subPath string) FileList

Retains all paths in FileList that match a specific subpath. If the path doesn't exist, the result is empty. Requires a trailing slash. If there is none, it will be automatically added temporarily.

func (FileList) FilterByIgnoreLines

func (fileList FileList) FilterByIgnoreLines(ignoreLines []string) FileList

Removes all filePaths from fileList.Files that match the specified ignoreLines. IgnoreLines are in the format as they are in any .gitignore.

func (FileList) FilterByLevel

func (fileList FileList) FilterByLevel(level int) FileList

Retains all filePaths that have the exact specified level from the root position. Works by counting slashes.

func (FileList) FilterByLevelAtFolderPath

func (fileList FileList) FilterByLevelAtFolderPath(path string, level int) FileList

Retains the filepaths below the provided path, which are the specified amount of path-levels below it Requires a trailing slash. If there is none, it will be automatically added temporarily.

func (FileList) FilterByTreePath

func (fileList FileList) FilterByTreePath(treepath string) FileList

Retains all filePaths that match the specified treepath This means from the root down to the lowest folder, only files in those directories are retained.

func (FileList) Remove

func (fileList FileList) Remove(filePathsToRemove ...string) FileList

Removes the provided filePaths to the fileList.Files

func (FileList) RemovePrefixFromAllPaths

func (fileList FileList) RemovePrefixFromAllPaths(prefix string) FileList

Removes the specified prefix to every filePath in fileList.Files

Jump to

Keyboard shortcuts

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