Skip to content

Commit bd52987

Browse files
Allows running proxy without plugins or URLs to watch. Closes #1042 (#1071)
1 parent cb4fa72 commit bd52987

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

dev-proxy/CommandHandlers/ProxyCommandHandler.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public int Invoke(InvocationContext context)
3232

3333
public async Task<int> InvokeAsync(InvocationContext context)
3434
{
35+
if (Configuration.Plugins.Count == 0)
36+
{
37+
_logger.LogWarning("You haven't configured any plugins. Please add plugins to your configuration file. Dev Proxy will exit.");
38+
return 1;
39+
}
40+
if (Configuration.UrlsToWatch.Count == 0)
41+
{
42+
_logger.LogWarning("You haven't configured any URLs to watch. Please add URLs to your configuration file or use the --urls-to-watch option. Dev Proxy will exit.");
43+
return 1;
44+
}
45+
3546
ParseOptions(context);
3647
_pluginEvents.RaiseOptionsLoaded(new OptionsLoadedArgs(context, _options));
3748
await CheckForNewVersionAsync();
@@ -174,6 +185,20 @@ private async Task CheckForNewVersionAsync()
174185
var configObject = new ProxyConfiguration();
175186
configuration.Bind(configObject);
176187

188+
// Custom binding for internal properties, because it's not happening
189+
// automatically
190+
var pluginsSection = configuration.GetSection("Plugins");
191+
if (pluginsSection.Exists())
192+
{
193+
configObject.Plugins = pluginsSection.Get<List<PluginReference>>() ?? [];
194+
}
195+
196+
var urlsSection = configuration.GetSection("UrlsToWatch");
197+
if (urlsSection.Exists())
198+
{
199+
configObject.UrlsToWatch = urlsSection.Get<List<string>>() ?? [];
200+
}
201+
177202
return configObject;
178203
});
179204
}

dev-proxy/PluginLoader.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,22 @@ public async Task<PluginLoaderResult> LoadPluginsAsync(IPluginEvents pluginEvent
6363
(pluginUrls != null && pluginUrls.Any()) ? pluginUrls : defaultUrlsToWatch,
6464
h.ConfigSection is null ? null : Configuration.GetSection(h.ConfigSection)
6565
);
66+
if (plugin is null)
67+
{
68+
_logger?.LogError("Plugin {pluginName} could not be created. Skipping...", h.Name);
69+
continue;
70+
}
6671
_logger?.LogDebug("Registering plugin {pluginName}...", plugin.Name);
6772
await plugin.RegisterAsync();
6873
_logger?.LogDebug("Plugin {pluginName} registered.", plugin.Name);
6974
plugins.Add(plugin);
7075
}
7176
}
7277

73-
return plugins.Count > 0
74-
? new PluginLoaderResult(globallyWatchedUrls.ToHashSet(), plugins)
75-
: throw new InvalidDataException("No plugins were loaded");
78+
return new PluginLoaderResult(globallyWatchedUrls.ToHashSet(), plugins);
7679
}
7780

78-
private IProxyPlugin CreatePlugin(
81+
private IProxyPlugin? CreatePlugin(
7982
Assembly assembly,
8083
PluginReference pluginReference,
8184
IPluginEvents pluginEvents,
@@ -84,6 +87,12 @@ private IProxyPlugin CreatePlugin(
8487
IConfigurationSection? configSection = null
8588
)
8689
{
90+
if (urlsToWatch is null || urlsToWatch.Count == 0)
91+
{
92+
_logger.LogError("Plugin {pluginName} must have at least one URL to watch. Please add a URL to watch in the configuration file or use the --urls-to-watch option.", pluginReference.Name);
93+
return null;
94+
}
95+
8796
foreach (Type type in assembly.GetTypes())
8897
{
8998
if (type.Name == pluginReference.Name &&
@@ -151,10 +160,6 @@ private PluginConfig PluginConfig
151160
_pluginConfig.UrlsToWatch = ProxyHost.UrlsToWatch.ToList();
152161
}
153162
}
154-
if (_pluginConfig == null || _pluginConfig.Plugins.Count == 0)
155-
{
156-
throw new InvalidDataException("The configuration must contain at least one plugin");
157-
}
158163
return _pluginConfig;
159164
}
160165
}

dev-proxy/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@
8585
{
8686
// we don't need to init plugins if the user is using a global option or
8787
// using a subcommand, so we can exit early
88-
await rootCommand.InvokeAsync(args);
88+
var exitCode = await rootCommand.InvokeAsync(args);
8989
// required to output all messages before closing the program
9090
loggerFactory.Dispose();
91+
Environment.Exit(exitCode);
9192
return;
9293
}
9394

@@ -121,6 +122,7 @@
121122
Console.Error.WriteLine("Unknown option(s): {0}", string.Join(" ", incomingOptions));
122123
Console.Error.WriteLine("TIP: Use --help view available options");
123124
Console.Error.WriteLine("TIP: Are you missing a plugin? See: https://aka.ms/devproxy/plugins");
125+
Environment.Exit(1);
124126
}
125127
else
126128
{
@@ -131,5 +133,7 @@
131133
}
132134

133135
rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, [.. options], loaderResults.UrlsToWatch, logger);
134-
await rootCommand.InvokeAsync(args);
136+
var exitCode = await rootCommand.InvokeAsync(args);
137+
loggerFactory.Dispose();
138+
Environment.Exit(exitCode);
135139
}

dev-proxy/ProxyConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public class ProxyConfiguration : IProxyConfiguration
3333
public bool NoFirstRun { get; set; } = false;
3434
[JsonConverter(typeof(JsonStringEnumConverter))]
3535
public ReleaseType NewVersionNotification { get; set; } = ReleaseType.Stable;
36+
internal List<PluginReference> Plugins { get; set; } = [];
3637
public int Port { get; set; } = 8000;
3738
public bool Record { get; set; } = false;
3839
public bool ShowSkipMessages { get; set; } = true;
3940
public bool ShowTimestamps { get; set; } = true;
4041
public long? TimeoutSeconds { get; set; }
42+
internal List<string> UrlsToWatch { get; set; } = [];
4143
public bool ValidateSchemas { get; set; } = true;
4244
public IEnumerable<int> WatchPids { get; set; } = new List<int>();
4345
public IEnumerable<string> WatchProcessNames { get; set; } = [];

0 commit comments

Comments
 (0)