-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReproHeadersMiddleware.cs
217 lines (192 loc) · 8.51 KB
/
ReproHeadersMiddleware.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
207
208
209
210
211
212
213
214
215
216
217
using CommandDotNet;
using CommandDotNet.Builders;
using CommandDotNet.Execution;
using CommandDotNet.Parsing;
using Jira2AzureDevOps.Logic.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jira2AzureDevOps.Console.Framework
{
public static class ReproHeadersMiddleware
{
public static AppRunner UseReproHeaders(this AppRunner appRunner,
Action<string> writer = null,
Predicate<CommandContext> skipCommand = null,
Predicate<IArgument> skipArgument = null,
Predicate<IArgument> obscureArgument = null,
bool includeToolVersion = false,
bool includeDotNetVersion = false,
bool includeOsVersion = false,
bool includeMachine = false,
bool includeUsername = false,
Func<CommandContext, IEnumerable<(string name, string text)>> additionalHeadersCallback = null)
{
return appRunner.Configure(c =>
{
c.Services.Add(new LogHeaderConfig(
writer, skipCommand, skipArgument, obscureArgument,
includeToolVersion, includeDotNetVersion, includeOsVersion,
includeMachine, includeUsername,
additionalHeadersCallback));
c.UseMiddleware(LogHeader, MiddlewareStages.PostBindValuesPreInvoke);
});
}
private class LogHeaderConfig
{
public Action<string> Writer { get; }
public Predicate<CommandContext> SkipCommand { get; }
public Predicate<IArgument> SkipArgument { get; }
public Predicate<IArgument> ObscureArgument { get; }
public bool IncludeToolVersion { get; }
public bool IncludeDotNetVersion { get; }
public bool IncludeOsVersion { get; }
public bool IncludeMachine { get; }
public bool IncludeUsername { get; }
public Func<CommandContext, IEnumerable<(string name, string text)>> AdditionalHeadersCallback { get; }
public LogHeaderConfig(Action<string> writer,
Predicate<CommandContext> skipCommand,
Predicate<IArgument> skipArgument,
Predicate<IArgument> obscureArgument,
bool includeToolVersion,
bool includeDotNetVersion,
bool includeOsVersion,
bool includeMachine,
bool includeUsername,
Func<CommandContext, IEnumerable<(string name, string text)>> additionalHeadersCallback)
{
Writer = writer;
SkipCommand = skipCommand;
SkipArgument = skipArgument;
ObscureArgument = obscureArgument;
IncludeToolVersion = includeToolVersion;
IncludeDotNetVersion = includeDotNetVersion;
IncludeOsVersion = includeOsVersion;
IncludeMachine = includeMachine;
IncludeUsername = includeUsername;
AdditionalHeadersCallback = additionalHeadersCallback;
}
}
private static Task<int> LogHeader(CommandContext commandContext, ExecutionDelegate next)
{
var config = commandContext.AppConfig.Services.Get<LogHeaderConfig>();
if (config.SkipCommand?.Invoke(commandContext) ?? false)
{
return next(commandContext);
}
PrintHeader(commandContext, config);
return next(commandContext);
}
private static void PrintHeader(CommandContext commandContext, LogHeaderConfig config)
{
var parseResult = commandContext.ParseResult;
var targetCommand = parseResult.TargetCommand;
var sb = new StringBuilder(Environment.NewLine);
sb.AppendLine("***************************************");
sb.AppendLine($" Command: {targetCommand.GetParentCommands(true).Reverse().Skip(1).Select(c => c.Name).ToCsv(" ")}");
sb.LogArguments("Arguments", targetCommand.Operands, config, parseResult);
sb.LogArguments("Options", IncludeInherited(targetCommand, c => c.Options).Where(o => !o.IsMiddlewareOption), config, parseResult);
sb.AppendLine();
sb.AppendLine(" Original input:");
sb.AppendLine($" {commandContext.Original.Args.ToCsv(" ")}");
var otherConfigEntries = GetOtherConfigInfo(commandContext, config).ToList();
if (!otherConfigEntries.IsNullOrEmpty())
{
sb.AppendLine();
var maxName = otherConfigEntries.Max(e => e.name.Length);
foreach (var entry in otherConfigEntries)
{
sb.AppendFormat($" {{0, -{maxName + 1}}} = {{1}}", entry.name, entry.text);
sb.AppendLine();
}
}
sb.AppendLine("***************************************");
var writer = config.Writer;
if (writer == null)
commandContext.Console.Out.WriteLine(sb.ToString());
else
writer(sb.ToString());
}
private static IEnumerable<(string name, string text)> GetOtherConfigInfo(CommandContext commandContext, LogHeaderConfig config)
{
if (config.IncludeToolVersion)
{
var versionInfo = VersionInfo.GetVersionInfo(commandContext);
yield return ("Tool version", $"{versionInfo.Filename} {versionInfo.Version}");
}
if (config.IncludeDotNetVersion)
{
yield return (".Net version", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.Trim());
}
if (config.IncludeOsVersion)
{
yield return ("OS version", System.Runtime.InteropServices.RuntimeInformation.OSDescription.Trim());
}
if (config.IncludeMachine)
{
yield return ("Machine", Environment.MachineName);
}
if (config.IncludeUsername)
{
yield return ("Username", $"{Environment.UserDomainName}\\{Environment.UserName}");
}
if (config.AdditionalHeadersCallback != null)
{
foreach (var header in config.AdditionalHeadersCallback(commandContext))
{
yield return header;
}
}
}
private static IEnumerable<T> IncludeInherited<T>(Command c, Func<Command, IEnumerable<T>> getArgs) where T : IArgument
{
var args = getArgs(c);
if (c.Parent != null)
{
args = args.Union(getArgs(c.Parent));
}
return args;
}
private static void LogArguments(this StringBuilder sb,
string type, IEnumerable<IArgument> arguments, LogHeaderConfig config, ParseResult parseResult)
{
var argValues = SuppliedValues(config, parseResult, arguments);
if (argValues.Any())
{
sb.AppendLine($" {type}:");
var maxName = argValues.Max(v => v.name.Length);
foreach (var argValue in argValues)
{
sb.AppendFormat($" {{0, -{maxName + 1}}} = {{1}}", argValue.name, argValue.value);
sb.AppendLine();
}
}
}
private static List<(string name, string value)> SuppliedValues(
LogHeaderConfig config, ParseResult parseResult, IEnumerable<IArgument> arguments)
{
return arguments
.Where(arg => !config.SkipArgument?.Invoke(arg) ?? true)
.Select(arg =>
{
var specified = parseResult.ArgumentValues.TryGetValues(arg, out var values);
return new { arg, specified, values };
})
.Where(a => a.values != null || a.arg.DefaultValue != null)
.Select(a =>
{
var value = config.ObscureArgument?.Invoke(a.arg) ?? false
? "***"
: a.values?.ToCsv();
if (value == null && a.arg.DefaultValue != null)
{
value = $"{a.arg.DefaultValue} (default)";
}
return (a.arg.Name, value);
})
.ToList();
}
}
}