in src/AWS.Deploy.CLI/ConsoleUtilities.cs [261:335]
public UserResponse<T> AskUserToChooseOrCreateNew<T>(IEnumerable<T> options, string title, UserInputConfiguration<T> userInputConfiguration, string? defaultChoosePrompt = null, string? defaultCreateNewPrompt = null, string? defaultCreateNewLabel = null)
{
var optionStrings = options.Select(userInputConfiguration.DisplaySelector);
var defaultOption = options.FirstOrDefault(userInputConfiguration.DefaultSelector);
var defaultValue = "";
var createNewLabel = !string.IsNullOrEmpty(defaultCreateNewLabel) ? defaultCreateNewLabel : Constants.CLI.CREATE_NEW_LABEL;
if (defaultOption != null)
{
defaultValue = userInputConfiguration.DisplaySelector(defaultOption);
}
else
{
if (userInputConfiguration.CurrentValue != null && string.IsNullOrEmpty(userInputConfiguration.CurrentValue.ToString()))
defaultValue = Constants.CLI.EMPTY_LABEL;
else
defaultValue = userInputConfiguration.CreateNew || !options.Any() ? createNewLabel : userInputConfiguration.DisplaySelector(options.First());
}
var displayOptionStrings = new List<string>();
// add empty option at the top if configured
if (userInputConfiguration.EmptyOption)
{
displayOptionStrings.Add(Constants.CLI.EMPTY_LABEL);
}
// add all the options, this can be empty list if there are no options
// e.g. selecting a role for a service when there are no roles with a service principal
displayOptionStrings.AddRange(optionStrings);
// add create new option at the bottom if configured
if (userInputConfiguration.CreateNew)
{
displayOptionStrings.Add(createNewLabel);
}
// if list contains any options, ask user to choose one
if (displayOptionStrings.Any())
{
var selectedString = AskUserToChoose(displayOptionStrings, title, defaultValue, defaultChoosePrompt);
if (selectedString == Constants.CLI.EMPTY_LABEL)
{
return new UserResponse<T>
{
IsEmpty = true
};
}
if (selectedString != createNewLabel)
{
var selectedOption = options.FirstOrDefault(option => userInputConfiguration.DisplaySelector(option) == selectedString);
return new UserResponse<T>
{
SelectedOption = selectedOption,
CreateNew = false
};
}
}
if (userInputConfiguration.AskNewName)
{
var newName = AskUserForValue(string.Empty, userInputConfiguration.DefaultNewName, false, defaultAskValuePrompt: defaultCreateNewPrompt);
return new UserResponse<T>
{
CreateNew = true,
NewName = newName
};
}
return new UserResponse<T>
{
CreateNew = true,
};
}