micro-gen/copy.go
Vasiliy Tolstov f902777e5b update
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-26 19:44:12 +03:00

48 lines
826 B
Go

package main
import (
"context"
"io"
"os"
"path/filepath"
"github.com/go-git/go-git/v5/plumbing/object"
"go.unistack.org/micro/v3/logger"
)
func writeFile(ctx context.Context, file *object.File, dir string, flag int, mode os.FileMode) error {
path := filepath.Join(dir, file.Name)
if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0o755)); err != nil {
return err
}
w, err := os.OpenFile(path, flag, mode)
if err != nil {
return err
}
defer func() {
if err := w.Close(); err != nil {
logger.Errorf(ctx, "failed to close file: %v", err)
}
}()
r, err := file.Reader()
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
logger.Errorf(ctx, "failed to close file: %v", err)
}
}()
if _, err = io.Copy(w, r); err != nil {
return err
}
return nil
}