Skip to content

Commit 466b84d

Browse files
Adds the --discover option. Closes #1038 (#1062)
1 parent a603656 commit 466b84d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

dev-proxy/CommandHandlers/ProxyCommandHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ private void ParseOptions(InvocationContext context)
145145
{
146146
Configuration.TimeoutSeconds = timeout.Value;
147147
}
148+
var isDiscover = context.ParseResult.GetValueForOption<bool?>(ProxyHost.DiscoverOptionName, _options);
149+
if (isDiscover is not null)
150+
{
151+
Configuration.Record = true;
152+
}
148153
}
149154

150155
private async Task CheckForNewVersionAsync()

dev-proxy/PluginLoader.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class PluginLoaderResult(ISet<UrlToWatch> urlsToWatch, IEnumerable<IPro
1414
public IEnumerable<IProxyPlugin> ProxyPlugins { get; } = proxyPlugins ?? throw new ArgumentNullException(nameof(proxyPlugins));
1515
}
1616

17-
internal class PluginLoader(ILogger logger, ILoggerFactory loggerFactory)
17+
internal class PluginLoader(bool isDiscover, ILogger logger, ILoggerFactory loggerFactory)
1818
{
1919
private PluginConfig? _pluginConfig;
2020
private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger));
@@ -126,7 +126,25 @@ private PluginConfig PluginConfig
126126
if (_pluginConfig == null)
127127
{
128128
_pluginConfig = new PluginConfig();
129-
Configuration.Bind(_pluginConfig);
129+
130+
if (isDiscover)
131+
{
132+
_pluginConfig.Plugins.Add(new PluginReference
133+
{
134+
Name = "UrlDiscoveryPlugin",
135+
PluginPath = "~appFolder/plugins/dev-proxy-plugins.dll"
136+
});
137+
_pluginConfig.Plugins.Add(new PluginReference
138+
{
139+
Name = "PlainTextReporter",
140+
PluginPath = "~appFolder/plugins/dev-proxy-plugins.dll"
141+
});
142+
_pluginConfig.UrlsToWatch.Add("https://*/*");
143+
}
144+
else
145+
{
146+
Configuration.Bind(_pluginConfig);
147+
}
130148

131149
if (ProxyHost.UrlsToWatch is not null && ProxyHost.UrlsToWatch.Any())
132150
{

dev-proxy/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@
5353
var hasGlobalOption = args.Any(arg => globalOptions.Contains(arg));
5454
var hasHelpOption = args.Any(arg => helpOptions.Contains(arg));
5555

56+
var isDiscover = args.Contains("--discover", StringComparer.OrdinalIgnoreCase);
57+
if (isDiscover)
58+
{
59+
logger.LogWarning("Dev Proxy is running in URL discovery mode. Configured plugins and URLs to watch will be ignored.");
60+
}
61+
5662
// load plugins to get their options and commands
57-
var pluginLoader = new PluginLoader(logger, loggerFactory);
63+
var pluginLoader = new PluginLoader(isDiscover, logger, loggerFactory);
5864
PluginLoaderResult loaderResults = await pluginLoader.LoadPluginsAsync(pluginEvents, context);
5965
var options = loaderResults.ProxyPlugins
6066
.SelectMany(p => p.GetOptions())

dev-proxy/ProxyHost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ internal class ProxyHost
3737
private static Option<IEnumerable<string>?>? _urlsToWatchOption;
3838
internal static readonly string TimeoutOptionName = "--timeout";
3939
private readonly Option<long?> _timeoutOption;
40+
internal static readonly string DiscoverOptionName = "--discover";
41+
private readonly Option<bool?> _discoverOption;
4042

4143
private static bool _configFileResolved = false;
4244
private static string _configFile = "devproxyrc.json";
@@ -231,6 +233,7 @@ public ProxyHost()
231233
};
232234

233235
_noFirstRunOption = new Option<bool?>(NoFirstRunOptionName, "Skip the first run experience");
236+
_discoverOption = new Option<bool?>(DiscoverOptionName, "Run Dev Proxy in discovery mode");
234237

235238
_asSystemProxyOption = new Option<bool?>(AsSystemProxyOptionName, "Set Dev Proxy as the system proxy");
236239
_asSystemProxyOption.AddValidator(input =>
@@ -314,7 +317,8 @@ public RootCommand GetRootCommand(ILogger logger)
314317
// _urlsToWatchOption is set while initialize the Program
315318
// As such, it's always set here
316319
_urlsToWatchOption!,
317-
_timeoutOption
320+
_timeoutOption,
321+
_discoverOption
318322
};
319323
command.Description = "Dev Proxy is a command line tool for testing Microsoft Graph, SharePoint Online and any other HTTP APIs.";
320324

@@ -487,6 +491,7 @@ public RootCommand GetRootCommand(ILogger logger)
487491
_asSystemProxyOption,
488492
_installCertOption,
489493
_timeoutOption,
494+
_discoverOption,
490495
.. optionsFromPlugins,
491496
],
492497
urlsToWatch,

0 commit comments

Comments
 (0)