github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/pkg/file/file.go (about)

     1  package file
     2  
     3  import (
     4  	"os"
     5  )
     6  
     7  // Exists return true when a file exists, false otherwise.
     8  func Exists(path string) bool {
     9  	_, err := os.Stat(path)
    10  	return !os.IsNotExist(err)
    11  }
    12  
    13  // Copy file to destination
    14  // TODO: Use io.Copy instead of completely reading, then writing the file to use Go optimizations.
    15  func Copy(src, dst string) error {
    16  	input, err := os.ReadFile(src)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	err = os.WriteFile(dst, input, 0644)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	return nil
    27  }
    28  
    29  // IsSymlink checks if the file is symbolic link
    30  // If there is an error, it will be of type *os.PathError.
    31  func IsSymlink(name string) (is bool, err error) {
    32  	fileInfo, err := os.Lstat(name)
    33  	if err != nil {
    34  		return false, err
    35  	}
    36  	return fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink, nil
    37  }