fs

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFromPath

func CopyFromPath(
	ctx context.Context,
	wg *sync.WaitGroup,
	errCh chan<- error,
	cfg interfaces.ModuleConfig,
	from, to string,
	existsMode types.FileExistsAction,
	convert bool,
	filterRules []string,
)

CopyFromPath performs the process of copying files from a source directory to a target directory.

The function checks if the context is valid, and if so, it invokes the `walk` function to traverse the source directory and perform the copy operation. It uses a `sync.WaitGroup` to wait for completion, and reports any errors encountered through the provided error channel.

Parameters:

  • ctx (context.Context): The context to control the execution and cancellation of the operation.
  • wg (*sync.WaitGroup): The wait group to synchronize the completion of the operation.
  • errCh (chan<- error): A channel for reporting errors encountered during the operation.
  • cfg (internal.ModuleConfig): Module instance
  • from (string): The source directory to copy from.
  • to (string): The destination directory to copy to.
  • existsMode (FileExistsAction): The action to take if the file already exists in the destination.
  • convert (bool): A flag to indicate whether to convert the files during the copy process.
  • filterRules ([]string): Filter rules for selective copying of files based on patterns.

If any error is encountered during the execution, it is reported through the `errCh` channel.

func IsEmptyDir

func IsEmptyDir(path string) bool

IsEmptyDir checks whether the specified directory exists and is empty.

Parameters:

  • path: The path to the directory to check.

Returns:

  • true if the directory exists and is empty.
  • false if the directory does not exist, is not accessible, or contains at least one entry.

Notes:

  • If the directory cannot be opened (e.g., due to permission issues), the function returns false.
  • If an error occurs while reading directory entries, it is assumed to be empty (e.g., when the directory does not exist).
  • Logs an error if closing the directory fails.

func MkDir

func MkDir(path string) (string, error)

MkDir creates a directory at the specified path, including any necessary parent directories. It first ensures the provided path is an absolute and clean path, and checks whether the path is valid. If the path does not exist, it will be created with permissions 0750. If the directory already exists, it does nothing.

Parameters:

  • path (string): The path where the directory should be created. The function will resolve this to an absolute path and ensure it is valid before attempting to create the directory.

Returns:

  • string: The absolute path of the created directory.
  • error: If an error occurs during path resolution or directory creation, an error is returned.

func RemoveEmptyDirs

func RemoveEmptyDirs(root string) (bool, error)

RemoveEmptyDirs recursively removes empty directories within the specified root directory.

Parameters:

  • root: The path of the directory to start the Cleanup.

Returns:

  • A boolean indicating whether the directory itself is empty after processing.
  • An error if any issue occurs while reading or removing directories.

Behavior:

  • Traverses all subdirectories recursively.
  • If a subdirectory becomes empty after processing its contents, it gets removed.
  • If any error occurs (e.g., permission issues), the error is returned.
  • The function ensures that only empty directories are removed, leaving files untouched.

Example:

_, err := RemoveEmptyDirs("/path/to/root")
if err != nil {
    log.Fatalf("Failed to remove empty directories: %v", err)
}

func ZipIt

func ZipIt(dirPath, archivePath string) error

ZipIt creates a ZIP archive from the specified directory, including all its files and subdirectories. It recursively walks through the directory, adding files and directories to the ZIP archive, preserving the relative paths of the files.

Parameters:

  • dirPath (string): The path to the directory to be archived. It is walked recursively, and all files and subdirectories are included in the ZIP archive.
  • archivePath (string): The path where the ZIP archive should be created. The function will resolve this to an absolute path and ensure the archive is saved at that location.

Returns:

  • error: If an error occurs during any part of the zipping process (such as file opening, writing, or walking), an error is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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