// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System.Threading.Tasks;
using AWS.Deploy.Common.IO;
namespace AWS.Deploy.Common.Recipes.Validation
{
///
/// Validates that a recipe or deployment bundle option with a FilePath typehint points to an actual file.
/// This can either be an absolute path to the file or relative to the project path
///
public class FileExistsValidator : IOptionSettingItemValidator
{
private readonly IFileManager _fileManager;
public FileExistsValidator(IFileManager fileManager)
{
_fileManager = fileManager;
}
public string ValidationFailedMessage { get; set; } = "The specified file does not exist";
///
/// Whether or not an empty filepath is valid (essentially whether this option is required)
///
public bool AllowEmptyString { get; set; } = true;
public Task Validate(object input, Recommendation recommendation, OptionSettingItem optionSettingItem)
{
var inputFilePath = input?.ToString() ?? string.Empty;
if (string.IsNullOrEmpty(inputFilePath))
{
if (AllowEmptyString)
{
return ValidationResult.ValidAsync();
}
else
{
return ValidationResult.FailedAsync("A file must be specified");
}
}
// Otherwise if there is a value, verify that it points to an actual file
if (_fileManager.Exists(inputFilePath, recommendation.GetProjectDirectory()))
{
return ValidationResult.ValidAsync();
}
else
{
return ValidationResult.FailedAsync($"The specified file {inputFilePath} does not exist");
}
}
}
}