From c63170a1085bb511df1c54e4c6522fe49ed1800d Mon Sep 17 00:00:00 2001 From: Nate Sales Date: Fri, 21 Jul 2023 14:35:41 -0700 Subject: [PATCH] fix: copy file --- pkg/process/process.go | 4 ++-- pkg/util/util.go | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/process/process.go b/pkg/process/process.go index ab34d197..286ab556 100644 --- a/pkg/process/process.go +++ b/pkg/process/process.go @@ -793,7 +793,7 @@ func Run(configFilename, lockFile, version string, noConfigure, dryRun, withdraw } // Copying manual configs - if err := util.CopyFileGlob(path.Join(c.BIRDDirectory, "manual*.conf"), c.CacheDirectory); err != nil { + if err := util.CopyFileToGlob(path.Join(c.BIRDDirectory, "manual*.conf"), c.CacheDirectory); err != nil { log.Fatalf("Copying manual config files: %v", err) } @@ -825,7 +825,7 @@ func Run(configFilename, lockFile, version string, noConfigure, dryRun, withdraw // Copy config file log.Debug("Copying Pathvector config file to cache directory") if err := util.CopyFile(configFilename, path.Join(c.CacheDirectory, "pathvector.yml")); err != nil { - log.Fatalf("Copying pathvector config file to cache directory: %v", err) + log.Fatalf("Copying Pathvector config file to cache directory: %v", err) } if !dryRun { diff --git a/pkg/util/util.go b/pkg/util/util.go index 041551cd..548c993e 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -153,9 +153,25 @@ func Ptr[T any](a T) *T { } // CopyFile copies a file from a source to destination -func CopyFile(source, destination_dir string) (err error) { +func CopyFile(src, dst string) error { + in, err := os.Open(src) + if err != nil { + return err + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return err + } + _, err = io.Copy(out, in) + defer out.Close() + return err +} + +// CopyFileTo copies a file from a source to destination directory +func CopyFileTo(source, destinationDir string) (err error) { _, destination := filepath.Split(source) - destination = filepath.Join(destination_dir, destination) + destination = filepath.Join(destinationDir, destination) src, err := os.Open(source) if err != nil { return err @@ -189,14 +205,14 @@ func CopyFile(source, destination_dir string) (err error) { return nil } -// CopyFileGlob copies files by glob to a destination -func CopyFileGlob(glob, dest string) error { +// CopyFileToGlob copies files by glob to a destination +func CopyFileToGlob(glob, dest string) error { files, err := filepath.Glob(glob) if err != nil { log.Fatal(err) } for _, f := range files { - if err := CopyFile(f, dest); err != nil { + if err := CopyFileTo(f, dest); err != nil { return err } }