fsinfo

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 9 Imported by: 1

README

fsinfo: File System Information

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).

Features

  • GetFolderInfo: Retrieves information about a folder's contents, subfolders, and files.
  • GetDrives: Obtain details about available drives on the system.
  • GetHomePath: Retrieves the home directory path of the current user.
  • FormatBytes: Converts a size in bytes into a human-readable string representation.
  • ShowHiddenFiles: Sets the visibility of hidden files and folders when retrieving folder information.

Documentation

This package documentation is indexed by pkg.go.dev here.

License

This library is licensed under the terms of the MIT open source license.

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

func FormatBytes(size int64) string

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

func GetHomePath() (string, error)

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

type DriveInfo struct {
	Name string
	Path string
}

func GetDrives

func GetDrives() ([]DriveInfo, error)

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 File

type File struct {
	Name    string // File's name
	Path    string // Absolute file's path
	Size    int64
	ModTime time.Time
}

type Folder

type Folder struct {
	Name    string // Folder's name
	Path    string // Absolute folder's path
	ModTime time.Time
}

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

Jump to

Keyboard shortcuts

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