terraform-provider-gitea/vendor/github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest/util.go

71 lines
1.6 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugintest
2022-04-03 04:07:16 +00:00
import (
"fmt"
2022-04-03 04:07:16 +00:00
"os"
"path/filepath"
)
func symlinkFile(src string, dest string) error {
err := os.Symlink(src, dest)
if err != nil {
return fmt.Errorf("unable to symlink %q to %q: %w", src, dest, err)
}
srcInfo, err := os.Stat(src)
if err != nil {
return fmt.Errorf("unable to stat %q: %w", src, err)
}
err = os.Chmod(dest, srcInfo.Mode())
if err != nil {
return fmt.Errorf("unable to set %q permissions: %w", dest, err)
2022-04-03 04:07:16 +00:00
}
return nil
2022-04-03 04:07:16 +00:00
}
// symlinkDirectoriesOnly finds only the first-level child directories in srcDir
// and symlinks them into destDir.
// Unlike symlinkDir, this is done non-recursively in order to limit the number
// of file descriptors used.
func symlinkDirectoriesOnly(srcDir string, destDir string) error {
2022-04-03 04:07:16 +00:00
srcInfo, err := os.Stat(srcDir)
if err != nil {
return fmt.Errorf("unable to stat source directory %q: %w", srcDir, err)
2022-04-03 04:07:16 +00:00
}
err = os.MkdirAll(destDir, srcInfo.Mode())
if err != nil {
return fmt.Errorf("unable to make destination directory %q: %w", destDir, err)
2022-04-03 04:07:16 +00:00
}
dirEntries, err := os.ReadDir(srcDir)
2022-04-03 04:07:16 +00:00
if err != nil {
return fmt.Errorf("unable to read source directory %q: %w", srcDir, err)
2022-04-03 04:07:16 +00:00
}
for _, dirEntry := range dirEntries {
if !dirEntry.IsDir() {
continue
2022-04-03 04:07:16 +00:00
}
srcPath := filepath.Join(srcDir, dirEntry.Name())
destPath := filepath.Join(destDir, dirEntry.Name())
err := symlinkFile(srcPath, destPath)
if err != nil {
return fmt.Errorf("unable to symlink directory %q to %q: %w", srcPath, destPath, err)
}
2022-04-03 04:07:16 +00:00
}
return nil
2022-04-03 04:07:16 +00:00
}