-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (134 loc) · 5.5 KB
/
Program.cs
File metadata and controls
152 lines (134 loc) · 5.5 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
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using Common;
using log4net;
using ServiceStack;
namespace MultiFile.Configurator
{
public class Program
{
private static ILog _log;
public static void Main(string[] args)
{
Environment.ExitCode = 1;
try
{
ConfigureLogging();
var program = new Program();
program.ParseArgs(args);
program.Run();
Environment.ExitCode = 0;
}
catch (WebServiceException exception)
{
_log.Error($"API: ({exception.StatusCode}) {string.Join(" ", exception.StatusDescription, exception.ErrorCode)}: {string.Join(" ", exception.Message, exception.ErrorMessage)}");
}
catch (ExpectedException exception)
{
_log.Error(exception.Message);
}
catch (Exception exception)
{
_log.Error("Unhandled exception", exception);
}
}
private static void ConfigureLogging()
{
using (var stream = new MemoryStream(LoadEmbeddedResource("log4net.config")))
using (var reader = new StreamReader(stream))
{
var xml = new XmlDocument();
xml.LoadXml(reader.ReadToEnd());
log4net.Config.XmlConfigurator.Configure(xml.DocumentElement);
_log = LogManager.GetLogger(GetMainProgramType());
}
}
private static byte[] LoadEmbeddedResource(string path)
{
var resourceName = $"{GetMainProgramType().Namespace}.{path}";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new ExpectedException($"Can't load '{resourceName}' as embedded resource.");
return stream.ReadFully();
}
}
private static Type GetMainProgramType()
{
// ReSharper disable once PossibleNullReferenceException
return MethodBase.GetCurrentMethod().DeclaringType;
}
private Context Context { get; } = new Context();
private void ParseArgs(string[] args)
{
var options = new[]
{
new CommandLineOption { Description = "AQTS app server credentials" },
new CommandLineOption
{
Key = nameof(Context.Server),
Setter = value => Context.Server = value,
Getter = () => Context.Server,
Description = "AQTS server"
},
new CommandLineOption
{
Key = nameof(Context.Username),
Setter = value => Context.Username = value,
Getter = () => Context.Username,
Description = "AQTS username"
},
new CommandLineOption
{
Key = nameof(Context.Password),
Setter = value => Context.Password = value,
Getter = () => Context.Password,
Description = "AQTS password"
},
new CommandLineOption(), new CommandLineOption { Description = "Generator settings" },
new CommandLineOption
{
Key = nameof(Context.IncludeDisabledPluginSettings),
Setter = value => Context.IncludeDisabledPluginSettings = bool.Parse(value),
Getter = () => $"{Context.IncludeDisabledPluginSettings}",
Description = "If true, include the settings for currently disabled plugins in the generated configuration."
},
new CommandLineOption
{
Key = nameof(Context.SaveOnServer),
Setter = value => Context.SaveOnServer = bool.Parse(value),
Getter = () => $"{Context.SaveOnServer}",
Description = "If true, save the generated configuration as a MultiFile plugin setting on the server."
},
new CommandLineOption
{
Key = nameof(Context.GenerateForExternalUse),
Setter = value => Context.GenerateForExternalUse = bool.Parse(value),
Getter = () => $"{Context.GenerateForExternalUse}",
Description = "If true, generate a config for external use, in the PluginTester or FieldVisitHotFolderService."
},
new CommandLineOption
{
Key = nameof(Context.JsonPath),
Setter = value => Context.JsonPath = value,
Getter = () => Context.JsonPath,
Description = "If set, save the generated configuration to this file."
},
};
var usageMessage = CommandLineUsage.ComposeUsageText(
"Create a MultiFile configuration setting from an AQTS app server's current configuration.", options);
var optionResolver = new CommandLineOptionResolver();
optionResolver.Resolve(args, options, usageMessage);
}
private void Run()
{
new Generator
{
Context = Context
}
.Run();
}
}
}