-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
180 lines (142 loc) · 6.27 KB
/
Program.cs
File metadata and controls
180 lines (142 loc) · 6.27 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
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
using CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using WinRepo.PowerShell;
using WinRepoSearch.Core;
using WinRepoSearch.Core.Services;
using WinRepoSearch.Core.ViewModels;
namespace WinRepoConsole
{
class Program
{
public static IHost ServiceHost { get; private set; }
[ModuleInitializer]
static internal void MyInitializer()
{
//Logger.LogDebug("Initializing Host.");
var builder = CreateHostBuilder();
ServiceHost = builder.Build();
ServiceHost.RunAsync();
}
public static IHostBuilder CreateHostBuilder() =>
Host.CreateDefaultBuilder()
.ConfigureContainer<IServiceCollection>(collection =>
{
Startup.ConfigureServices(collection);
//Logger.LogDebug("ConfiguredServices.");
});
private static ILogger? Logger { get; set; }
public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
public bool Verbose { get; set; }
[Option('q', "query", Required = true, HelpText = "The search criteria.")]
public string Query { get; set; }
[Option('r', "repo", Required = false, HelpText = "Repository to search. Default = All.")]
public DefaultRepos Repo { get; set; } = DefaultRepos.All;
}
private static ParserResult<Options> CommandBuilder(params string[] args)
=> Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if (o.Query is not null and { Length: > 0 })
{
Console.WriteLine($"Searching for: {o.Query}");
}
Console.WriteLine($"Repository to search: {o.Repo}");
});
static async Task Main(string[] args)
{
var parsed = CommandBuilder(args);
var service = ServiceHost.Services.GetRequiredService<SearchService>();
var logger = ServiceHost.Services.GetRequiredService<ILogger<Program>>();
var viewModel = ServiceHost.Services.GetRequiredService<SearchViewModel>();
DefaultRepos repo = DefaultRepos.All;
Commands command = Commands.search;
string query = "";
bool isVerbose = false;
parsed.WithParsed(options =>
{
(repo, command, query, isVerbose) = args.FirstOrDefault()?.ToLowerInvariant() switch {
nameof(Commands.show) => (options.Repo, Commands.show, options.Query, options.Verbose),
nameof(Commands.install) => (options.Repo, Commands.install, options.Query, options.Verbose),
nameof(Commands.list) => (options.Repo, Commands.list, options.Query, options.Verbose),
nameof(Commands.uninstall) => (options.Repo, Commands.uninstall, options.Query, options.Verbose),
nameof(Commands.upgrade) => (options.Repo, Commands.upgrade, options.Query, options.Verbose),
_ => (options.Repo, Commands.search, options.Query, options.Verbose)
};
});
Logger = Startup.Services?.GetRequiredService<ILogger<Program>>();
Logger.LogDebug($"SearchRepositoryCmdlet.ProcessRecord: Enter - SearchTerm: {query}, RepoToSearch: {repo}");
if (string.IsNullOrWhiteSpace(query))
{
return;
}
viewModel
.Repositories
.ToList()
.ForEach(r =>
r.IsEnabled =
(repo == DefaultRepos.All) ||
r.RepositoryName.Equals(repo.ToString(), StringComparison.OrdinalIgnoreCase)
);
viewModel.SearchTerm = query;
viewModel.Command = command;
viewModel.IsVerbose = isVerbose;
try
{
switch(command)
{
case Commands.list:
await Show(service, viewModel);
break;
case Commands.search:
await Search(service, viewModel);
break;
case Commands.install:
break;
case Commands.show:
break;
case Commands.uninstall:
break;
case Commands.upgrade:
break;
}
}
catch (AggregateException ae)
{
Console.Error.WriteLine(ae.ToString());
}
}
private static async Task Search(SearchService service, SearchViewModel viewModel)
{
Logger.LogDebug("Search: Before PerformSearchAsync");
var results = service.PerformSearchAsync(viewModel);
Logger.LogDebug("Search: After PerformSearchAsync");
await foreach (var resultSet in results)
{
Logger.LogDebug("Search: Moved Next");
Logger.LogDebug($"Search: Found {resultSet.Result.Count()} results in {resultSet.Result.FirstOrDefault()?.Repo.RepositoryName}");
resultSet.Result.ToList().ForEach(r => Console.WriteLine(r.ListMarkdown));
Logger.LogDebug($"Search: wrote: {resultSet.Result.Count()} records.");
}
}
private static async Task Show(SearchService service, SearchViewModel viewModel)
{
Logger.LogDebug("Show: Before PerformSearchAsync");
var results = await service.PerformGetInfoAsync(viewModel);
Logger.LogDebug("Show: After PerformSearchAsync");
Console.WriteLine(results.Result.FirstOrDefault()?.Markdown ?? "<Not found>");
}
}
}