FileManager

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAll added in v0.3.8

func CreateAll(loc string, perm os.FileMode) (*os.File, error)

CreateAll creates a file and all directories in the path if they do not exist.

Parameters:

  • loc: A string representing the path to the file.
  • perm: An os.FileMode representing the permissions to set on the file.

Returns:

  • *os.File: A pointer to the created file.
  • error: An error if it fails to create the file or directories.

Remember to close the file after using it. For perm, use 0644 for read/write permissions.

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) (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 FileManager added in v0.2.43

type FileManager interface {
	// GetLocation returns the location of the file.
	//
	// Returns:
	//   - string: The location of the file.
	GetLocation() string

	// Exists checks if the file exists at the location.
	//
	// Returns:
	//   - bool: A boolean indicating whether the file exists.
	//   - error: An error if one occurred while checking the file.
	Exists() (bool, error)
}

FileManager represents a file manager that can be used to read and write files.

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) *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) Close added in v0.3.9

func (fr *FileReader) Close() error

Close closes the file that is being read.

Returns:

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

func (*FileReader) Create added in v0.3.9

func (fr *FileReader) Create() error

Create creates a new file at the location.

Returns:

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

func (*FileReader) Exists added in v0.2.43

func (fr *FileReader) Exists() (bool, error)

Exists checks if the file exists at the location.

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 (fr *FileReader) GetLocation() string

GetLocation returns the location of the file.

Returns:

  • string: The location of the 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 (fr *FileReader) Open() error

Open opens the file at the location for reading. It closes the file if it is already open.

Returns:

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

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) *FileWriter

NewFileWriter creates a new FileWriter with the given location.

Parameters:

  • loc: A string representing the location of 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) 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 that is being written to.

Returns:

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

func (*FileWriter) Create added in v0.2.43

func (fw *FileWriter) Create(perm fs.FileMode) error

Create creates a new file at the location; including any necessary directories.

Parameters:

  • perm: An fs.FileMode representing the permissions to set on the file.

Returns:

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

Behaviors:

  • If the file already exists, the function does nothing.
  • Once the file is opened, it is kept open until the FileWriter is closed.

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 the file exists at the location.

Parameters:

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

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

func (fw *FileWriter) Open() error

Open opens the file for writing.

Returns:

  • *os.File: A pointer to the opened file.
  • error: An error if one occurred while opening the file.

Behaviors:

  • The file is opened in append mode and write-only.
  • If the file is already open, the function does nothing.
  • If the file does not exist, the function creates the file.

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() JSONEncoder
}

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) *JSONManager[T]

NewJSONManager creates a new JSONManager.

Parameters:

  • loc: The path to the JSON 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[T]) CreateEmpty added in v0.2.19

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

CreateEmpty creates an empty JSON file with the default values of the data. If the file already exists, it will overwrite the file.

Returns:

  • error: An error if there was an issue creating the empty file.

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

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

Delete deletes the JSON file. No operation is performed if the file does not exist.

Returns:

  • error: An error if there was an issue deleting the file.

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

func (m *JSONManager[T]) Exists() (bool, error)

Exists checks if the JSON file exists.

Returns:

  • bool: True if the file exists, false otherwise.
  • error: An error if there was an issue checking if the file exists.

func (*JSONManager[T]) GetLocation added in v0.2.43

func (m *JSONManager[T]) GetLocation() string

GetLocation returns the path to the JSON file.

Returns:

  • string: The path to the JSON 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[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