From e0a2c0bf3746397e6d3e65e8dd013eb7b223ce37 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 2 Dec 2024 21:53:43 -0800 Subject: [PATCH] Added documentation and merged 2 if statments. --- docs/types.md | 3 ++- internal/common/filespec.go | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/types.md b/docs/types.md index a3f024f0..46e701fe 100644 --- a/docs/types.md +++ b/docs/types.md @@ -785,8 +785,9 @@ All types of FileSpecs share some common fields: A FileSpec with `type` equal to `path` points to an absolute or relative path accessible from the current machine. The path may also include a glob pattern to target multiple files or directories. -For paths that match multiple dirents, the dirents will be copied into `dest`. +For paths that match multiple dirents, the dirents will be copied into `dest` only if `dest` is a directory. If `dest` does not exist, it will be automatically created as a directory. +Attempting to copy multiple dirents into `dest` if `dest` is an existing file will result in an error. When a relative path is specified, additional context is required to know the relative base. For example, a FileSpec in an assignment config is relative to the assignment directory (the directory where the `assignment.json` file lives). diff --git a/internal/common/filespec.go b/internal/common/filespec.go index b7473520..5d6b8d02 100644 --- a/internal/common/filespec.go +++ b/internal/common/filespec.go @@ -270,14 +270,17 @@ func (this *FileSpec) copyPaths(baseDir string, destDir string, onlyContents boo return fmt.Errorf("No targets found for the path '%s'.", this.Path) } - if util.IsFile(destPath) && len(paths) > 1 { - return fmt.Errorf("Cannot copy multiple targets into the existing file '%s'.", destDir) - } + // Ensure destPath is a directory if there are multiple paths. + if len(paths) > 1 { + if util.IsFile(destPath) { + return fmt.Errorf("Cannot copy multiple targets into the existing file '%s'.", destDir) + } - if !util.PathExists(destPath) && len(paths) > 1 { - err := util.MkDir(destPath) - if err != nil { - return fmt.Errorf("Failed to make a directory for the Filespec at path '%s': '%v'.", destPath, err) + if !util.PathExists(destPath) { + err := util.MkDir(destPath) + if err != nil { + return fmt.Errorf("Failed to create a directory for the Filespec at path '%s': '%v'.", destPath, err) + } } }