github.com/sandwich-go/boost@v1.3.29/xos/file.go (about)

     1  package xos
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/sandwich-go/boost/xpanic"
    11  )
    12  
    13  // FileCopyToDir the src file to dst. Any existing file will be overwritten and will not
    14  // copy file attributes.
    15  func FileCopyToDir(src, dstDir string) error {
    16  	in, err := os.Open(src)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	defer func() {
    21  		_ = in.Close()
    22  	}()
    23  
    24  	out, err0 := os.Create(filepath.Join(dstDir, filepath.Base(src)))
    25  	if err0 != nil {
    26  		return err0
    27  	}
    28  	defer func() {
    29  		_ = out.Close()
    30  	}()
    31  
    32  	_, err = io.Copy(out, in)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	return nil
    37  }
    38  
    39  // Ext 返回后缀,例如 'xxx.go' => '.go'
    40  func Ext(path string) string {
    41  	ext := filepath.Ext(path)
    42  	if p := strings.IndexByte(ext, '?'); p != -1 {
    43  		ext = ext[0:p]
    44  	}
    45  	return ext
    46  }
    47  
    48  // FileGetContents 获取文件内容
    49  func FileGetContents(filename string) ([]byte, error) {
    50  	return ioutil.ReadFile(filename)
    51  }
    52  
    53  // MustFilePutContents 写入文件,如果发生错误则panic
    54  func MustFilePutContents(filename string, content []byte) {
    55  	dirName := filepath.Dir(filename)
    56  	xpanic.WhenErrorAsFmtFirst(os.MkdirAll(dirName, os.ModePerm), "got error:%w while MkdirAll with:%s", dirName)
    57  	xpanic.WhenErrorAsFmtFirst(ioutil.WriteFile(filename, content, 0644), "got error:%w while WriteFile with:%s", filename)
    58  }
    59  
    60  // FilePutContents 写入文件
    61  func FilePutContents(filename string, content []byte) error {
    62  	dirName := filepath.Dir(filename)
    63  	err := os.MkdirAll(dirName, os.ModePerm)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	return ioutil.WriteFile(filename, content, 0644)
    68  }
    69  
    70  // MustGetFileWriter 获取写文件句柄
    71  // filePath 指定文件
    72  // prepend 是否保留源数据,如果保留,则源数据会被追加到文件尾
    73  func MustGetFileWriter(filePath string, prepend bool) (writer io.Writer, deferFunc func()) {
    74  	var prependData []byte
    75  	if prepend {
    76  		if ExistsFile(filePath) {
    77  			fileContent, err := FileGetContents(filePath)
    78  			xpanic.WhenErrorAsFmtFirst(err, "got error:%w while FileGetContents:%s", filePath)
    79  			prependData = fileContent
    80  		}
    81  	}
    82  	dirParent := filepath.Dir(filePath)
    83  	xpanic.WhenErrorAsFmtFirst(os.MkdirAll(dirParent, os.ModePerm), "got error:%w while MkdirAll:%s", dirParent)
    84  	output, err := os.Create(filePath)
    85  	xpanic.WhenErrorAsFmtFirst(err, "got error:%w while Create:%s", filePath)
    86  	return output, func() {
    87  		_, _ = output.Write(prependData)
    88  		_ = output.Close()
    89  	}
    90  }