FileManager

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 11 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 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.

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 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 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 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 FileReader added in v0.2.43

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

FileReader represents a file reader that can be used to read files.

func NewFileReader added in v0.2.43

func NewFileReader(loc string, dirPerm, filePerm os.FileMode) *FileReader

NewFileReader creates a new FileReader with the given location.

Parameters:

  • loc: A string representing the location of the file.

Returns:

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

func (FileReader) ChangePath added in v0.3.11

func (fh FileReader) ChangePath(path string)

ChangePath changes the path of the file.

Parameters:

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

func (FileReader) Close added in v0.3.9

func (fh FileReader) Close() error

Close closes the file if it is open.

Returns:

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

func (FileReader) Create added in v0.3.9

func (fh FileReader) 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 (FileReader) Delete added in v0.3.11

func (fh FileReader) Delete() error

Delete deletes the file at the location.

Returns:

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

func (FileReader) Exists added in v0.2.43

func (fh FileReader) Exists() (bool, error)

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

Returns:

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

func (FileReader) GetLocation added in v0.2.43

func (fh FileReader) GetLocation() string

GetLocation returns the location of the file.

Returns:

  • string: The location of the file.

func (FileReader) GetPermissions added in v0.3.11

func (fh FileReader) 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 (*FileReader) Lines added in v0.2.43

func (fr *FileReader) Lines() ([]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 (FileReader) Open added in v0.3.9

func (fh FileReader) 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 (*FileReader) Read added in v0.2.43

func (fr *FileReader) Read() (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.

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 (fh 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 (fh 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 (fh 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 (fh 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 (fh FileWriter) Exists() (bool, error)

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

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 (fh 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 (fh 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 (fh 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 JSONEncoder added in v0.2.19

type JSONEncoder interface {
	json.Marshaler
	json.Unmarshaler

	// Empty returns the default values of the data.
	//
	// Returns:
	//   - JSONEncoder: The default values of the data.
	Empty() json.Marshaler
}

JSONEncoder is an interface for encoding and decoding JSON data.

type JSONManager added in v0.2.19

type JSONManager[T JSONEncoder] struct {
	// Data is the data to save and load.
	Data T
	// contains filtered or unexported fields
}

JSONManager is a manager for saving and loading data to and from a JSON file.

func NewJSONManager added in v0.2.19

func NewJSONManager[T JSONEncoder](loc string, dirPerm, filePerm os.FileMode) *JSONManager[T]

NewJSONManager creates a new JSONManager.

Parameters:

  • loc: The path to the JSON file.
  • dirPerm: The permissions for the directory.
  • filePerm: The permissions for the file.

Returns:

  • JSONManager[T]: The new JSONManager.

func (*JSONManager[T]) ChangePath added in v0.2.19

func (m *JSONManager[T]) ChangePath(path string)

ChangePath changes the path of the JSON file.

Parameters:

  • path: The new path to the JSON file.

func (JSONManager) Close added in v0.3.11

func (fh JSONManager) Close() error

Close closes the file if it is open.

Returns:

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

func (*JSONManager[T]) Create added in v0.3.11

func (m *JSONManager[T]) Create() error

Create implements the FileManager interface.

func (JSONManager) Delete added in v0.2.19

func (fh JSONManager) Delete() error

Delete deletes the file at the location.

Returns:

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

func (JSONManager) Exists added in v0.2.19

func (fh JSONManager) Exists() (bool, error)

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

Returns:

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

func (JSONManager) GetLocation added in v0.2.43

func (fh JSONManager) GetLocation() string

GetLocation returns the location of the file.

Returns:

  • string: The location of the file.

func (JSONManager) GetPermissions added in v0.3.11

func (fh JSONManager) 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 (*JSONManager[T]) Load added in v0.2.19

func (m *JSONManager[T]) Load() error

Load loads the data from the JSON file.

Returns:

  • error: An error if there was an issue loading the data.

func (JSONManager) Open added in v0.3.11

func (fh JSONManager) 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 (*JSONManager[T]) Save added in v0.2.19

func (m *JSONManager[T]) Save() error

Save saves the data to the JSON file. It will overwrite the file if it already exists.

Returns:

  • error: An error if there was an issue saving the data.

Jump to

Keyboard shortcuts

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