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