Documentation
¶
Index ¶
- Constants
- func GetAllFileNamesInDirectory(directoryPath string) (map[string]string, error)
- func GetFilesEndingIn(directoryPath string, extensions ...string) ([]string, error)
- func MediaDownloader(dest, url string, force bool) (string, error)
- func SplitPath(filePath string) []string
- type ErrFileNotOpen
- type FileReader
- func (fh FileReader) ChangePath(path string)
- func (fh FileReader) Close() error
- func (fh FileReader) Create() error
- func (fh FileReader) Delete() error
- func (fh FileReader) Exists() (bool, error)
- func (fh FileReader) GetLocation() string
- func (fh FileReader) GetPermissions() [2]os.FileMode
- func (fr *FileReader) Lines() ([]string, error)
- func (fh FileReader) Open() error
- func (fr *FileReader) Read() (string, error)
- type FileWriter
- func (fw *FileWriter) AppendLine(content string) error
- func (fh FileWriter) ChangePath(path string)
- func (fw *FileWriter) Clear() error
- func (fh FileWriter) Close() error
- func (fh FileWriter) Create() error
- func (fh FileWriter) Delete() error
- func (fw *FileWriter) EmptyLine() error
- func (fh FileWriter) Exists() (bool, error)
- func (fh FileWriter) GetLocation() string
- func (fh FileWriter) GetPermissions() [2]os.FileMode
- func (fh FileWriter) Open() error
- func (fw *FileWriter) Write(p []byte) (n int, err error)
- type JSONEncoder
- type JSONManager
- func (m *JSONManager[T]) ChangePath(path string)
- func (fh JSONManager) Close() error
- func (m *JSONManager[T]) Create() error
- func (fh JSONManager) Delete() error
- func (fh JSONManager) Exists() (bool, error)
- func (fh JSONManager) GetLocation() string
- func (fh JSONManager) GetPermissions() [2]os.FileMode
- func (m *JSONManager[T]) Load() error
- func (fh JSONManager) Open() error
- func (m *JSONManager[T]) Save() error
Constants ¶
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 ¶
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 ¶
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 ¶
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
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
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
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
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
GetPermissions returns the permissions of the file.
Returns:
- [2]os.FileMode: An array of os.FileMode representing the permissions of the directory and file.
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
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
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.