NTP Analyzer  0.8.2
Analyze the operation of time servers
Program.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Globalization;
24 using System.IO;
25 using System.Threading;
26 using Ntp.Analyzer.Config;
28 using Ntp.Analyzer.Config.Node;
29 using Ntp.Common.IO;
30 
31 namespace Ntp.Analyzer.Validate.Cli
32 {
33  public static class Program
34  {
35  private const int ParameterErrorCode = 2;
36  private const int ConfigErrorCode = 1;
37  private const int SuccessCode = 0;
38  private static bool usage;
39 
40  public static int Main(string[] args)
41  {
42  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
43  bool validate = false;
44  bool show = false;
45 
46  var dec = new Decompiler();
47  var param = new OptionSet
48  {
49  {"h|?|help", v => { ShowUsage(); }},
50  {"s|show", v => { show = true; }},
51  {"d|default", v => { dec.ShowDefaultValues = true; }},
52  {"v|validate", v => { validate = true; }},
53  {"t=|tabs=", v => SetDecompilerParam(dec, '\t', v)},
54  {"w=|whitespace=", v => SetDecompilerParam(dec, ' ', v)}
55  };
56 
57  string[] rem;
58 
59  try
60  {
61  rem = param.Parse(args).ToArray();
62  }
63  catch (Exception e)
64  {
65  Console.WriteLine(e.Message);
66  return ParameterErrorCode;
67  }
68 
69  if (usage)
70  {
71  return ParameterErrorCode;
72  }
73 
74  if (rem.Length != 1)
75  {
76  Console.WriteLine("Wrong parameters.");
77  return ParameterErrorCode;
78  }
79 
80  if (!show && !validate)
81  {
82  ShowUsage();
83  return ParameterErrorCode;
84  }
85 
86  Configuration config;
87  try
88  {
89  config = LoadConfig(rem[0]);
90  }
91  catch (Exception e)
92  {
93  Console.WriteLine($"Unexpected error while loading configuration file: {e.Message}");
94  return ConfigErrorCode;
95  }
96 
97  if (config == null)
98  return ConfigErrorCode;
99 
100  if (show)
101  {
102  dec.Configuration = config;
103  Console.Write(dec.Execute());
104  }
105  else if (validate)
106  {
107  Console.WriteLine("Configuration is valid.");
108  }
109 
110  return SuccessCode;
111  }
112 
113  private static Configuration LoadConfig(string configFile)
114  {
115  if (!File.Exists(configFile))
116  {
117  Console.WriteLine($"Cannot find configuration file {configFile}");
118  return null;
119  }
120 
121  var reader = new ConfigBuilder(configFile);
122  var config = reader.Execute();
123  if (config != null)
124  return config;
125 
126  foreach (string error in reader.Errors)
127  {
128  Console.WriteLine(error);
129  }
130 
131  return null;
132  }
133 
134  private static void SetDecompilerParam(Decompiler decompiler, char indent, string count)
135  {
136  int parsed;
137  decompiler.IndentChar = indent;
138  decompiler.IndentSize = int.TryParse(count, out parsed) ? parsed : 1;
139  }
140 
141  private static void ShowUsage()
142  {
143  Console.WriteLine("NTP Analyzer validator tool v0.8.2");
144  Console.WriteLine("Usage: ntpav [-s|-v] [-d] [-t n|-w n] configuration");
145  Console.WriteLine(" -s Show parsed configuration");
146  Console.WriteLine(" -v Check if configuration is valid");
147  Console.WriteLine(" -d Show all default value");
148  Console.WriteLine(" -t Indent with n tabulators");
149  Console.WriteLine(" -w Indent with n whitespaces");
150  usage = true;
151  }
152  }
153 }
char IndentChar
Gets or sets the indent char.
Definition: Decompiler.cs:64
static void SetDecompilerParam(Decompiler decompiler, char indent, string count)
Definition: Program.cs:134
static Configuration LoadConfig(string configFile)
Definition: Program.cs:113
var e
Definition: bootstrap.min.js:6
int IndentSize
Gets or sets the size of the indent.
Definition: Decompiler.cs:70
static int Main(string[] args)
Definition: Program.cs:40