safeio

package
v0.0.0-...-777e37e Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIOResolver = IOResolver{
	Open: func(path string) (ReadAtWriteCloser, error) {
		return os.Open(path)
	},
	OpenDirRecursive: func(path string) ([]string, error) {
		var files []string
		err := filepath.Walk(path, func(p string, info fs.FileInfo, err error) error {
			if err != nil {
				return burrito.WrapErrorf(err, "Failed to walk path %s", p)
			}
			if info.IsDir() {
				return nil
			}
			rel, err := filepath.Rel(path, p)
			if err != nil {
				return burrito.WrapErrorf(err, "Failed to get relative path of %s", p)
			}
			files = append(files, rel)
			return nil
		})
		if err != nil {
			return nil, burrito.WrapErrorf(err, "Failed to walk path %s", path)
		}
		return files, nil
	},
	OpenDir: func(path string) ([]string, error) {
		var files []string
		dir, err := os.ReadDir(path)
		if err != nil {
			return nil, burrito.WrapErrorf(err, "Failed to read dir %s", path)
		}
		for _, file := range dir {
			files = append(files, file.Name())
		}
		return files, nil
	},
	MkdirAll: func(path string) error {
		return os.MkdirAll(path, 0755)
	},
	Remove: func(path string) error {
		return os.RemoveAll(path)
	},
	HttpGet: func(url string) (io.ReadCloser, http.Header, error) {
		open, err := http.Get(url)
		if err != nil {
			return nil, nil, burrito.WrapErrorf(err, "Failed to open url %s", url)
		}
		return open.Body, open.Header, nil
	},
	Create: func(path string) (io.ReadWriteCloser, error) {
		return os.Create(path)
	},
	Stat: func(path string) (fs.FileInfo, error) {
		return os.Stat(path)
	},
	ExecCommand: func(name string, arg ...string) ([]byte, error) {
		return exec.Command(name, arg...).Output()
	},
}

DefaultIOResolver Default IO Resolver, that uses the os package

View Source
var NoIOResolver = IOResolver{
	Open: func(path string) (ReadAtWriteCloser, error) {
		return nil, burrito.WrappedErrorf("IO is disabled")
	},
	OpenDir: func(path string) ([]string, error) {
		return nil, burrito.WrappedErrorf("IO is disabled")
	},
	MkdirAll: func(path string) error {
		return burrito.WrappedErrorf("IO is disabled")
	},
	Remove: func(path string) error {
		return burrito.WrappedErrorf("IO is disabled")
	},
	HttpGet: func(url string) (io.ReadCloser, http.Header, error) {
		return nil, nil, burrito.WrappedErrorf("IO is disabled")
	},
	Create: func(path string) (io.ReadWriteCloser, error) {
		return nil, burrito.WrappedErrorf("IO is disabled")
	},
	Stat: func(path string) (fs.FileInfo, error) {
		return nil, burrito.WrappedErrorf("IO is disabled")
	},
	ExecCommand: func(name string, arg ...string) ([]byte, error) {
		return nil, burrito.WrappedErrorf("IO is disabled")
	},
}

NoIOResolver Resolver that does not allow any IO

Functions

This section is empty.

Types

type FakeFile

type FakeFile struct {
	ReadAtWriteCloser
	// contains filtered or unexported fields
}

func CreateFakeFile

func CreateFakeFile(data []byte) *FakeFile

CreateFakeFile Creates a fake file from a byte slice

func (*FakeFile) Bytes

func (f *FakeFile) Bytes() []byte

func (*FakeFile) Close

func (f *FakeFile) Close() error

func (*FakeFile) Read

func (f *FakeFile) Read(p []byte) (n int, err error)

func (*FakeFile) ReadAt

func (f *FakeFile) ReadAt(p []byte, off int64) (n int, err error)

func (*FakeFile) Seek

func (f *FakeFile) Seek(offset int64, whence int) (int64, error)

func (*FakeFile) Write

func (f *FakeFile) Write(p []byte) (int, error)

type FakeFileInfo

type FakeFileInfo struct {
	os.FileInfo
	// contains filtered or unexported fields
}

func (*FakeFileInfo) IsDir

func (f *FakeFileInfo) IsDir() bool

func (*FakeFileInfo) ModTime

func (f *FakeFileInfo) ModTime() time.Time

func (*FakeFileInfo) Mode

func (f *FakeFileInfo) Mode() os.FileMode

func (*FakeFileInfo) Name

func (f *FakeFileInfo) Name() string

func (*FakeFileInfo) Size

func (f *FakeFileInfo) Size() int64

func (*FakeFileInfo) Sys

func (f *FakeFileInfo) Sys() interface{}

type IOResolver

type IOResolver struct {
	Open             func(path string) (ReadAtWriteCloser, error)
	OpenDir          func(path string) ([]string, error)
	OpenDirRecursive func(path string) ([]string, error)
	MkdirAll         func(path string) error
	Remove           func(path string) error
	HttpGet          func(url string) (io.ReadCloser, http.Header, error)
	Create           func(path string) (io.ReadWriteCloser, error)
	Stat             func(path string) (fs.FileInfo, error)
	ExecCommand      func(name string, arg ...string) ([]byte, error)
}
var Resolver IOResolver = DefaultIOResolver

Resolver Current Resolver used by jsonte to access IO

func CreateFakeFS

func CreateFakeFS(fs map[string]interface{}, withNetwork bool) IOResolver

CreateFakeFS Creates a fake filesystem from a map of byte slices

type ReadAtWriteCloser

type ReadAtWriteCloser interface {
	io.ReadWriteCloser
	io.ReaderAt
}

Jump to

Keyboard shortcuts

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