github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/utils/file.go (about)

     1  package utils
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  )
     7  
     8  // FileStater is a helper interface to allow testing.
     9  type FileStater interface {
    10  	Stat(name string) (os.FileInfo, error)
    11  }
    12  
    13  // OsFS implements Stat() for local disk.
    14  type OsFS struct{}
    15  
    16  // Stat proxies to os.Stat.
    17  func (OsFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
    18  
    19  // ReadFile reads the content of given filename and returns it as a string
    20  func ReadFile(filename string) (string, error) {
    21  	if _, err := os.Stat(filename); err != nil {
    22  		return "", err
    23  	}
    24  	bytes, err := ioutil.ReadFile(filename)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	return string(bytes), nil
    29  }