FileManager

package
v0.3.17 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DP_OwnerOnly is the permission where only the owner can read, write, and execute.
	DP_OwnerOnly fs.FileMode = 0700

	// DP_OwnerRestrictOthers is the permission where the owner can read, write, and execute, and others can read and execute.
	DP_OwnerRestrictOthers fs.FileMode = 0755

	// DP_All is the permission where everyone can read, write, and execute.
	DP_All fs.FileMode = 0777

	// FP_OwnerOnly is the permission where only the owner can read and write.
	FP_OwnerOnly fs.FileMode = 0600

	// FP_OwnerRestrictOthers is the permission where the owner can read and write, and others can read.
	FP_OwnerRestrictOthers fs.FileMode = 0644

	// FP_All is the permission where everyone can read and write.
	FP_All fs.FileMode = 0666
)

Variables

This section is empty.

Functions

func CheckPath added in v0.3.14

func CheckPath(loc string, isDir bool) (bool, error)

CheckPath checks if the path is a directory or file as expected.

Parameters:

  • loc: A string representing the path to check.
  • isDir: A boolean indicating whether the path should be a directory.

Returns:

  • bool: True if the path is as expected, false otherwise.
  • error: An error if the path is not as expected or if an error occurred while checking the path.

func CountDepth added in v0.3.16

func CountDepth(loc string) int

CountDepth counts the depth of a file path.

Parameters:

  • loc: A string representing the path to the file.

Returns:

  • int: The depth of the file path.

func Create added in v0.3.14

func Create(loc string, dirPerm, filePerm os.FileMode) (*os.File, error)

Create creates the file at the location.

Returns:

  • error: An error if one occurred while creating the file.

Behaviors:

  • If the file already exists, it closes the previous file and creates a new one.
  • Once the file is opened, it is kept open until the FileManager is closed.

func FileExists added in v0.2.9

func FileExists(loc string) (bool, error)

FileExists checks if a file exists at the specified location.

Parameters:

  • loc: The location of the file.

Returns:

  • bool: True if the file exists, false otherwise.

func GetAllFileNamesInDirectory

func GetAllFileNamesInDirectory(directoryPath string) (map[string]string, error)

GetAllFileNamesInDirectory is a function that retrieves all file names in a given directory and their extensions.

Parameters:

  • directoryPath: A string representing the path to the directory.

Returns:

  • map[string]string: A map where the keys are the file paths and the values are the file extensions.
  • error: An error if it fails to read the directory or any of its files.

Behaviors:

  • The function does not search subdirectories, nor returns directories.
  • The file paths are relative to the directory path.
  • The keys contain the full path to the file (including the extension).

func GetFilesEndingIn

func GetFilesEndingIn(directoryPath string, extensions ...string) ([]string, error)

GetFilesEndingIn is a function that retrieves all files in a given directory that have a specified extension. This function does not search subdirectories.

Parameters:

  • directoryPath: A string representing the path to the directory.
  • extensions: A variadic parameter of strings representing the file extensions to match.

Returns:

  • []string: A slice of strings representing the paths to the matching files.
  • error: An error if it fails to read the directory.

func GroupFilesByExtension added in v0.3.15

func GroupFilesByExtension(dir string) error

GroupFilesByExtension is a function that groups files in a directory by their extension and moves them to a folder with the same name as the extension.

Parameters:

  • dir: A string representing the path to the directory.

Returns:

  • error: An error if it fails to read the directory, create folders, or move files.

func Lines added in v0.3.14

func Lines(loc string, create bool) ([]string, error)

Lines reads the file line by line and returns a slice of strings, where each string represents a line from the file.

Returns:

  • []string: A slice of strings where each string is a line from the file.
  • error: An error if one occurred while opening or scanning the file.

Behaviors:

  • The function reads the file line by line. Each line does not include the newline character.
  • If an error occurs, the function returns the error and the lines read up to that point.

func MediaDownloader

func MediaDownloader(dest, url string, force bool) (string, error)

MediaDownloader downloads a file from the given URL and saves it to the specified destination.

The name of the file is derived from the URL and it does not download the file if the name already exists in the destination.

Parameters:

  • dest: A string representing the path to the directory where the file will be saved.
  • url: A string representing the URL of the file to download.

Returns:

  • string: The path to the downloaded file.
  • error: An error if the download fails.

Example:

file_path, err := MediaDownloader("/path/to/destination", "http://example.com/file.mp3")
if err != nil {
    log.Fatal(err)
}
fmt.Println(file_path) // Output: /path/to/destination/file.mp3

func NewDEWalkerIter added in v0.3.16

func NewDEWalkerIter(loc string) (ui.Iterater[Item], error)

NewDirEntryIterator creates a new directory entry iterator.

This iterator reads the directories and all of its subdirectories in a depth-first manner without using recursion.

Parameters:

  • loc: A string representing the path to the directory.

Returns:

  • *Iter1: The new directory entry iterator.
  • error: An error if it fails to read the directory.

func PerLine added in v0.3.14

func PerLine[T any](loc string, create bool, f func(string) (T, error)) ([]T, error)

PerLine reads the file line by line and applies a function to each line.

Parameters:

  • loc: The location of the file.
  • create: A boolean indicating whether to create the file if it does not exist.
  • f: A function that processes a line of text and returns a value.

Returns:

  • []T: A slice of values returned by the function for each line.
  • error: An error if one occurred while opening or scanning the file.

Behaviors:

  • The function reads the file line by line and applies the function f to each line.
  • If an error occurs, the function returns the error and the values processed up to that point.

func Read added in v0.3.14

func Read(loc string, create bool) (string, error)

ReadFile reads the content of a file from the provided path and returns the content as a string.

Parameters:

  • filePath: A string representing the path to the file to be read.

Returns:

  • string: A string representing the content of the file.
  • error: An error of type *os.PathError if the file could not be opened or read.

Behaviors:

  • The function reads the entire file into memory.
  • If an error occurs, the function returns the error and an empty string.

func SplitPath added in v0.2.3

func SplitPath(filePath string) []string

SplitPath splits a file path into its components, where each component is a part of the file path.

Parameters:

  • filePath: A string representing the path to the file.

Returns:

  • []string: A slice of strings representing the parts of the file path.

Types

type DEWalkerIter added in v0.3.15

type DEWalkerIter struct {
	// contains filtered or unexported fields
}

DEWalkerIter is an iterator that reads directories and all of its subdirectories in a depth-first manner without using recursion.

func (*DEWalkerIter) Consume added in v0.3.15

func (iter *DEWalkerIter) Consume() (*ItemList, error)

Consume implements the Iterators.Iterater interface.

It ignores entries that would cause an error when reading them. However, if all entries are invalid, only the furthest error is returned.

func (*DEWalkerIter) Restart added in v0.3.15

func (iter *DEWalkerIter) Restart()

Restart implements the Iterators.Iterater interface.

func (*DEWalkerIter) Size added in v0.3.15

func (iter *DEWalkerIter) Size() int

Size implements the Iterators.Iterater interface.

type ErrFileNotOpen added in v0.2.43

type ErrFileNotOpen struct{}

ErrFileNotOpen is an error type for when a file was not opened.

func NewErrFileNotOpen added in v0.2.43

func NewErrFileNotOpen() *ErrFileNotOpen

NewErrFileNotOpen creates a new ErrFileNotOpen error.

Returns:

  • *ErrFileNotOpen: A pointer to the newly created error.

func (*ErrFileNotOpen) Error added in v0.2.43

func (e *ErrFileNotOpen) Error() string

Error returns the error message: "file was not opened".

type ErrPathNot added in v0.3.14

type ErrPathNot struct {
	// Path is the path that was not as expected.
	Path string

	// Expected is the expected value of the path.
	Expected string
}

ErrPathNot is an error type for when a path is not as expected.

func NewErrPathNot added in v0.3.14

func NewErrPathNot(path, expected string) *ErrPathNot

NewErrPathNot creates a new ErrPathNot error.

Parameters:

  • path: A string representing the path that was not as expected.
  • expected: A string representing the expected value of the path.

Returns:

  • *ErrPathNot: A pointer to the newly created error.

func (*ErrPathNot) Error added in v0.3.14

func (e *ErrPathNot) Error() string

Error returns the error message: "path <path> is not <expected>".

Returns:

  • string: The error message.

type FileWriter added in v0.2.43

type FileWriter struct {
	// contains filtered or unexported fields
}

FileWriter represents a file writer that can be used to write to files.

func NewFileWriter added in v0.2.43

func NewFileWriter(loc string, dirPerm, filePerm os.FileMode) *FileWriter

NewFileWriter creates a new FileWriter with the given location.

Parameters:

  • loc: A string representing the location of the file.
  • dirPerm: An os.FileMode representing the permissions to set on the directories.
  • filePerm: An os.FileMode representing the permissions to set on the file.

Returns:

  • *FileWriter: A pointer to the newly created FileWriter.

func (*FileWriter) AppendLine added in v0.2.43

func (fw *FileWriter) AppendLine(content string) error

AppendLine appends a line of content to the file.

Parameters:

  • content: A string representing the content to append to the file.

Returns:

  • error: An error if one occurred while writing to the file.

Errors:

  • *ErrFileNotOpen: If the file is not open.
  • error: If any other case.

func (*FileWriter) ChangePath added in v0.3.11

func (fw *FileWriter) ChangePath(path string)

ChangePath changes the path of the file.

Parameters:

  • path: A string representing the new path of the file.

func (*FileWriter) Clear added in v0.2.43

func (fw *FileWriter) Clear() error

Clear clears the contents of the file.

Returns:

  • error: An error if one occurred while truncating the file.

Errors:

  • *ErrFileNotOpen: If the file is not open.
  • *os.PathError: If any other case.

func (*FileWriter) Close added in v0.2.43

func (fw *FileWriter) Close() error

Close closes the file if it is open.

Returns:

  • error: An error if one occurred while closing the file.

func (*FileWriter) Create added in v0.2.43

func (fw *FileWriter) Create() error

Create creates the file at the location.

Returns:

  • error: An error if one occurred while creating the file.

Behaviors:

  • If the file already exists, it closes the previous file and creates a new one.
  • Once the file is opened, it is kept open until the FileManager is closed.

func (*FileWriter) Delete added in v0.3.11

func (fw *FileWriter) Delete() error

Delete deletes the file at the location.

Returns:

  • error: An error if one occurred while deleting the file.

func (*FileWriter) EmptyLine added in v0.3.9

func (fw *FileWriter) EmptyLine() error

EmptyLine appends an empty line to the file.

Returns:

  • error: An error if one occurred while writing to the file.

Errors:

  • *ErrFileNotOpen: If the file is not open.
  • error: If any other case.

func (*FileWriter) Exists added in v0.2.43

func (fw *FileWriter) Exists() (bool, error)

Exists checks if a file exists at the location of the FileWriter.

Returns:

  • bool: A boolean indicating whether the file exists.
  • error: An error if one occurred while checking the file.

func (*FileWriter) GetLocation added in v0.2.43

func (fw *FileWriter) GetLocation() string

GetLocation returns the location of the file.

Returns:

  • string: The location of the file.

func (*FileWriter) GetPermissions added in v0.3.11

func (fw *FileWriter) GetPermissions() [2]os.FileMode

GetPermissions returns the permissions of the file.

Returns:

  • [2]os.FileMode: An array of os.FileMode representing the permissions of the directory and file.

func (*FileWriter) Open added in v0.2.43

func (fw *FileWriter) Open() error

Open opens the file at the location.

Returns:

  • error: An error if one occurred while opening the file.

Behaviors:

  • If the file is already open, the function closes the file and opens it again.

func (*FileWriter) Write added in v0.3.9

func (fw *FileWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface for the FileWriter.

type Item added in v0.3.15

type Item struct {

	// fs.DirEntry is the directory entry.
	fs.DirEntry
	// contains filtered or unexported fields
}

Item is a struct that represents a directory entry with its location.

func NewItem added in v0.3.15

func NewItem(loc string, de fs.DirEntry) Item

NewItem creates a new item.

Parameters:

  • loc: A string representing the path to the directory.
  • de: The directory entry.

Returns:

  • *Item: The new item.

func (*Item) FullPath added in v0.3.15

func (item *Item) FullPath() string

FullPath returns the full path to the directory entry including the directory entry name.

Returns:

  • string: The full path to the directory entry.

func (*Item) Path added in v0.3.15

func (item *Item) Path() string

Path returns the path to the directory. This excludes the directory entry name.

Returns:

  • string: The path to the directory.

type ItemList added in v0.3.16

type ItemList struct {
	// contains filtered or unexported fields
}

ItemList is a struct that represents a list of items.

func (*ItemList) Iterator added in v0.3.16

func (il *ItemList) Iterator() ui.Iterater[Item]

Iterator implements the Iterators.Iterable interface.

Jump to

Keyboard shortcuts

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