FileManager

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToFile

func AppendToFile(filePath string, contents ...string) error

AppendToFile is a function that appends content to a file, each content on a new line.

Panics if it fails to open the file or write to it.

Parameters:

  • filePath: A string representing the path to the file.
  • contents: A variadic parameter of strings representing the content to be appended.

func FileExists added in v0.2.9

func FileExists(filePath string) (bool, error)

FileExists checks if a file exists at the given path.

Parameters:

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

Returns:

  • A boolean indicating whether the file exists.

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 ReadWholeFileLineByLine

func ReadWholeFileLineByLine(path string) ([]string, error)

ReadWholeFileLineByLine reads a file from the provided path line by line and returns a slice of strings, where each string represents a line from the file. If an error occurs while opening the file, the function returns the error and a nil slice.

Parameters:

  • path: The path to the file to be read.

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.

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

NewJSONManager creates a new JSONManager.

Parameters:

  • path: The path to the JSON file.

Returns:

  • *JSONManager[T]: A pointer to a 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]) 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