Documentation ¶
Overview ¶
Package fsinfo is a lightweight Go package designed to retrieve essential information about folders and drives in both Linux and Windows environments. It can be used to gather insights into the file system and storage devices on the host machine (e.g.: file browser for local webapp).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatBytes ¶ added in v0.1.1
FormatBytes converts a size in bytes into a human-readable string representation. The function takes a size in bytes as input and returns a string that includes the formatted size in appropriate units (bytes, kilobytes, megabytes, gigabytes, terabytes, or petabytes). If the size is zero, the string "0B" will be returned.
Parameters:
- size: The size in bytes to be formatted.
Returns:
A string representing the formatted size with the corresponding unit.
Example:
size := int64(1234567890) formattedSize := FormatBytes(size) fmt.Println("File size:", formattedSize)
Expected Output:
File size: 1.15 GB
func GetHomePath ¶
GetHomePath retrieves the home directory path of the current user. The function returns the home directory path and an error if encountered.
Returns:
- string: The home directory path of the current user.
- error: An error, if any, that occurred during the retrieval of the home directory path.
Example usage:
homePath, err := GetHomePath() if err != nil { fmt.Println("Error:", err) return } fmt.Println("Home Directory Path:", homePath)
func ShowHiddenFiles ¶ added in v0.3.0
func ShowHiddenFiles(show bool)
ShowHiddenFiles sets the visibility of hidden files and folders when retrieving folder information.
Parameters:
- show (bool): A boolean value indicating whether to hide hidden files and folders (false) or not (true). (default = true)
Example usage:
// Hide hidden files and folders when retrieving folder information ShowHiddenFiles(false) // Include hidden files and folders when retrieving folder information ShowHiddenFiles(true)
Note:
Changes made using ShowHiddenFiles apply globally to subsequent calls to GetFolderInfo.
Types ¶
type DriveInfo ¶
func GetDrives ¶
GetDrives retrieves information about the available drives on the system. It returns a slice of DriveInfo structs containing details about the available drives.
Returns:
- []DriveInfo: A slice of DriveInfo structs with information about the available drives.
- error: An error if the information about drives cannot be retrieved.
Example usage:
drives, err := GetDrives() if err != nil { fmt.Println("Error:", err) return } fmt.Println("Available Drives:") for _, drive := range drives { fmt.Println(" Name:", drive.Name) fmt.Println(" Path:", drive.Path) }
type FolderInfo ¶
type FolderInfo struct { Path string `json:"path"` // Absolute folder's path Dir string `json:"parent"` // Directory where the folder is located Folders []Folder `json:"folders"` // Subfolders contained within the folder Files []File `json:"files"` // Files contained within the folder }
FolderInfo contains information about a folder and its contents.
func GetFolderInfo ¶
func GetFolderInfo(path string) (*FolderInfo, error)
GetFolderInfo retrieves information about the contents of a specified folder and its parent directory. It returns a pointer to a FolderInfo struct that contains details about the folder, its subfolders, and the files it contains.
Parameters:
- path (string): An absolute or relative path to the folder for which information is to be retrieved.
Returns:
- *FolderInfo: A pointer to a FolderInfo struct with information about the folder and its contents.
- error: An error, if any, that occurred during the retrieval of the folder information.
Example usage:
// Call the GetFolderInfo function to retrieve information about a folder path := "/path/to/your/target/folder" folderInfo, err := GetFolderInfo(path) if err != nil { fmt.Printf("Error: %v\n", err) return } // Print folder information fmt.Printf("Folder Path: %s\n", folderInfo.Path) fmt.Printf("Parent Directory: %s\n", folderInfo.Dir) fmt.Println("Folders:") for _, folder := range folderInfo.Folders { fmt.Printf("- %s (Last Modified: %s)\n", folder.Name, folder.ModTime) } fmt.Println("Files:") for _, file := range folderInfo.Files { fmt.Printf("- %s (Size: %d bytes, Last Modified: %s)\n", file.Name, file.Size, file.ModTime) }