-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathProgram.cs
206 lines (178 loc) · 5.25 KB
/
Program.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Hosting;
using System.Linq;
using System.Reflection;
using UtilityBelt.Logging;
using UtilityBelt.Models;
namespace UtilityBelt
{
internal class Program
{
private static ILogger logger;
[ImportMany]
private static IEnumerable<IUtility> utilities { get; set; }
private static Dictionary<string, IUtility> menu;
private static Dictionary<string, IUtility> commands;
private static void Compose()
{
var configuration = new ContainerConfiguration()
.WithAssembly(typeof(Program).GetTypeInfo().Assembly);
using (var container = configuration.CreateContainer())
{
utilities = container.GetExports<IUtility>();
}
}
private static void GenerateMenu()
{
menu = new Dictionary<string, IUtility>();
foreach (var item in utilities)
{
menu.Add(item.Name.ToLower(), item);
}
}
private static void GenerateCommands()
{
commands = new Dictionary<string, IUtility>();
foreach (var item in utilities)
{
foreach (var c in item.Commands)
{
commands.Add(c, item);
}
}
}
private static void Main(string[] args)
{
// Logger
logger = Logger.InitLogger<Program>();
logger.LogInformation("Starting Application");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(Properties.Resources.ASCIIart);
Console.WriteLine("Loading...");
IServiceProvider services = ServiceProviderBuilder.GetServiceProvider(args);
IOptions<SecretsModel> options = services.GetRequiredService<IOptions<SecretsModel>>();
// load all utilities from the current assembly into Utilities list
// and generate future menu and command list
Compose();
GenerateMenu();
GenerateCommands();
bool showMenu = true;
do
{
logger.LogInformation("Load Menu Options");
MenuOptions(options);
showMenu = RecursiveOptions();
} while (showMenu);
}
#region Choice Handler
private static bool RecursiveOptions()
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Green;
string testUserInput = null;
do
{
Console.Write("Would you like to run another option?: ");
testUserInput = Console.ReadLine();
if (int.TryParse(testUserInput, out _)) //If user input was a number...
{
logger.LogWarning("Answer could not be translated to a yes/no");
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.\n");
testUserInput = null;
}
} while (testUserInput == null);
try
{
logger.LogInformation($"Get user Input : {testUserInput}");
return FromString(testUserInput);
}
catch
{
logger.LogWarning($"Answer could not be translated to a yes/no : {testUserInput}");
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.");
return RecursiveOptions();
}
}
private static void MenuOptions(IOptions<SecretsModel> options)
{
logger.LogInformation("Showing Menu Options");
var itemNumber = 1;
foreach (var item in menu.Keys)
{
Console.WriteLine($"{itemNumber}) {menu[item].Name}");
itemNumber++;
}
Console.WriteLine("0) Exit");
Console.WriteLine("");
Console.Write("Your choice:");
string optionPicked = Console.ReadLine().ToLower();
logger.LogInformation($"User Choice : {optionPicked}");
var isNumberPicked = int.TryParse(optionPicked, out int optionNumber);
IUtility util = null;
if (isNumberPicked)
{
if (optionNumber == 0)
Environment.Exit(0);
if (optionNumber > 0 && optionNumber <= menu.Keys.ToList().Count)
{
var key = menu.Keys.ToList()[optionNumber - 1];
util = menu[key];
}
}
else
{
if (menu.ContainsKey(optionPicked))
{
util = menu[optionPicked];
}
else
{
if (commands.ContainsKey(optionPicked))
{
util = commands[optionPicked];
}
}
}
if (util != null)
{
logger.LogInformation($"User Choice : {util.Name}");
util.Configure(options);
util.Run();
}
else
{
Console.WriteLine("Please make a valid option");
MenuOptions(options);
}
}
#endregion Choice Handler
#region Utility
private enum BooleanAliases
{
YES = 1,
AYE = 1,
COOL = 1,
TRUE = 1,
Y = 1,
YEAH = 1,
NAW = 0,
NO = 0,
FALSE = 0,
N = 0
}
private static bool FromString(string str)
{
return Convert.ToBoolean(Enum.Parse(typeof(BooleanAliases), str.ToUpper()));
}
#endregion Utility
}
}