supertouch package - github.com/balanza/supertouch - Go Packages

supertouch

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 3 Imported by: 0

README

Supertouch

Supertouch is a Go package that allows you to scaffold a file structure from a descriptor. It is similar to executing mkdir and touch commands recursively. This package is primarily designed for testing file access packages.

Installation

go get -u github.com/balanza/supertouch@latest

Usage

import (
  s "github.com/balanza/supertouch"
)

func main() {
  // define the file tree declaratively
  tree := s.Tree(
    s.File("file.txt", "Lorem ipsum"),
    s.Dir("sub_dir",
      s.EmptyFile("other_file.txt"),
    ),
    s.Dir("empty_dir"),
  )

  // creates the file tree in a memory file system
  fs, err := s.Touch(tree)

  // creates the file tree in the provided directory of the memory file system
  fs, err := s.Touch(tree, s.WithBasePath("/my/path"))

  // creates the file tree in the provided directory of the current file system
  fs, err := s.Touch(tree,
    s.WithFileSystem(afero.NewOsFs()),
    s.WithBasePath("/my/path"),
  )
}

Acknowledgement

It turns out there is already a package called Supertouch as well that serves a similar purpose, but in Python. It's an involuntary reference but you know, great minds think alike.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Touch

func Touch(tree []TreeNode, opts ...Option) (afero.Fs, error)

Touch creates a directory structure based on the given tree inside a temporary directory

Types

type Option

type Option func(options *optionDict) error

func WithBasePath

func WithBasePath(path string) Option

WithBasePath sets the base path for the filesystem

func WithFileSystem

func WithFileSystem(fs afero.Fs) Option

WithFileSystem sets a custom filesystem to use

type TreeNode

type TreeNode struct {
	// the name of the file or directory
	Name string
	// optional content of the file, ignored if this is a directory
	Content string
	// if nil, this is a file
	// if not nil, this is a directory
	// and the children are the files and directories
	Children []TreeNode
}

TreeNode represents a file or directory

func Dir

func Dir(name string, children ...TreeNode) TreeNode

func EmptyFile

func EmptyFile(name string) TreeNode

func File

func File(name, content string) TreeNode

func Tree

func Tree(nodes ...TreeNode) []TreeNode

Jump to

Keyboard shortcuts

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