-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (153 loc) · 6.44 KB
/
Copy pathProgram.cs
File metadata and controls
183 lines (153 loc) · 6.44 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
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using CommandLine;
using CommandLine.Text;
using Duke;
using Duke.Matchers;
using NLog;
namespace DukeConsoleApplication
{
internal class Program
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private static void Main(string[] args)
{
_logger.Info("* Application Start *");
var options = new Options();
var parser = new CommandLineParser();
if (parser.ParseArguments(args, options))
{
// write the application header
Console.WriteLine(GetApplicationHeader());
// display all of the options gathered from the commandline args...
DisplayExecutionOptions(options);
if (!HasValidConfigFile(options.ConfigFile))
{
string errMessage =
String.Format(
"The configuration file '{0}' is NOT valid.\r\n Please check the file path.",
options.ConfigFile);
DisplayErrorMessageAndExit(errMessage, ExitCode.InvalidConfigFile);
}
if (!HasValidXml(options.ConfigFile))
{
string errMessage =
String.Format(
"The XML in the configuration file '{0}' is NOT valid.\r\n Please check the file contents.",
options.ConfigFile);
DisplayErrorMessageAndExit(errMessage, ExitCode.InvalidConfigFileXml);
}
// get the intial options
int count = 0;
// load the configuration
var config = ConfigLoader.Load(options.ConfigFile);
// spin up the Processor
var proc = new Processor(config);
proc.AddMatchListener(new PrintMatchListener(true, true, true, false));
// deduplicate the items
proc.Deduplicate();
// close out the processor
proc.Close();
}
else
{
_logger.Debug("Application called without proper arguments.");
Console.WriteLine(options.GetUsage());
//Console.WriteLine("Error reading commandline arguments!");
}
_logger.Info("Application successfully exited");
Environment.Exit((int) ExitCode.Success);
}
private static string GetApplicationHeader()
{
var sb = new StringBuilder();
const string padding = "==============================";
sb.AppendLine(padding);
var help = new HelpText
{
Heading = new HeadingInfo("Duke Console Application", "1.0"),
Copyright = new CopyrightInfo("Ken Taylor", 2012),
AdditionalNewLineAfterOption = false,
AddDashesToOption = false
};
sb.AppendLine(help);
sb.AppendLine(padding);
return sb.ToString();
}
private static bool HasValidConfigFile(string pathToConfigFile)
{
_logger.Debug("Checking {0} to see if it is a valid config file.", pathToConfigFile);
string directoryPath = Path.GetDirectoryName(pathToConfigFile);
string fileName = Path.GetFileName(pathToConfigFile);
if (directoryPath != null && !Directory.Exists(directoryPath))
{
directoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
if (fileName != null && (directoryPath != null && !File.Exists(Path.Combine(directoryPath, fileName))))
return false;
string extension = Path.GetExtension(fileName);
if (extension != null && extension.ToLower() == ".xml")
{
return true;
}
return false;
}
private static bool HasValidXml(string pathToConfigFile)
{
_logger.Debug("Checking {0} for valid XML", pathToConfigFile);
try
{
if (String.IsNullOrEmpty(pathToConfigFile))
return false;
var xmlDoc = new XmlDocument();
xmlDoc.Load(pathToConfigFile);
return true;
}
catch (XmlException)
{
return false;
}
}
private static void DisplayExecutionOptions(Options options)
{
_logger.Debug(options.ToString);
Console.WriteLine("Execution Options:");
Console.WriteLine(String.Format("Configuration File = {0}", options.ConfigFile));
// consume Options type properties
Console.WriteLine(String.Format("Show ShowProgress = {0}", options.ShowProgress));
Console.WriteLine(String.Format("Show Matches = {0}", options.ShowMatches));
if (!String.IsNullOrEmpty(options.LinkFile))
{
string linkFile = options.LinkFile;
Console.WriteLine(String.Format("Link File = {0}", linkFile));
}
Console.WriteLine(String.Format("IsInteractive = {0}", options.IsInteractive));
if (!String.IsNullOrEmpty(options.TestFile))
{
string statsFile = options.TestFile;
Console.WriteLine(String.Format("Test File = {0}", statsFile));
}
Console.WriteLine(String.Format("Verbose = {0}", options.ShowVerbose));
Console.WriteLine(String.Format("No Reindex = {0}", options.IsNoReindex));
Console.WriteLine(String.Format("Batch Size = {0}", options.BatchSize));
}
private static void DisplayErrorMessageAndExit(string errorMessage, ExitCode errorCode)
{
Console.WriteLine(Environment.NewLine);
Console.WriteLine(String.Format("* ERROR: {0}", errorMessage));
_logger.Error(errorMessage);
Environment.Exit((int) errorCode);
}
#region Nested type: ExitCode
private enum ExitCode
{
Success = 0,
InvalidConfigFile = 1,
InvalidConfigFileXml = 2
}
#endregion
}
}