From 8e2e5e1ff7c3d904f20606d9bdabb5e17f25484f Mon Sep 17 00:00:00 2001 From: Igor Peshansky Date: Tue, 6 Aug 2024 21:05:54 -0400 Subject: [PATCH] Use "cp -r" to copy. --- integration_test/gce/gce_testing.go | 54 ++--------------------------- 1 file changed, 3 insertions(+), 51 deletions(-) diff --git a/integration_test/gce/gce_testing.go b/integration_test/gce/gce_testing.go index b752c7e111b..0bdc13e2441 100644 --- a/integration_test/gce/gce_testing.go +++ b/integration_test/gce/gce_testing.go @@ -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 { @@ -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 }