- Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathToolInfoCommand.cs
81 lines (67 loc) · 2.48 KB
/
ToolInfoCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
usingSystem.Text;
usingSystem.Text.Json;
usingAmazon.Lambda.TestTool.Commands.Settings;
usingAmazon.Lambda.TestTool.Models;
usingAmazon.Lambda.TestTool.Services;
usingAmazon.Lambda.TestTool.Utilities;
usingSpectre.Console.Cli;
namespaceAmazon.Lambda.TestTool.Commands;
/// <summary>
/// Command to display tool information like the version of the tool.
/// </summary>
/// <param name="toolInteractiveService"></param>
publicclassToolInfoCommand(IToolInteractiveServicetoolInteractiveService)
:Command<ToolInfoCommandSettings>
{
/// <summary>
/// The method responsible for executing the <see cref="RunCommand"/>.
/// </summary>
publicoverrideintExecute(CommandContextcontext,ToolInfoCommandSettingssettings)
{
varinfo=CollectInformation();
varformattedInfo=settings.Formatswitch
{
ToolInfoCommandSettings.InfoFormat.Text=>GenerateToolInfoText(info),
ToolInfoCommandSettings.InfoFormat.Json=>GenerateToolInfoJson(info),
_ =>GenerateToolInfoText(info)
};
toolInteractiveService.WriteLine(formattedInfo);
returnCommandReturnCodes.Success;
}
privatestringGenerateToolInfoText(IDictionary<string,string>info)
{
varstringBuilder=newStringBuilder();
foreach(varkvpininfo)
{
stringBuilder.AppendLine($"{kvp.Key}: {kvp.Value}");
}
returnstringBuilder.ToString();
}
privatestringGenerateToolInfoJson(IDictionary<string,string>info)
{
varstream=newMemoryStream();
Utf8JsonWriterutf8JsonWriter=newUtf8JsonWriter(stream,options:newJsonWriterOptions()
{
Indented=false
});
utf8JsonWriter.WriteStartObject();
foreach(varkvpininfo)
{
utf8JsonWriter.WriteString(kvp.Key,kvp.Value);
}
utf8JsonWriter.WriteEndObject();
utf8JsonWriter.Flush();
stream.Position=0;
returnnewStreamReader(stream).ReadToEnd();
}
privateDictionary<string,string>CollectInformation()
{
varinfo=newDictionary<string,string>();
info["Version"]=Utils.DetermineToolVersion();
info["InstallPath"]=GetInstallPath();
returninfo;
}
privatestringGetInstallPath()=>Directory.GetParent(typeof(Utils).Assembly.Location)!.FullName;
}