aferoutils

package
v2.2.6 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 4 Imported by: 0

README

goutils/v2/aferoutils

The aferoutils package is a collection of utility functions designed to simplify common aferoutils tasks.


Table of contents


Functions

Tree(afero.Fs, string, io.Writer)
Tree(afero.Fs, string, io.Writer) error

Tree displays the directory tree structure starting from the specified directory path in a format similar to the tree command.

Parameters:

fs: The afero.Fs representing the file system to use. dirPath: The path of the directory to display the tree structure for. prefix: The prefix string to use for each line of the tree structure. indent: The indent string to use for each level of the tree structure. out: The io.Writer to write the tree structure output to.

Returns:

error: An error if any issue occurs while trying to display the tree structure.


Installation

To use the goutils/v2/aferoutils package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/aferoutils

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/aferoutils"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/aferoutils:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tree

func Tree(fs afero.Fs, dirPath, prefix, indent string, out io.Writer) error

Tree displays the directory tree structure starting from the specified directory path in a format similar to the `tree` command.

**Parameters:**

fs: The afero.Fs representing the file system to use. dirPath: The path of the directory to display the tree structure for. prefix: The prefix string to use for each line of the tree structure. indent: The indent string to use for each level of the tree structure. out: The io.Writer to write the tree structure output to.

**Returns:**

error: An error if any issue occurs while trying to display the tree structure.

Example
package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/l50/goutils/v2/file/aferoutils"
	"github.com/spf13/afero"
)

func main() {
	// Create an in-memory file system
	fs := afero.NewMemMapFs()

	// Create directories
	_ = fs.MkdirAll("/file/aferoutils", 0755)
	_ = fs.MkdirAll("/file/fileutils", 0755)

	// Create files
	_ = afero.WriteFile(fs, "/file/aferoutils/README.md", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/aferoutils/aferoutils.go", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/aferoutils/aferoutils_examples_test.go", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/aferoutils/aferoutils_test.go", []byte{}, 0644)

	_ = afero.WriteFile(fs, "/file/fileutils/README.md", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/fileutils/fileutils.go", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/fileutils/fileutils_examples_test.go", []byte{}, 0644)
	_ = afero.WriteFile(fs, "/file/fileutils/fileutils_test.go", []byte{}, 0644)

	// Set up the output buffer
	var buf bytes.Buffer

	// Display the directory tree structure
	err := aferoutils.Tree(fs, "/file", "", "    ", &buf)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Print the output
	fmt.Println(strings.TrimSpace(buf.String()))
}
Output:

file
├── aferoutils
│   ├── README.md
│   ├── aferoutils.go
│   ├── aferoutils_examples_test.go
│   └── aferoutils_test.go
└── fileutils
    ├── README.md
    ├── fileutils.go
    ├── fileutils_examples_test.go
    └── fileutils_test.go

Types

This section is empty.

Jump to

Keyboard shortcuts

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