dirsnap

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package dirsnap simplifies reading and writing snapshots of directories and their contents.

Here is an example of how to use this package during testing:

import (
	"testing"

	"github.com/echocrow/fsnap/dirsnap"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type fsd = dirsnap.Dirs

func TestMyFilesPurge(t *testing.T) {
	dir := t.TempDir()

	// Source dirs snapshot.
	require.NoError(t, fsd{
		"file": nil,
		"dir": fsd{
			"subfile": nil,
			"subdir":  fsd{},
		},
	}.Write(dir))

	// Expected dirs snapshot.
	want := fsd{
		"dir": fsd{
			"subdir": fsd{},
		},
	}

	err := MyFilesPurge(dir)
	assert.NoError(t, err)

	// Actual dirs snapshot.
	got, err := dirsnap.Read(dir, -1)
	require.NoError(t, err)
	assert.Equal(t, want, got)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dirs

type Dirs map[string]Dirs

Dirs represents a nested snapshot of directory contents.

Keys represent the name of a file or folder. Files are represented by a nil value and subfolders by a nested Dirs instance.

func Read

func Read(dir string, n int) (Dirs, error)

Read scans a directory and returs a Dirs tree of its files and folders.

If n < 0, Read will scan all subdirectories.

If n >= 0, Read will descend at most n directory levels below directory dir.

func ReadFS

func ReadFS(fsys fs.FS, dir string, n int) (Dirs, error)

ReadFS scans a fsys directory dir and returs a Dirs tree of its files and folders.

See Read().

func (Dirs) Write

func (d Dirs) Write(dir string) error

Write writes Dirs d into directory dir, creating new files and folders accordingly.

Collisions with already existing files or folders will not result in errors as long as they are of the same type (directory or file respectively).

Jump to

Keyboard shortcuts

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