This repository has been archived by the owner on Feb 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Program.cs
142 lines (119 loc) · 4.36 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
using Axinom.Toolkit;
using Mono.Options;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace DockerExporter
{
internal sealed class Program
{
private readonly LogSource _log = Log.Default;
private readonly FilteringLogListener _filteringLogListener;
private readonly ExporterLogic _logic = new ExporterLogic();
private void Run(string[] args)
{
// We signal this to shut down the service.
var cancel = new CancellationTokenSource();
try
{
if (!ParseArguments(args))
{
Environment.ExitCode = -1;
return;
}
_log.Info(GetVersionString());
// Control+C will gracefully shut us down.
Console.CancelKeyPress += (s, e) =>
{
_log.Info("Canceling execution due to received signal.");
e.Cancel = true;
cancel.Cancel();
};
_logic.RunAsync(cancel.Token).WaitAndUnwrapExceptions();
_log.Info("Application logic execution has completed.");
}
catch (OperationCanceledException)
{
if (cancel.IsCancellationRequested)
{
// We really were cancelled. That's fine.
}
else
{
_log.Error("Unexpected cancellation/timeout halted execution.");
Environment.ExitCode = -1;
}
}
catch (Exception ex)
{
_log.Error(Helpers.Debug.GetAllExceptionMessages(ex));
Environment.ExitCode = -1;
}
}
private bool ParseArguments(string[] args)
{
var showHelp = false;
var verbose = false;
var debugger = false;
var options = new OptionSet
{
GetVersionString(),
"",
"General",
{ "h|?|help", "Displays usage instructions.", val => showHelp = val != null },
{ "docker-url=", $"URL to use for accessing Docker. Defaults to {_logic.DockerUrl}", val => _logic.DockerUrl = val },
"",
"Diagnostics",
{ "verbose", "Displays extensive diagnostic information.", val => verbose = val != null },
{ "debugger", "Requests a debugger to be attached before execution starts.", val => debugger = val != null, true },
};
List<string> remainingOptions;
try
{
remainingOptions = options.Parse(args);
if (showHelp)
{
options.WriteOptionDescriptions(Console.Out);
return false;
}
if (verbose)
_filteringLogListener.MinimumSeverity = LogEntrySeverity.Debug;
}
catch (OptionException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("For usage instructions, use the --help command line parameter.");
return false;
}
if (remainingOptions.Count != 0)
{
Console.WriteLine("Unknown command line parameters: {0}", string.Join(" ", remainingOptions.ToArray()));
Console.WriteLine("For usage instructions, use the --help command line parameter.");
return false;
}
if (debugger)
Debugger.Launch();
return true;
}
private string GetVersionString()
{
return $"{typeof(Program).Namespace} v{Constants.VersionString}";
}
private Program()
{
// We default to displaying Info or higher but allow this to be reconfiured later, if the user wishes.
_filteringLogListener = new FilteringLogListener(new ConsoleLogListener())
{
#if !DEBUG
MinimumSeverity = LogEntrySeverity.Info
#endif
};
Log.Default.RegisterListener(_filteringLogListener);
}
private static void Main(string[] args)
{
new Program().Run(args);
}
}
}