internal/file/utils.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package file import ( "io" "io/fs" "os" "gitlab.com/tozd/go/errors" ) func Mkdirp(dir string) errors.E { err := os.MkdirAll(dir, os.ModeDir|os.ModePerm) if err != nil { return errors.WithMessagef(err, "could not create directory %s", dir) } return nil } func NeedNotExist(err error) errors.E { if err != nil && !errors.Is(err, fs.ErrNotExist) { return errors.WithStack(err) } return nil } func StatIfExists(file string) (fs.FileInfo, errors.E) { stat, err := os.Stat(file) return stat, NeedNotExist(err) } func Exists(file string) (bool, errors.E) { stat, err := StatIfExists(file) return stat != nil, err } func WriteToFile(path string, body io.Reader) errors.E { file, err := os.Create(path) if err != nil { return errors.WithMessagef(err, "error creating file at %s", path) } defer file.Close() _, err = io.Copy(file, body) if err != nil { return errors.WithMessagef(err, "error downloading file %s", path) } return nil } |