2022-08-06 14:21:18 +00:00
|
|
|
package plugintest
|
2022-04-03 04:07:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-06 14:21:18 +00:00
|
|
|
"fmt"
|
2022-04-03 04:07:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2022-08-06 14:21:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-06 14:21:18 +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.
|
2022-12-24 16:57:19 +00:00
|
|
|
func symlinkDirectoriesOnly(srcDir string, destDir string) error {
|
2022-04-03 04:07:16 +00:00
|
|
|
srcInfo, err := os.Stat(srcDir)
|
|
|
|
if err != nil {
|
2022-12-24 16:57:19 +00:00
|
|
|
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 {
|
2022-12-24 16:57:19 +00:00
|
|
|
return fmt.Errorf("unable to make destination directory %q: %w", destDir, err)
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 16:57:19 +00:00
|
|
|
dirEntries, err := os.ReadDir(srcDir)
|
|
|
|
|
2022-04-03 04:07:16 +00:00
|
|
|
if err != nil {
|
2022-12-24 16:57:19 +00:00
|
|
|
return fmt.Errorf("unable to read source directory %q: %w", srcDir, err)
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 16:57:19 +00:00
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
if !dirEntry.IsDir() {
|
|
|
|
continue
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 16:57:19 +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
|
|
|
}
|
2022-12-24 16:57:19 +00:00
|
|
|
|
|
|
|
return nil
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|