-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.cs
More file actions
100 lines (91 loc) · 2.99 KB
/
CommandLine.cs
File metadata and controls
100 lines (91 loc) · 2.99 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using IISWeb.Services;
namespace IISWeb;
public static class CommandLine
{
public static bool IsCommand(string[] args, out string command)
{
command = string.Empty;
if (args.Length == 0) return false;
var first = args[0];
if (string.Equals(first, "seed-admin", StringComparison.OrdinalIgnoreCase) ||
string.Equals(first, "--seed-admin", StringComparison.OrdinalIgnoreCase))
{
command = "seed-admin";
return true;
}
return false;
}
public static async Task<int> SeedAdminAsync(IUserService users, string[] args)
{
string? username = null;
string? password = null;
for (int i = 1; i < args.Length; i++)
{
switch (args[i])
{
case "--username" or "-u" when i + 1 < args.Length:
username = args[++i]; break;
case "--password" or "-p" when i + 1 < args.Length:
password = args[++i]; break;
case "--help" or "-h":
PrintUsage(); return 0;
}
}
if (string.IsNullOrWhiteSpace(username))
{
PrintUsage();
return 2;
}
if (string.IsNullOrEmpty(password))
{
password = ReadPasswordFromConsole("Password: ");
var confirm = ReadPasswordFromConsole("Confirm: ");
if (!string.Equals(password, confirm, StringComparison.Ordinal))
{
Console.Error.WriteLine("Passwords do not match.");
return 3;
}
}
try
{
await users.CreateAdminAsync(username!, password!);
Console.WriteLine($"Admin '{username}' has been created.");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to create admin: {ex.Message}");
return 1;
}
}
private static void PrintUsage()
{
Console.Error.WriteLine("Usage:");
Console.Error.WriteLine(" IISWeb.exe seed-admin --username <user> [--password <pwd>]");
Console.Error.WriteLine();
Console.Error.WriteLine("If --password is omitted you will be prompted (input is not echoed).");
Console.Error.WriteLine("Passwords must be at least 12 characters long.");
}
private static string ReadPasswordFromConsole(string prompt)
{
Console.Write(prompt);
var sb = new System.Text.StringBuilder();
while (true)
{
var key = Console.ReadKey(intercept: true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (key.Key == ConsoleKey.Backspace)
{
if (sb.Length > 0) sb.Length--;
continue;
}
if (!char.IsControl(key.KeyChar))
sb.Append(key.KeyChar);
}
return sb.ToString();
}
}