This commit is contained in:
Chris Cheetham 2020-02-17 11:12:13 -05:00
Родитель 35a913b2a5
Коммит a147888d8f
5 изменённых файлов: 156 добавлений и 7 удалений

Просмотреть файл

@ -12,8 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using Steeltoe.Tooling;
using Steeltoe.Tooling.Controllers;
namespace Steeltoe.Cli
@ -32,7 +36,7 @@ Examples:
public const string CommandName = "show-topic";
[Argument(0, Name = "topic", Description = "Topic")]
private string Topic { get; set; }
private string Topic { get; } = null;
public ShowTopicCommand(IConsole console) : base(console)
{
@ -40,7 +44,66 @@ Examples:
protected override Controller GetController()
{
throw new NotImplementedException();
return new ShowTopicController(Topic);
}
private class ShowTopicController : Controller
{
private static readonly ILogger<ShowTopicController> Logger =
Logging.LoggerFactory.CreateLogger<ShowTopicController>();
private readonly string _topicsPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"steeltoe.rc",
"topics");
private readonly string _topic;
public ShowTopicController(string topic)
{
_topic = topic;
}
protected override void Execute()
{
Logger.LogDebug($"topics path: {_topicsPath}");
if (_topic == null)
{
ListTopics();
}
else
{
ShowTopic(_topic);
}
}
private void ListTopics()
{
var topicPaths = Directory.GetFiles(_topicsPath, "*.txt");
var maxTopicLength = topicPaths.Select(Path.GetFileNameWithoutExtension)
.Select(topic => topic.Length).Concat(new[] {0}).Max();
var descriptionColumn = maxTopicLength + 4;
foreach (var topicPath in topicPaths)
{
var topic = Path.GetFileNameWithoutExtension(topicPath);
var description = File.ReadLines(topicPath).First();
Context.Console.WriteLine($"{topic.PadRight(descriptionColumn)}{description}");
}
}
private void ShowTopic(string topic)
{
var topicPath = Path.Combine(_topicsPath, $"{topic}.txt");
if (!File.Exists(topicPath))
{
throw new ItemDoesNotExistException(topic);
}
foreach (var line in File.ReadLines(topicPath))
{
Context.Console.WriteLine(line);
}
}
}
}
}

Просмотреть файл

@ -20,7 +20,7 @@ namespace Steeltoe.Tooling
public abstract class Shell
{
/// <summary>
/// Implemetations return the result of running a command.
/// Implementations return the result of running a command.
/// </summary>
/// <param name="command">System command.</param>
/// <param name="args">Command arguments.</param>

Просмотреть файл

@ -0,0 +1,38 @@
Application and Service Autodetection
Application Autodetection
Name
The application name is determined, in order of precedence:
* The value of the –-name option
* The output directory name if the output directory contains the file <name>.csproj
* The file name <name>.csproj if the output directory contains one and only file with extension .csproj
If none of the above is satisfied, an error occurs.
Framework
The application framework is determined, in order of precedence:
* The value of the –-framework option
* The value of the <TargetFramework> element in the project .csproj file
* The first framework listed in the <TargetFrameworks> element in the project .csproj file
If none of the above is satisfied, an error occurs.
Service Autodetection
Services are autodetected by searching a projects .csproj file for the presence of any of a services dependencies.
A dependency is considered present if any of the .csproj file elements matches one of the following XPath expressions:
/Project/ItemGroup/PackageReference[@Include='<dep>']
/Project/ItemGroup/Reference[@Include='<dep>']
Examples:
* A project with a dependency on RabbitMQ:
<Project>
<ItemGroup>
<PackageReference Include="RabbitMQ.Client" />
</ItemGroup>
</Project>
* A project with a dependency on GemFire:
<Project>
<ItemGroup>
<Reference Include="Pivotal.GemFire" />
</ItemGroup>
</Project>

Просмотреть файл

@ -170,9 +170,17 @@ namespace scratch
var actualMessages = actual.ToArray();
// '*' denotes we don't care
// '*' denotes we don't care about this line
// '***' denotes we don't care about any further lines
for (var i = 0; i < messages.Length; ++i)
{
if (messages[i] == "***")
{
Array.Resize(ref messages, i);
Array.Resize(ref actualMessages, i);
break;
}
if (messages[i] == "*")
{
actualMessages[i] = "*";

Просмотреть файл

@ -23,7 +23,7 @@ namespace Steeltoe.Cli.Test
public void ShowTopicHelp()
{
Runner.RunScenario(
given => a_dotnet_project("show_topic_help"),
given => an_empty_directory("show_topic_help"),
when => the_developer_runs_cli_command("show-topic --help"),
then => the_cli_command_should_succeed(),
and => the_cli_should_output(new[]
@ -47,10 +47,50 @@ namespace Steeltoe.Cli.Test
public void ShowTopicTooManyArgs()
{
Runner.RunScenario(
given => a_dotnet_project("show_topic_too_many_args"),
given => an_empty_directory("show_topic_too_many_args"),
when => the_developer_runs_cli_command("show-topic arg1 arg2"),
then => the_cli_should_fail_parse("Unrecognized command or argument 'arg2'")
);
}
[Scenario]
public void ShowTopicList()
{
Runner.RunScenario(
given => an_empty_directory("show_topic_list"),
when => the_developer_runs_cli_command("show-topic"),
then => the_cli_command_should_succeed(),
and => the_cli_should_output(new[]
{
"autodetection Application and Service Autodetection",
})
);
}
[Scenario]
public void ShowTopicNotFound()
{
Runner.RunScenario(
given => an_empty_directory("show_topic_not_found"),
when => the_developer_runs_cli_command("show-topic no-such-topic"),
then => the_cli_should_error(ErrorCode.Tooling, "'no-such-topic' does not exist")
);
}
[Scenario]
public void ShowTopicAutodetection()
{
Runner.RunScenario(
given => an_empty_directory("show_topic_autodetection"),
when => the_developer_runs_cli_command("show-topic autodetection"),
then => the_cli_command_should_succeed(),
and => the_cli_should_output(new[]
{
"Application and Service Autodetection",
"Application Autodetection",
"***",
})
);
}
}
}