Skip to content

Commit

Permalink
libmachine: move copy file function where it is used, in the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumerose authored and praveenkumar committed Feb 1, 2021
1 parent 3283d45 commit f91a0d6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
33 changes: 33 additions & 0 deletions pkg/drivers/hyperv/copy.go
@@ -0,0 +1,33 @@
package hyperv

import (
"io"
"os"
)

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
}

defer out.Close()

if _, err = io.Copy(out, in); err != nil {
return err
}

fi, err := os.Stat(src)
if err != nil {
return err
}

return os.Chmod(dst, fi.Mode())
}
@@ -1,4 +1,4 @@
package mcnutils
package hyperv

import (
"io/ioutil"
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestCopyFile(t *testing.T) {

destFilePath := filepath.Join(os.TempDir(), destFi.Name())

if err := CopyFile(srcFilePath, destFilePath); err != nil {
if err := copyFile(srcFilePath, destFilePath); err != nil {
t.Fatal(err)
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/drivers/hyperv/hyperv.go
Expand Up @@ -7,7 +7,6 @@ import (
"time"

log "github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/libmachine/mcnutils"
"github.com/code-ready/machine/libmachine/drivers"
"github.com/code-ready/machine/libmachine/state"
)
Expand Down Expand Up @@ -144,7 +143,7 @@ func (d *Driver) getDiskPath() string {
}

func (d *Driver) Create() error {
if err := mcnutils.CopyFile(d.ImageSourcePath, d.getDiskPath()); err != nil {
if err := copyFile(d.ImageSourcePath, d.getDiskPath()); err != nil {
return err
}

Expand Down
29 changes: 0 additions & 29 deletions pkg/libmachine/mcnutils/utils.go
Expand Up @@ -2,38 +2,9 @@ package mcnutils

import (
"fmt"
"io"
"os"
"time"
)

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
}

defer out.Close()

if _, err = io.Copy(out, in); err != nil {
return err
}

fi, err := os.Stat(src)
if err != nil {
return err
}

return os.Chmod(dst, fi.Mode())
}

func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error {
for i := 0; i < maxAttempts; i++ {
stop, err := f()
Expand Down

0 comments on commit f91a0d6

Please sign in to comment.