github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/filesys/fs.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package filesys
    16  
    17  import (
    18  	"encoding/json"
    19  	"errors"
    20  	"io"
    21  	"os"
    22  	"time"
    23  )
    24  
    25  var ErrIsDir = errors.New("operation not valid on a directory")
    26  var ErrIsFile = errors.New("operation not valid on a file")
    27  var ErrDirNotExist = errors.New("directory does not exist")
    28  
    29  // ReadableFS is an interface providing read access to objs in a filesystem
    30  type ReadableFS interface {
    31  
    32  	// OpenForRead opens a file for reading
    33  	OpenForRead(fp string) (io.ReadCloser, error)
    34  
    35  	// ReadFile reads the entire contents of a file
    36  	ReadFile(fp string) ([]byte, error)
    37  
    38  	// Exists will tell you if a file or directory with a given path already exists, and if it does is it a directory
    39  	Exists(path string) (exists bool, isDir bool)
    40  
    41  	// converts a path to an absolute path.  If it's already an absolute path the input path will be returned unaltered
    42  	Abs(path string) (string, error)
    43  
    44  	// LastModified gets the last modified timestamp for a file or directory at a given path
    45  	LastModified(path string) (t time.Time, exists bool)
    46  }
    47  
    48  // WritableFS is an interface providing write access to objs in a filesystem
    49  type WritableFS interface {
    50  	// OpenForWrite opens a file for writing.  The file will be created if it does not exist, and if it does exist
    51  	// it will be overwritten.
    52  	OpenForWrite(fp string, perm os.FileMode) (io.WriteCloser, error)
    53  
    54  	// WriteFile writes the entire data buffer to a given file.  The file will be created if it does not exist,
    55  	// and if it does exist it will be overwritten.
    56  	WriteFile(fp string, data []byte) error
    57  
    58  	// MkDirs creates a folder and all the parent folders that are necessary to create it.
    59  	MkDirs(path string) error
    60  
    61  	// DeleteFile will delete a file at the given path
    62  	DeleteFile(path string) error
    63  
    64  	// Delete will delete an empty directory, or a file.  If trying delete a directory that is not empty you can set force to
    65  	// true in order to delete the dir and all of it's contents
    66  	Delete(path string, force bool) error
    67  
    68  	// MoveFile will move a file from the srcPath in the filesystem to the destPath
    69  	MoveFile(srcPath, destPath string) error
    70  }
    71  
    72  // FSIterCB specifies the signature of the function that will be called for every item found while iterating.
    73  type FSIterCB func(path string, size int64, isDir bool) (stop bool)
    74  
    75  // WalkableFS is an interface for walking the files and subdirectories of a directory
    76  type WalkableFS interface {
    77  
    78  	// Iter iterates over the files and subdirectories within a given directory (Optionally recursively).  There
    79  	// are no guarantees about the ordering of results.  Two calls to iterate the same directory may yield
    80  	// differently ordered results.
    81  	Iter(directory string, recursive bool, cb FSIterCB) error
    82  }
    83  
    84  // ReadWriteFS is an interface whose implementors will provide read, and write implementations but may not allow
    85  // for files to be listed.
    86  type ReadWriteFS interface {
    87  	ReadableFS
    88  	WritableFS
    89  }
    90  
    91  // Filesys is an interface whose implementors will provide read, write, and list mechanisms
    92  type Filesys interface {
    93  	ReadableFS
    94  	WritableFS
    95  	WalkableFS
    96  }
    97  
    98  func UnmarshalJSONFile(fs ReadableFS, path string, dest interface{}) error {
    99  	data, err := fs.ReadFile(path)
   100  
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	return json.Unmarshal(data, dest)
   106  }