|
12 | 12 |
|
13 | 13 | namespace Microsoft.Git.CredentialManager
|
14 | 14 | {
|
15 |
| - public static class Application |
| 15 | + public class Application : IDisposable |
16 | 16 | {
|
17 |
| - private static readonly IHostProviderRegistry HostProviderRegistry = new HostProviderRegistry(); |
| 17 | + private readonly ICommandContext _context; |
| 18 | + private readonly IHostProviderRegistry _providerRegistry = new HostProviderRegistry(); |
18 | 19 |
|
19 |
| - private static readonly ICollection<CommandBase> Commands = new CommandBase[] |
20 |
| - { |
21 |
| - new EraseCommand(HostProviderRegistry), |
22 |
| - new GetCommand(HostProviderRegistry), |
23 |
| - new StoreCommand(HostProviderRegistry), |
24 |
| - new VersionCommand(), |
25 |
| - new HelpCommand(), |
26 |
| - }; |
27 |
| - |
28 |
| - public static async Task<int> RunAsync(string[] args) |
| 20 | + private TextWriter _traceFileWriter; |
| 21 | + |
| 22 | + public Application(ICommandContext context) |
29 | 23 | {
|
30 |
| - var context = new CommandContext(); |
| 24 | + EnsureArgument.NotNull(context, nameof(context)); |
| 25 | + |
| 26 | + _context = context; |
| 27 | + } |
31 | 28 |
|
| 29 | + public async Task<int> RunAsync(string[] args) |
| 30 | + { |
32 | 31 | // Launch debugger
|
33 |
| - if (context.IsEnvironmentVariableTruthy(Constants.EnvironmentVariables.GcmDebug, false)) |
| 32 | + if (_context.IsEnvironmentVariableTruthy(Constants.EnvironmentVariables.GcmDebug, false)) |
34 | 33 | {
|
35 |
| - context.StdError.WriteLine("Waiting for debugger to be attached..."); |
| 34 | + _context.StdError.WriteLine("Waiting for debugger to be attached..."); |
36 | 35 | PlatformUtils.WaitForDebuggerAttached();
|
37 | 36 |
|
38 | 37 | // Now the debugger is attached, break!
|
39 | 38 | Debugger.Break();
|
40 | 39 | }
|
41 | 40 |
|
42 | 41 | // Enable tracing
|
43 |
| - if (context.TryGetEnvironmentVariable(Constants.EnvironmentVariables.GcmTrace, out string traceEnvar)) |
| 42 | + if (_context.TryGetEnvironmentVariable(Constants.EnvironmentVariables.GcmTrace, out string traceEnvar)) |
44 | 43 | {
|
45 | 44 | if (traceEnvar.IsTruthy()) // Trace to stderr
|
46 | 45 | {
|
47 |
| - context.Trace.AddListener(context.StdError); |
| 46 | + _context.Trace.AddListener(_context.StdError); |
48 | 47 | }
|
49 | 48 | else if (Path.IsPathRooted(traceEnvar) && // Trace to a file
|
50 |
| - TryCreateTextWriter(context, traceEnvar, out var fileWriter)) |
| 49 | + TryCreateTextWriter(_context, traceEnvar, out var fileWriter)) |
51 | 50 | {
|
52 |
| - context.Trace.AddListener(fileWriter); |
| 51 | + _traceFileWriter = fileWriter; |
| 52 | + _context.Trace.AddListener(fileWriter); |
53 | 53 | }
|
54 | 54 | else
|
55 | 55 | {
|
56 |
| - context.StdError.WriteLine($"warning: cannot write trace output to {traceEnvar}"); |
| 56 | + _context.StdError.WriteLine($"warning: cannot write trace output to {traceEnvar}"); |
57 | 57 | }
|
58 | 58 | }
|
59 | 59 |
|
60 | 60 | // Enable sensitive tracing and show warning
|
61 |
| - if (context.IsEnvironmentVariableTruthy(Constants.EnvironmentVariables.GcmTraceSecrets, false)) |
| 61 | + if (_context.IsEnvironmentVariableTruthy(Constants.EnvironmentVariables.GcmTraceSecrets, false)) |
62 | 62 | {
|
63 |
| - context.Trace.EnableSecretTracing = true; |
64 |
| - context.StdError.WriteLine( |
65 |
| - "warning: Secret tracing is enabled. Trace output may contain sensitive information."); |
| 63 | + _context.Trace.EnableSecretTracing = true; |
| 64 | + _context.StdError.WriteLine("Secret tracing is enabled. Trace output may contain sensitive information."); |
66 | 65 | }
|
67 | 66 |
|
68 | 67 | // Register all supported host providers
|
69 |
| - HostProviderRegistry.Register( |
70 |
| - new AzureReposHostProvider(context), |
71 |
| - new GenericHostProvider(context) |
| 68 | + _providerRegistry.Register( |
| 69 | + new AzureReposHostProvider(_context), |
| 70 | + new GenericHostProvider(_context) |
72 | 71 | );
|
73 | 72 |
|
| 73 | + // Construct all supported commands |
| 74 | + var commands = new CommandBase[] |
| 75 | + { |
| 76 | + new EraseCommand(_providerRegistry), |
| 77 | + new GetCommand(_providerRegistry), |
| 78 | + new StoreCommand(_providerRegistry), |
| 79 | + new VersionCommand(), |
| 80 | + new HelpCommand(), |
| 81 | + }; |
| 82 | + |
74 | 83 | // Trace the current version and program arguments
|
75 |
| - context.Trace.WriteLine($"{Constants.GetProgramHeader()} '{string.Join(" ", args)}'"); |
| 84 | + _context.Trace.WriteLine($"{Constants.GetProgramHeader()} '{string.Join(" ", args)}'"); |
76 | 85 |
|
77 | 86 | if (args.Length == 0)
|
78 | 87 | {
|
79 |
| - context.StdError.WriteLine("Missing command."); |
80 |
| - HelpCommand.PrintUsage(context.StdError); |
| 88 | + _context.StdError.WriteLine("Missing command."); |
| 89 | + HelpCommand.PrintUsage(_context.StdError); |
81 | 90 | return -1;
|
82 | 91 | }
|
83 | 92 |
|
84 |
| - foreach (var cmd in Commands) |
| 93 | + foreach (var cmd in commands) |
85 | 94 | {
|
86 | 95 | if (cmd.CanExecute(args))
|
87 | 96 | {
|
88 | 97 | try
|
89 | 98 | {
|
90 |
| - await cmd.ExecuteAsync(context, args); |
| 99 | + await cmd.ExecuteAsync(_context, args); |
91 | 100 | return 0;
|
92 | 101 | }
|
93 | 102 | catch (Exception e)
|
94 | 103 | {
|
95 | 104 | if (e is AggregateException ae)
|
96 | 105 | {
|
97 |
| - ae.Handle(x => WriteException(context, x)); |
| 106 | + ae.Handle(x => WriteException(_context, x)); |
98 | 107 | }
|
99 | 108 | else
|
100 | 109 | {
|
101 |
| - WriteException(context, e); |
| 110 | + WriteException(_context, e); |
102 | 111 | }
|
103 | 112 |
|
104 | 113 | return -1;
|
105 | 114 | }
|
106 | 115 | }
|
107 | 116 | }
|
108 | 117 |
|
109 |
| - context.StdError.WriteLine("Unrecognized command '{0}'.", args[0]); |
110 |
| - HelpCommand.PrintUsage(context.StdError); |
| 118 | + _context.StdError.WriteLine("Unrecognized command '{0}'.", args[0]); |
| 119 | + HelpCommand.PrintUsage(_context.StdError); |
111 | 120 | return -1;
|
112 | 121 | }
|
113 | 122 |
|
| 123 | + #region Helpers |
| 124 | + |
114 | 125 | private static bool WriteException(ICommandContext context, Exception e)
|
115 | 126 | {
|
116 | 127 | context.StdError.WriteLine("fatal: {0}", e.Message);
|
@@ -140,5 +151,17 @@ private static bool TryCreateTextWriter(ICommandContext context, string path, ou
|
140 | 151 |
|
141 | 152 | return writer != null;
|
142 | 153 | }
|
| 154 | + |
| 155 | + #endregion |
| 156 | + |
| 157 | + #region IDisposable |
| 158 | + |
| 159 | + public void Dispose() |
| 160 | + { |
| 161 | + _providerRegistry.Dispose(); |
| 162 | + _traceFileWriter?.Dispose(); |
| 163 | + } |
| 164 | + |
| 165 | + #endregion |
143 | 166 | }
|
144 | 167 | }
|
0 commit comments