fsadapter

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: BSD-3-Clause Imports: 12 Imported by: 1

README

fsadapter - File System adapter

Go Reference

Purpose

"fsadapter" provides write-only adapters to various destinations. There are currently 2 adapters:

  • Directory
  • ZIP

It is meant to be a drop-in replacement for os.* functions for Slackdump.

Details

Each adapter exposes the following methods:

  • Create(string) (io.WriteCloser, error)
  • WriteFile(name string, data []byte, perm os.FileMode) error
  • Close() error

Testing

If you need to use a mock of "fsadapter.FSCloser", it's in the mocks/mock_fsadapter package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIllegalDir = errors.New("illegal file path reference outside of working directory")

ErrIllegalDir is returned, if the file path reference is outside of the working directory.

Functions

This section is empty.

Types

type Directory

type Directory struct {
	// contains filtered or unexported fields
}

func NewDirectory

func NewDirectory(dir string) Directory

NewDirectory returns a new Directory filesystem adapter for a given directory.

func (Directory) Close

func (fs Directory) Close() error

Close is a noop for Directory.

func (Directory) Create

func (fs Directory) Create(fpath string) (io.WriteCloser, error)

Create creates a new file in the directory.

func (Directory) String

func (d Directory) String() string

func (Directory) WriteFile

func (fs Directory) WriteFile(name string, data []byte, perm os.FileMode) error

WriteFile writes data to a file named by name. If the file does not exist, WriteFile creates it with permissions perm (before umask); otherwise WriteFile truncates it before writing.

type FS

type FS interface {
	Create(string) (io.WriteCloser, error)
	WriteFile(name string, data []byte, perm os.FileMode) error
}

FS is interface for operating on the files of the underlying filesystem.

type FSCloser

type FSCloser interface {
	FS
	io.Closer
}

FSCloser is a FS that can be closed.

func New

func New(location string) (FSCloser, error)

New returns appropriate filesystem based on the name of the location. Logic is simple:

  • if location has a known extension, the appropriate adapter is returned.
  • else: it's a directory.

Currently supported extensions: ".zip" (case insensitive)

type ZIP

type ZIP struct {
	// contains filtered or unexported fields
}

ZIP is a filesystem adapter for zip files.

func NewZIP

func NewZIP(zw *zip.Writer) *ZIP

NewZIP returns a new ZIP filesystem adapter for a given ZipWriter.

func NewZipFile

func NewZipFile(filename string) (*ZIP, error)

NewZipFile returns a new ZIP filesystem adapter for a given filename.

func (*ZIP) Close

func (z *ZIP) Close() error

Close closes the underlying zip writer and the file handle. It is only necessary if ZIP was initialised using NewZipFile

func (*ZIP) Create

func (z *ZIP) Create(filename string) (io.WriteCloser, error)

Create creates a new file in the zip archive.

func (*ZIP) String

func (z *ZIP) String() string

func (*ZIP) WriteFile

func (z *ZIP) WriteFile(filename string, data []byte, _ os.FileMode) error

WriteFile writes the given data to the given filename in the zip archive.

Directories

Path Synopsis
mocks
mock_fsadapter
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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