in src/authoring/Elastic.Documentation.Refactor/Move.cs [165:234]
private bool ValidateInputs(string source, string target, out IFileInfo[] fromFiles, out IFileInfo[] toFiles)
{
fromFiles = [];
toFiles = [];
var fromFile = readFileSystem.FileInfo.New(source);
var fromDirectory = readFileSystem.DirectoryInfo.New(source);
var toFile = readFileSystem.FileInfo.New(target);
var toDirectory = readFileSystem.DirectoryInfo.New(target);
//from does not exist at all
if (!fromFile.Exists && !fromDirectory.Exists)
{
if (!string.IsNullOrEmpty(fromFile.Extension))
_logger.LogError("Source file '{File}' does not exist", fromFile);
else
_logger.LogError("Source directory '{Directory}' does not exist", fromDirectory);
return false;
}
//moving file
if (fromFile.Exists)
{
if (!fromFile.Extension.Equals(".md", OrdinalIgnoreCase))
{
_logger.LogError("Source path must be a markdown file. Directory paths are not supported yet");
return false;
}
//if toFile has no extension assume move to folder
if (toFile.Extension == string.Empty)
toFile = readFileSystem.FileInfo.New(Path.Combine(toDirectory.FullName, fromFile.Name));
if (!toFile.Extension.Equals(".md", OrdinalIgnoreCase))
{
_logger.LogError("Target path '{FullName}' must be a markdown file.", toFile.FullName);
return false;
}
if (toFile.Exists)
{
_logger.LogError("Target file {Target} already exists", target);
return false;
}
fromFiles = [fromFile];
toFiles = [toFile];
}
//moving folder
else if (fromDirectory.Exists)
{
if (toDirectory.Exists)
{
_logger.LogError("Target directory '{FullName}' already exists.", toDirectory.FullName);
return false;
}
if (toDirectory.FullName.StartsWith(fromDirectory.FullName, OrdinalIgnoreCase))
{
_logger.LogError("Can not move source directory '{SourceDirectory}' to a '{TargetFile}'", toDirectory.FullName, toFile.FullName);
return false;
}
fromFiles = fromDirectory.GetFiles("*.md", SearchOption.AllDirectories);
toFiles = [.. fromFiles.Select(f =>
{
var relative = Path.GetRelativePath(fromDirectory.FullName, f.FullName);
return readFileSystem.FileInfo.New(Path.Combine(toDirectory.FullName, relative));
})];
}
return true;
}