-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
192 lines (163 loc) · 7.26 KB
/
Copy pathProgram.cs
File metadata and controls
192 lines (163 loc) · 7.26 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
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Linq;
using CommandLine;
using NLog;
using Rdmp.Core.CommandLine;
using Rdmp.Core.CommandLine.DatabaseCreation;
using Rdmp.Core.CommandLine.Gui;
using Rdmp.Core.CommandLine.Options;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Logging.Listeners.NLogListeners;
using Rdmp.Core.MapsDirectlyToDatabaseTable.Versioning;
using Rdmp.Core.ReusableLibraryCode;
using Rdmp.Core.ReusableLibraryCode.Checks;
namespace Rdmp.Core;
internal class Program
{
/// <summary>
/// True if the user passed the -q switch at startup to suppress any helpful messages we might
/// show (e.g. maybe they want to pipe the results somewhere)
/// </summary>
public static bool Quiet { get; private set; }
private static int Main(string[] args)
{
try
{
var nlog = Path.Combine(AppContext.BaseDirectory, "NLog.config");
if (File.Exists(nlog))
{
LogManager.ThrowConfigExceptions = false;
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(nlog);
}
}
catch (Exception ex)
{
Console.WriteLine($"Could not load NLog.config:{ex.Message}");
}
if (args.Any(a => a.Equals("-q")) ||
args.Any(a => a.Equals("--quiet", StringComparison.CurrentCultureIgnoreCase)))
{
Quiet = true;
DisableConsoleLogging();
}
var logger = LogManager.GetCurrentClassLogger();
logger.Info($"Dotnet Version:{Environment.Version}");
logger.Info($"RDMP Version:{typeof(Catalogue).Assembly.GetName().Version}");
Startup.Startup.PreStartup();
return HandleArguments(args, logger);
}
/// <summary>
/// Disables all log targets that contain the word 'Console'. This prevents logging output
/// corrupting the screen during TUI or cli use (e.g. with -q flag).
/// </summary>
public static void DisableConsoleLogging()
{
foreach (var t in LogManager.Configuration.AllTargets.ToArray())
if (t.GetType().Name.Contains("Console", StringComparison.CurrentCultureIgnoreCase))
LogManager.Configuration.RemoveTarget(t.Name);
}
private static int HandleArguments(string[] args, Logger logger)
{
int returnCode;
try
{
returnCode =
UsefulStuff.GetParser()
.ParseArguments<
PackOptions,
ConsoleGuiOptions,
PlatformDatabaseCreationOptions,
PatchDatabaseOptions,
DleOptions,
DqeOptions,
CacheOptions,
ExtractionOptions,
ReleaseOptions,
CohortCreationOptions,
ExecuteCommandOptions>(args)
.MapResult(static (PackOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (ConsoleGuiOptions opts) =>
RdmpCommandLineBootStrapper.Run(opts, new ConsoleGuiRunner(opts)),
static (PlatformDatabaseCreationOptions opts) => Run(opts),
static (PatchDatabaseOptions opts) => Run(opts),
static (DleOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (DqeOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (CacheOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (ExtractionOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (ReleaseOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (CohortCreationOptions opts) => RdmpCommandLineBootStrapper.Run(opts),
static (ExecuteCommandOptions opts) => RdmpCommandLineBootStrapper.RunCmd(opts),
errs => HasHelpArguments(args)
? returnCode = 0
: returnCode =
RdmpCommandLineBootStrapper.HandleArgumentsWithStandardRunner(args, logger));
logger.Info($"Exiting with code {returnCode}");
return returnCode;
}
catch (Exception e)
{
logger.Error(e.Message);
logger.Info(e, "Fatal error occurred so returning -1");
return -1;
}
}
private static bool HasHelpArguments(string[] args)
{
return
args.Any(a =>
a.Equals("--help", StringComparison.CurrentCultureIgnoreCase) ||
a.Equals("--version", StringComparison.CurrentCultureIgnoreCase)
);
}
private static int Run(PlatformDatabaseCreationOptions opts)
{
var serverName = opts.ServerName;
var prefix = opts.Prefix;
Console.WriteLine($"About to create on server '{serverName}' databases with prefix '{prefix}'");
try
{
PlatformDatabaseCreation.CreatePlatformDatabases(opts);
}
catch (Exception e)
{
Console.WriteLine(e);
return -1;
}
return 0;
}
private static int Run(PatchDatabaseOptions opts)
{
opts.PopulateConnectionStringsFromYamlIfMissing(ThrowImmediatelyCheckNotifier.Quiet);
var repo = opts.GetRepositoryLocator();
if (!RdmpCommandLineBootStrapper.CheckRepo(repo)) return RdmpCommandLineBootStrapper.REPO_ERROR;
var checker = new NLogICheckNotifier(true, false);
var start = new Startup.Startup(repo);
var badTimes = false;
start.DatabaseFound += (s, e) =>
{
var db = e.Repository.DiscoveredServer.GetCurrentDatabase();
switch (e.Status)
{
case Startup.Events.RDMPPlatformDatabaseStatus.RequiresPatching:
{
var mds = new MasterDatabaseScriptExecutor(db);
mds.PatchDatabase(e.Patcher, checker, p => true, () => opts.BackupDatabase);
break;
}
case <= Startup.Events.RDMPPlatformDatabaseStatus.Broken:
checker.OnCheckPerformed(new CheckEventArgs($"Database {db} had status {e.Status}",
CheckResult.Fail));
badTimes = true;
break;
}
};
start.DoStartup(IgnoreAllErrorsCheckNotifier.Instance);
return badTimes ? -1 : 0;
}
}