// 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
{
///
/// Validator that validates if a given directory exists
///
public class DirectoryExistsValidator : IOptionSettingItemValidator
{
private readonly IDirectoryManager _directoryManager;
public DirectoryExistsValidator(IDirectoryManager directoryManager)
{
_directoryManager = directoryManager;
}
///
/// Validates that the given directory exists.
/// This can be either an absolute path, or a path relative to the project directory.
///
/// Path to validate
/// Selected recommendation, which may be used if the validator needs to consider properties other than itself
/// Selected option setting item, which may be used if the validator needs to consider properties other than itself
/// Valid if the directory exists, invalid otherwise
public Task Validate(object input, Recommendation recommendation, OptionSettingItem optionSettingItem)
{
var executionDirectory = (string)input;
if (!string.IsNullOrEmpty(executionDirectory) && !_directoryManager.Exists(executionDirectory))
return ValidationResult.FailedAsync("The specified directory does not exist.");
else
return ValidationResult.ValidAsync();
}
}
}