Skip to content

Commit 786d4f7

Browse files
committed
Added 'show-key' command.
1 parent f93496b commit 786d4f7

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using AppCore.SigningTool.Extensions;
3+
using AppCore.SigningTool.StrongName;
4+
using dnlib.DotNet;
5+
using McMaster.Extensions.CommandLineUtils;
6+
7+
namespace AppCore.SigningTool.Commands.Sn
8+
{
9+
public class SnShowKeyCommand : ICommand<SnCommand>
10+
{
11+
private readonly IStrongNameKeyLoader _keyLoader;
12+
private CommandOption<bool> _showKey;
13+
private CommandArgument _keyFile;
14+
15+
public string Name => "show-key";
16+
17+
public string Description => "Displays the public key token.";
18+
19+
public SnShowKeyCommand(IStrongNameKeyLoader keyLoader)
20+
{
21+
_keyLoader = keyLoader;
22+
}
23+
24+
public void Configure(CommandLineApplication cmd)
25+
{
26+
_showKey = cmd.Option<bool>(
27+
"--with-key",
28+
"Output the public key in addition to the key token.",
29+
CommandOptionType.NoValue);
30+
31+
_keyFile = cmd.Argument("KEYFILE", "The path of the public key file.")
32+
.IsRequired(false, "The 'KEYFILE' argument is required.");
33+
}
34+
35+
public int Execute(CommandLineApplication cmd)
36+
{
37+
bool showKey = _showKey.HasValue();
38+
string keyFile = _keyFile.Value;
39+
40+
StrongNamePublicKey publicKey;
41+
try
42+
{
43+
publicKey = _keyLoader.LoadPublicKey(keyFile);
44+
}
45+
catch (Exception error)
46+
{
47+
cmd.Error.WriteLine("ERROR: {0}", error.Message);
48+
return ExitCodes.FromException(error);
49+
}
50+
51+
if (showKey)
52+
{
53+
cmd.Out.WriteLine("Public key (hash algorithm {0}) is:");
54+
cmd.Out.WriteLine(publicKey.CreatePublicKey().ToHexString());
55+
cmd.Out.WriteLine();
56+
}
57+
58+
cmd.Out.WriteLine("Public key token is:");
59+
cmd.Out.WriteLine(publicKey.CreatePublicKeyToken().ToHexString());
60+
61+
return ExitCodes.Success;
62+
}
63+
}
64+
}

src/AppCore.SigningTool/ExitCodes.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ internal static class ExitCodes
1212
{
1313
public const int Success = 0;
1414

15-
public const int FileAreadyExists = 1;
15+
public const int UnrecognizedCommandOrArgument = 1;
1616

1717
public const int FileNotFound = 2;
1818

1919
public const int InvalidKey = 3;
2020

2121
public const int InvalidAssembly = 4;
2222

23+
public const int FileAreadyExists = 5;
24+
2325
public const int Unknown = -1;
2426

2527
public static int FromException(Exception error)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace AppCore.SigningTool.Extensions
4+
{
5+
internal static class ByteArrayExtensions
6+
{
7+
public static string ToHexString(this byte[] array)
8+
{
9+
return BitConverter.ToString(array)
10+
.Replace("-", "")
11+
.ToLowerInvariant();
12+
}
13+
}
14+
}

src/AppCore.SigningTool/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static int Main(string[] args)
1919
Name = "dotnet-signtool"
2020
};
2121

22+
app.MakeSuggestionsInErrorMessage = true;
2223
app.VersionOptionFromAssemblyAttributes(typeof(Program).Assembly);
2324
app.HelpOption("-h|--help", true);
2425
app.OnExecute(
@@ -30,7 +31,21 @@ static int Main(string[] args)
3031

3132
app.RegisterCommands(serviceProvider);
3233

33-
return app.Execute(args);
34+
try
35+
{
36+
return app.Execute(args);
37+
}
38+
catch (UnrecognizedCommandParsingException error)
39+
{
40+
app.Error.WriteLine(error.Message + ".");
41+
app.Out.WriteLine("Did you mean '{0}' ?", string.Join(' ', error.NearestMatches));
42+
}
43+
catch (CommandParsingException error)
44+
{
45+
app.Error.WriteLine(error.Message);
46+
}
47+
48+
return ExitCodes.UnrecognizedCommandOrArgument;
3449
}
3550

3651
private static ServiceProvider CreateServiceProvider()
@@ -49,6 +64,7 @@ private static void ConfigureServices(IServiceCollection services)
4964
services.AddSingleton<ICommand, SnCommand>();
5065
services.AddSingleton<ICommand<SnCommand>, SnCreateKeyCommand>();
5166
services.AddSingleton<ICommand<SnCommand>, SnExportPublicKeyCommand>();
67+
services.AddSingleton<ICommand<SnCommand>, SnShowKeyCommand>();
5268
services.AddSingleton<ICommand<SnCommand>, SnSignCommand>();
5369
}
5470
}

0 commit comments

Comments
 (0)