filesystem

package module
v0.0.0-...-e4cf78d Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2018 License: MIT Imports: 5 Imported by: 0

README

File system abstraction

Travis Coverage Status Go Report Card GoDoc license

Work in progress!

  1. Install
  2. Filesystem manipulations
    1. List directory contents
    2. Deep inside directories
    3. Check path
    4. Path info
    5. Create directories
    6. Rename
    7. Remove
  3. Seekers
  4. License

Install

$ go get -u github.com/alexeyco/filesystem

Filesystem manipulations

root, err := filesystem.Root("/path/to/root/directory")
if err != nil {
    log.Fatalln(err)
}
List directory contents

Directories:

err := root.Each().Dir(func(dir *filesystem.Dir) {
    fmt.Println("Dir:  ", dir.Name())
})

Files:

err = root.Each().File(func(file *filesystem.File) {
    fmt.Println("File: ", file.Name())
})

Anything:

err = root.Each().Entry(func(entry filesystem.Entry) {
    fmt.Println("Entry: ", entry.Name())
})
Deep inside directories
err = root.Read(filesystem.Deep()).Each().Entry(func(entry filesystem.Entry) {
    if entry.IsDir() {
        fmt.Println("Dir:  ", entry.Name())
    } else {
        fmt.Println("File: ", entry.Name())
    }
})
Check path

Check path exists:

exist := root.Exist("path/to/file")

Check path is a directory:

exist := root.IsDir("path/to/directory")

Check path is a file:

exist := root.IsFile("path/to/file")
Path info

Get directory:

dir, err := root.Dir("path/to/dir")

Get file:

file, err := root.Dir("path/to/file")
Create directories

Create directory:

err = root.Mkdir("path/to/directory")
Move/rename

Rename files and directories:

err = root.Move("path/to/source", "path/to/dest")
Remove

Remove files and directories:

err = root.Move("path/to/source", "path/to/dest")

License

MIT License

Copyright (c) 2018 Alexey Popov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package filesystem - filesystem abstraction

Permission is hereby granted, free of charge, to any person obtaining a copyAll of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copyAll, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See more examples on https://github.com/alexeyco/filesystem

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

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

Dir directory type

func (*Dir) IsDir

func (d *Dir) IsDir() bool

IsDir always true

func (*Dir) IsFile

func (d *Dir) IsFile() bool

IsFile always false

func (*Dir) Name

func (d *Dir) Name() string

Name returns directory name

func (*Dir) Stat

func (d *Dir) Stat() os.FileInfo

Stat returns directory info

type EachDirHandler

type EachDirHandler func(dir *Dir)

EachDirHandler directories iteration handler

type EachEntryHandler

type EachEntryHandler func(entry Entry)

EachEntryHandler files iteration handler

type EachFileHandler

type EachFileHandler func(file *File)

EachFileHandler file iteration handler

type Entry

type Entry interface {
	IsDir() bool
	IsFile() bool
	Name() string
	Stat() os.FileInfo
}

Entry basic filesystem object

type ErrAlreadyExist

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

ErrAlreadyExist file already exist

func (*ErrAlreadyExist) Error

func (e *ErrAlreadyExist) Error() string

Error returns error string

type ErrNotDir

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

ErrNotDir path is not directory

func (*ErrNotDir) Error

func (e *ErrNotDir) Error() string

Error returns error string

type ErrNotExist

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

ErrNotExist path is not exist

func (*ErrNotExist) Error

func (e *ErrNotExist) Error() string

Error returns error string

type ErrNotFile

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

ErrNotFile path is not file

func (*ErrNotFile) Error

func (e *ErrNotFile) Error() string

Error returns error string

type ErrNotInRoot

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

ErrNotInRoot path is outside root directory

func (*ErrNotInRoot) Error

func (e *ErrNotInRoot) Error() string

Error returns error string

type File

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

File file type

func (*File) Ext

func (f *File) Ext() string

Ext returns file extension

func (*File) IsDir

func (*File) IsDir() bool

IsDir always false

func (*File) IsFile

func (*File) IsFile() bool

IsFile always true

func (*File) Name

func (f *File) Name() string

Name returns file name

func (*File) Stat

func (f *File) Stat() os.FileInfo

Stat returns file info

type Fs

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

Fs root directory object

func Root

func Root(dir string) (*Fs, error)

Root returns Fs object by path

func (*Fs) Dir

func (fs *Fs) Dir(path string) (*Dir, error)

Dir returns directory info by root-relative path

func (*Fs) Each

func (fs *Fs) Each() *Iterator

Each creates an iterator and returns it

func (*Fs) Exist

func (fs *Fs) Exist(path string) bool

Exist checks if root-relative path exist

func (*Fs) File

func (fs *Fs) File(path string) (*File, error)

File returns file info by root-relative path

func (*Fs) IsDir

func (fs *Fs) IsDir(path string) bool

IsDir checks if root-relative path is a directory

func (*Fs) IsFile

func (fs *Fs) IsFile(path string) bool

IsFile checks if root-relative path is a file

func (*Fs) Mkdir

func (fs *Fs) Mkdir(path string) error

Mkdir creates a new directory in root directory

func (*Fs) Move

func (fs *Fs) Move(source, dest string) error

Move moves anything inside root

func (*Fs) Read

func (fs *Fs) Read(seeker Seeker) Seeker

Read seekers factory

func (*Fs) Remove

func (fs *Fs) Remove(path string) error

Remove removes root-relative directory or file by path

func (*Fs) Root

func (fs *Fs) Root() string

Root returns absolute path to current root directory

type Iterator

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

Iterator files iterator

func (*Iterator) Dir

func (i *Iterator) Dir(handler EachDirHandler) error

Dir iterates through directories

func (*Iterator) Entry

func (i *Iterator) Entry(handler EachEntryHandler) error

Entry iterates through files and directories

func (*Iterator) File

func (i *Iterator) File(handler EachFileHandler) error

File iterates through files

type Seeker

type Seeker interface {
	SetRoot(string)
	Root() string
	Each() *Iterator
	Exist(path string) bool
	IsDir(path string) bool
	IsFile(path string) bool
	Dir(path string) (*Dir, error)
	File(path string) (*File, error)
	Mkdir(path string) error
	Move(source, dest string) error
	Remove(path string) error
	// contains filtered or unexported methods
}

Seeker file seeker interface

func Deep

func Deep() Seeker

Deep returns deep seeker

func In

func In(dir string) Seeker

In returns SeekerIn

type SeekerDeep

type SeekerDeep struct {
	*SeekerIn
	// contains filtered or unexported fields
}

SeekerDeep deep file seeker

func (*SeekerDeep) Each

func (s *SeekerDeep) Each() *Iterator

Each returns iterator with current seeker

func (*SeekerDeep) Root

func (s *SeekerDeep) Root() string

Root returns seeker root

func (*SeekerDeep) SetRoot

func (s *SeekerDeep) SetRoot(root string)

SetRoot sets seeker root

type SeekerIn

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

SeekerIn basic seeker

func (*SeekerIn) Dir

func (s *SeekerIn) Dir(path string) (*Dir, error)

Dir returns directory by path

func (*SeekerIn) Each

func (s *SeekerIn) Each() *Iterator

Each returns iterator

func (*SeekerIn) Exist

func (s *SeekerIn) Exist(path string) bool

Exist checks if file exist

func (*SeekerIn) File

func (s *SeekerIn) File(path string) (*File, error)

File returns file by path

func (*SeekerIn) IsDir

func (s *SeekerIn) IsDir(path string) bool

IsDir checks if path is directory

func (*SeekerIn) IsFile

func (s *SeekerIn) IsFile(path string) bool

IsFile checks if path is file

func (*SeekerIn) Mkdir

func (s *SeekerIn) Mkdir(path string) error

Mkdir creates directory

func (*SeekerIn) Move

func (s *SeekerIn) Move(source, dest string) error

Move moves files and directories

func (*SeekerIn) Remove

func (s *SeekerIn) Remove(path string) error

Remove removes files and directories

func (*SeekerIn) Root

func (s *SeekerIn) Root() string

Root returns seeker root

func (*SeekerIn) SetRoot

func (s *SeekerIn) SetRoot(root string)

SetRoot sets seeker root (relative to Fs root)

Jump to

Keyboard shortcuts

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