github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/cmd/git-chglog/fs.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  )
     8  
     9  // FileSystem ...
    10  type FileSystem interface {
    11  	Exists(path string) bool
    12  	MkdirP(path string) error
    13  	Create(name string) (File, error)
    14  	WriteFile(path string, content []byte) error
    15  }
    16  
    17  // File ...
    18  type File interface {
    19  	io.Closer
    20  	io.Reader
    21  	io.ReaderAt
    22  	io.Seeker
    23  	io.Writer
    24  	Stat() (os.FileInfo, error)
    25  }
    26  
    27  var fs = &osFileSystem{}
    28  
    29  type osFileSystem struct{}
    30  
    31  func (*osFileSystem) Exists(path string) bool {
    32  	_, err := os.Stat(path)
    33  	return err == nil
    34  }
    35  
    36  func (*osFileSystem) MkdirP(path string) error {
    37  	if _, err := os.Stat(path); os.IsNotExist(err) {
    38  		return os.MkdirAll(path, os.ModePerm)
    39  	}
    40  	return nil
    41  }
    42  
    43  func (*osFileSystem) Create(name string) (File, error) {
    44  	return os.Create(name)
    45  }
    46  
    47  func (*osFileSystem) WriteFile(path string, content []byte) error {
    48  	return ioutil.WriteFile(path, content, os.ModePerm)
    49  }