Skip to content

Commit

Permalink
Use "cp -r" to copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpeshansky committed Aug 7, 2024
1 parent f3a26c8 commit 8e2e5e1
Showing 1 changed file with 3 additions and 51 deletions.
54 changes: 3 additions & 51 deletions integration_test/gce/gce_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,58 +748,10 @@ func getGcloudConfigDir(ctx context.Context) (string, error) {
return out.Stdout, nil
}

// copyFile copies the contents of source into target.
func copyFile(source string, target string) error {
in, err := os.Open(source)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(target)
if err != nil {
return err
}
_, copyErr := io.Copy(out, in)
syncErr := out.Sync()
closeErr := out.Close()
return multierr.Combine(copyErr, syncErr, closeErr)
}

// copyDirectory copies the contents of the source directory into target.
func copyDirectory(source string, target string) error {
files, err := os.ReadDir(source)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if err := os.MkdirAll(target, 0700); err != nil {
return err
}
for _, file := range files {
s := filepath.Join(source, file.Name())
d := filepath.Join(target, file.Name())
sfi, err := os.Stat(s)
if err != nil {
return err
}
if sfi.IsDir() {
if err := copyDirectory(s, d); err != nil {
return err
}
} else {
if err := copyFile(s, d); err != nil {
return err
}
}
}
return nil
}

// SetupGcloudConfigDir sets up a new gcloud configuration directory.
// This copies the "configurations" subdirectory of the context-specified
// configuration directory into the new directory.
// This only works on Linux.
func SetupGcloudConfigDir(ctx context.Context, directory string) error {
currentConfigDir, err := getGcloudConfigDir(ctx)
if err != nil {
Expand All @@ -826,8 +778,8 @@ func SetupGcloudConfigDir(ctx context.Context, directory string) error {
return nil
}
// TODO: Replace with os.CopyFS() once available.
if err = copyDirectory(currentConfigDir, directory); err != nil {
return fmt.Errorf("error copying directory contents: %w", err)
if out, err := runCommand(ctx, log.New(io.Discard, "", 0), nil, []string{"cp", "-r", filepath.Join(currentConfigDir, "."), directory}, nil); err != nil {
return fmt.Errorf("error copying directory contents: %s (%w)", out.Stderr, err)
}
return nil
}
Expand Down

0 comments on commit 8e2e5e1

Please sign in to comment.