Skip to content

Commit 6e055d8

Browse files
Adds support for specifying the config file name. Closes #228 (#229)
Adds support for specifying the config file name. Closes #228 (#229)
1 parent 6dfbea5 commit 6e055d8

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// If you have changed target frameworks, make sure to update the program path.
1313
"program": "${workspaceFolder}/msgraph-developer-proxy/bin/Debug/net7.0/mgdp.dll",
1414
"args": [],
15-
"cwd": "${workspaceFolder}/msgraph-developer-proxy",
15+
"cwd": "${workspaceFolder}/msgraph-developer-proxy/bin/Debug/net7.0",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
1717
"console": "integratedTerminal",
1818
"stopAtEntry": false,

msgraph-developer-proxy/PluginLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private PluginConfig PluginConfig {
9292

9393
private readonly Lazy<IConfigurationRoot> ConfigurationFactory = new(() =>
9494
new ConfigurationBuilder()
95-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
95+
.AddJsonFile(ProxyHost.ConfigFile, optional: true, reloadOnChange: true)
9696
.Build()
9797
);
9898
}

msgraph-developer-proxy/ProxyCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task<int> InvokeAsync(InvocationContext context) {
9191
private static readonly Lazy<ProxyConfiguration> ConfigurationFactory = new(() => {
9292
var builder = new ConfigurationBuilder();
9393
var configuration = builder
94-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
94+
.AddJsonFile(ProxyHost.ConfigFile, optional: true, reloadOnChange: true)
9595
.Build();
9696
var configObject = new ProxyConfiguration();
9797
configuration.Bind(configObject);

msgraph-developer-proxy/ProxyHost.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,43 @@ internal class ProxyHost {
1111
private Option<int?> _portOption;
1212
private Option<LogLevel?> _logLevelOption;
1313
private Option<bool?> _recordOption;
14+
private static Option<string?>? _configFileOption;
15+
16+
private static bool _configFileResolved = false;
17+
private static string _configFile = "appsettings.json";
18+
public static string ConfigFile {
19+
get {
20+
if (_configFileResolved) {
21+
return _configFile;
22+
}
23+
24+
if (_configFileOption is null) {
25+
_configFileOption = new Option<string?>("--config-file", "The path to the configuration file");
26+
_configFileOption.AddAlias("-c");
27+
_configFileOption.ArgumentHelpName = "configFile";
28+
_configFileOption.AddValidator(input => {
29+
var filePath = input.Tokens.First().Value;
30+
if (String.IsNullOrEmpty(filePath)) {
31+
return;
32+
}
33+
34+
if (!File.Exists(filePath)) {
35+
input.ErrorMessage = $"File {filePath} does not exist";
36+
}
37+
});
38+
}
39+
40+
var result = _configFileOption.Parse(Environment.GetCommandLineArgs());
41+
var configFile = result.GetValueForOption<string?>(_configFileOption);
42+
if (configFile is not null) {
43+
_configFile = configFile;
44+
}
45+
46+
_configFileResolved = true;
47+
48+
return _configFile;
49+
}
50+
}
1451

1552
public ProxyHost() {
1653
_portOption = new Option<int?>("--port", "The port for the proxy server to listen on");
@@ -32,7 +69,10 @@ public RootCommand GetRootCommand() {
3269
var command = new RootCommand {
3370
_portOption,
3471
_logLevelOption,
35-
_recordOption
72+
_recordOption,
73+
// _configFileOption is set during the call to load
74+
// `ProxyCommandHandler.Configuration`. As such, it's always set here
75+
_configFileOption!
3676
};
3777
command.Description = "Microsoft Graph Developer Proxy is a command line tool that simulates real world behaviors of Microsoft Graph and other APIs, locally.";
3878

0 commit comments

Comments
 (0)