| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | | 9 | | |
| | | 10 | | namespace Azure.Messaging.EventHubs.Samples |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// |
| | | 14 | | /// The main entry point for executing the samples. |
| | | 15 | | /// |
| | | 16 | | /// To run a sample, after a successful build, go to the following folder: |
| | | 17 | | /// |
| | | 18 | | /// "artifacts\bin\Azure.Messaging.EventHubs.Samples\Debug\" |
| | | 19 | | /// |
| | | 20 | | /// If using .NET Core, move to the corresponding sub-folder and launch |
| | | 21 | | /// one of the following commands from command line: |
| | | 22 | | /// |
| | | 23 | | /// dotnet Azure.Messaging.EventHubs.Samples.dll ` |
| | | 24 | | /// --ConnectionString "<< YOUR_CONNECTION_STRING >>" ` |
| | | 25 | | /// --EventHub "<< YOUR_EVENT_HUB_NAME >>" |
| | | 26 | | /// |
| | | 27 | | /// To run an identity sample extra parameters will be needed: |
| | | 28 | | /// |
| | | 29 | | /// dotnet Azure.Messaging.EventHubs.Samples.dll ` |
| | | 30 | | /// --FullyQualifiedNamespace "{yournamespace}.servicebus.windows.net" ` |
| | | 31 | | /// --EventHub "<< YOUR_EVENT_HUB_NAME >>" ` |
| | | 32 | | /// --TenantId " << YOUR_TENANT_ID >> " ` |
| | | 33 | | /// --ClientId "<< YOUR_CLIENT_ID >>" ` |
| | | 34 | | /// --Secret "<< YOUR_SECRET_ID >>" |
| | | 35 | | /// |
| | | 36 | | /// </summary> |
| | | 37 | | /// |
| | | 38 | | public static class Program |
| | | 39 | | { |
| | | 40 | | /// <summary> |
| | | 41 | | /// A set of controlling options displayed to the user on console. |
| | | 42 | | /// </summary> |
| | | 43 | | /// |
| | 0 | 44 | | private static readonly string[] ExtraOptionsForSamples = new[] |
| | 0 | 45 | | { |
| | 0 | 46 | | "Explore Identity Samples" |
| | 0 | 47 | | }; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// A set of controlling option displayed to the user if they choose to explore identity samples. |
| | | 51 | | /// </summary> |
| | | 52 | | /// |
| | 0 | 53 | | private static readonly string[] ExtraOptionsForIdentitySamples = new[] |
| | 0 | 54 | | { |
| | 0 | 55 | | "Go Back" |
| | 0 | 56 | | }; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Serves as the main entry point of the application. |
| | | 60 | | /// </summary> |
| | | 61 | | /// |
| | | 62 | | /// <param name="args">The set of command line arguments passed.</param> |
| | | 63 | | /// |
| | | 64 | | public static async Task Main(string[] args) |
| | | 65 | | { |
| | | 66 | | // Parse the command line arguments determine if help was explicitly requested or if the |
| | | 67 | | // needed information was passed. |
| | | 68 | | |
| | 0 | 69 | | CommandLineArguments parsedArgs = ParseArguments(args); |
| | | 70 | | |
| | 0 | 71 | | if (parsedArgs.Help) |
| | | 72 | | { |
| | 0 | 73 | | DisplayHelp(); |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | // Display the welcome message. |
| | | 78 | | |
| | 0 | 79 | | Console.WriteLine(); |
| | 0 | 80 | | Console.WriteLine("========================================="); |
| | 0 | 81 | | Console.WriteLine("Welcome to the Event Hubs client library!"); |
| | 0 | 82 | | Console.WriteLine("========================================="); |
| | 0 | 83 | | Console.WriteLine(); |
| | | 84 | | |
| | 0 | 85 | | ISample sample = RetrieveSample(); |
| | | 86 | | |
| | 0 | 87 | | if (sample == null) |
| | | 88 | | { |
| | 0 | 89 | | Console.WriteLine(); |
| | 0 | 90 | | Console.WriteLine(); |
| | 0 | 91 | | Console.WriteLine("Quitting..."); |
| | 0 | 92 | | Console.WriteLine(); |
| | | 93 | | |
| | 0 | 94 | | return; |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | Console.WriteLine(); |
| | 0 | 99 | | Console.WriteLine(); |
| | 0 | 100 | | Console.WriteLine(); |
| | 0 | 101 | | Console.WriteLine("-------------------------------------------------------------------------"); |
| | 0 | 102 | | Console.WriteLine($"Running: { sample.Name }"); |
| | 0 | 103 | | Console.WriteLine("-------------------------------------------------------------------------"); |
| | 0 | 104 | | Console.WriteLine(); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // Run the chosen sample |
| | | 108 | | |
| | 0 | 109 | | if (sample is IEventHubsSample eventHubsSample) |
| | | 110 | | { |
| | 0 | 111 | | PromptConnectionStringIfMissing(parsedArgs); |
| | 0 | 112 | | PromptEventHubNameIfMissing(parsedArgs); |
| | | 113 | | |
| | 0 | 114 | | await eventHubsSample.RunAsync(parsedArgs.ConnectionString, parsedArgs.EventHub); |
| | | 115 | | } |
| | 0 | 116 | | else if (sample is IEventHubsIdentitySample identitySample) |
| | | 117 | | { |
| | 0 | 118 | | PromptFullyQualifiedNamespaceIfMissing(parsedArgs); |
| | 0 | 119 | | PromptEventHubNameIfMissing(parsedArgs); |
| | 0 | 120 | | PromptTenantIdIfMissing(parsedArgs); |
| | 0 | 121 | | PromptClientIdIfMissing(parsedArgs); |
| | 0 | 122 | | PromptSecretIfMissing(parsedArgs); |
| | | 123 | | |
| | 0 | 124 | | await identitySample.RunAsync(parsedArgs.FullyQualifiedNamespace, |
| | 0 | 125 | | parsedArgs.EventHub, |
| | 0 | 126 | | parsedArgs.Tenant, |
| | 0 | 127 | | parsedArgs.Client, |
| | 0 | 128 | | parsedArgs.Secret); |
| | | 129 | | } |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// It prints a list of options to console for the user. It reads the user's |
| | | 134 | | /// choice then it returns the chosen sample. |
| | | 135 | | /// </summary> |
| | | 136 | | /// |
| | | 137 | | /// <returns>A sample or null if that could not be determined.</returns> |
| | | 138 | | /// |
| | | 139 | | private static ISample RetrieveSample() |
| | | 140 | | { |
| | 0 | 141 | | var samples = LocateSamples<IEventHubsSample>(); |
| | | 142 | | |
| | 0 | 143 | | PrintEventHubsSamples(samples); |
| | | 144 | | |
| | 0 | 145 | | int? choice = ReadSelection(samples); |
| | | 146 | | |
| | 0 | 147 | | if (IsEventHubsIdentity(samples, choice)) |
| | | 148 | | { |
| | 0 | 149 | | var identitySamples = LocateSamples<IEventHubsIdentitySample>(); |
| | | 150 | | |
| | 0 | 151 | | PrintEventHubsIdentitySamples(identitySamples); |
| | | 152 | | |
| | 0 | 153 | | while (IsEventHubsIdentity(samples, choice)) |
| | | 154 | | { |
| | 0 | 155 | | choice = ReadSelection(identitySamples); |
| | | 156 | | |
| | 0 | 157 | | if (choice.HasValue && !IsGoBack(identitySamples, choice)) |
| | | 158 | | { |
| | 0 | 159 | | return identitySamples[choice.Value]; |
| | | 160 | | } |
| | 0 | 161 | | else if (IsGoBack(identitySamples, choice)) |
| | | 162 | | { |
| | 0 | 163 | | choice = ReadSelection(samples); |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | if (choice.HasValue) |
| | | 169 | | { |
| | 0 | 170 | | return samples[choice.Value]; |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return null; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// It checks if an option is to go back. |
| | | 178 | | /// </summary> |
| | | 179 | | /// |
| | | 180 | | /// <param name="identitySamples">A list of identity samples</param> |
| | | 181 | | /// <param name="choice">The zero-based index referring to the option chosen from console</param> |
| | | 182 | | /// |
| | | 183 | | /// <returns>If the user has chosed to go back in the main sample listing.</returns> |
| | | 184 | | /// |
| | | 185 | | private static bool IsGoBack(IReadOnlyList<IEventHubsIdentitySample> identitySamples, int? choice) |
| | | 186 | | { |
| | 0 | 187 | | return IsLastOption(identitySamples, ExtraOptionsForIdentitySamples.Length, choice); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// It checks if an option is to see the identity samples. |
| | | 192 | | /// </summary> |
| | | 193 | | /// |
| | | 194 | | /// <param name="samples">A list of samples</param> |
| | | 195 | | /// <param name="choice">The zero-based index referring to the option chosen from console</param> |
| | | 196 | | /// |
| | | 197 | | /// <returns>If the user has chosen to see the event hubs identity samples.</returns> |
| | | 198 | | /// |
| | | 199 | | private static bool IsEventHubsIdentity(IReadOnlyList<IEventHubsSample> samples, int? choice) |
| | | 200 | | { |
| | 0 | 201 | | return IsLastOption(samples, ExtraOptionsForSamples.Length, choice); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// It checks if an option is the last available. |
| | | 206 | | /// </summary> |
| | | 207 | | /// |
| | | 208 | | /// <param name="samples">A list of identity samples</param> |
| | | 209 | | /// <param name="numberOfExtraOptions">The number of extra options available for the category of samples</param> |
| | | 210 | | /// <param name="choice">The zero-based index referring to the option chosen from console</param> |
| | | 211 | | /// |
| | | 212 | | /// <typeparam name="TSampleCategory">The category of samples selected.</typeparam> |
| | | 213 | | /// |
| | | 214 | | /// <returns>If the users have chosen the last of the set of options that were presented to them.</returns> |
| | | 215 | | /// |
| | | 216 | | private static bool IsLastOption<TSampleCategory>(IReadOnlyList<TSampleCategory> samples, int? numberOfExtraOpti |
| | | 217 | | { |
| | 0 | 218 | | return choice == (samples.Count + numberOfExtraOptions - 1); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// It prints to console a set of samples and controlling options. |
| | | 223 | | /// </summary> |
| | | 224 | | /// |
| | | 225 | | /// <param name="samples">A list of samples to be printed</param> |
| | | 226 | | /// |
| | | 227 | | private static void PrintEventHubsSamples(IReadOnlyList<ISample> samples) |
| | | 228 | | { |
| | 0 | 229 | | PrintSamples(samples); |
| | | 230 | | |
| | 0 | 231 | | for (int i = 0; i < ExtraOptionsForSamples.Length; i++) |
| | | 232 | | { |
| | 0 | 233 | | Console.WriteLine($"{ samples.Count + i + 1 }) { ExtraOptionsForSamples[i] }"); |
| | 0 | 234 | | Console.WriteLine(); |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | Console.WriteLine(); |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// It prints to console a set of identity samples and controlling options. |
| | | 242 | | /// </summary> |
| | | 243 | | /// |
| | | 244 | | /// <param name="samples">A list of samples to be printed</param> |
| | | 245 | | /// |
| | | 246 | | private static void PrintEventHubsIdentitySamples(IReadOnlyList<ISample> samples) |
| | | 247 | | { |
| | 0 | 248 | | PrintSamples(samples); |
| | | 249 | | |
| | 0 | 250 | | for (int i = 0; i < ExtraOptionsForIdentitySamples.Length; i++) |
| | | 251 | | { |
| | 0 | 252 | | Console.WriteLine($"{ samples.Count + i + 1 }) { ExtraOptionsForIdentitySamples[i] }"); |
| | 0 | 253 | | Console.WriteLine(); |
| | | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | Console.WriteLine(); |
| | 0 | 257 | | } |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// It prints to console a set of sample names and their description. |
| | | 261 | | /// </summary> |
| | | 262 | | /// |
| | | 263 | | /// <param name="samples">A list of samples to be printed</param> |
| | | 264 | | /// |
| | | 265 | | private static void PrintSamples(IReadOnlyList<ISample> samples) |
| | | 266 | | { |
| | | 267 | | // Display the set of available samples and allow the user to choose. |
| | | 268 | | |
| | 0 | 269 | | Console.WriteLine(); |
| | 0 | 270 | | Console.WriteLine("Available Samples:"); |
| | 0 | 271 | | Console.WriteLine(); |
| | | 272 | | |
| | 0 | 273 | | for (var index = 0; index < samples.Count; ++index) |
| | | 274 | | { |
| | 0 | 275 | | Console.WriteLine($"{ index + 1 }) { samples[index].Name }"); |
| | 0 | 276 | | Console.WriteLine($"\t{ samples[index].Description }"); |
| | 0 | 277 | | Console.WriteLine(); |
| | | 278 | | } |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Prompt the user to insert the EventHubs connection string, if not |
| | | 283 | | /// already passed it from command line. |
| | | 284 | | /// </summary> |
| | | 285 | | /// |
| | | 286 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 287 | | /// |
| | | 288 | | private static void PromptConnectionStringIfMissing(CommandLineArguments parsedArgs) |
| | | 289 | | { |
| | | 290 | | // Prompt for the connection string, if it wasn't passed. |
| | | 291 | | |
| | 0 | 292 | | while (string.IsNullOrEmpty(parsedArgs.ConnectionString)) |
| | | 293 | | { |
| | 0 | 294 | | Console.Write("Please provide the connection string for the Event Hubs namespace that you'd like to use |
| | 0 | 295 | | parsedArgs.ConnectionString = Console.ReadLine().Trim(); |
| | 0 | 296 | | Console.WriteLine(); |
| | | 297 | | } |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Prompt the user to insert the EventHubs fully qualified name, if not |
| | | 302 | | /// already passed it from command line. |
| | | 303 | | /// </summary> |
| | | 304 | | /// |
| | | 305 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 306 | | /// |
| | | 307 | | private static void PromptEventHubNameIfMissing(CommandLineArguments parsedArgs) |
| | | 308 | | { |
| | | 309 | | // Prompt for the Event Hub name, if it wasn't passed. |
| | | 310 | | |
| | 0 | 311 | | while (string.IsNullOrEmpty(parsedArgs.EventHub)) |
| | | 312 | | { |
| | 0 | 313 | | Console.Write("Please provide the name of the Event Hub that you'd like to use and then press Enter: "); |
| | 0 | 314 | | parsedArgs.EventHub = Console.ReadLine().Trim(); |
| | 0 | 315 | | Console.WriteLine(); |
| | | 316 | | } |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Prompt the user to insert the Azure Active Directory service client id, if not |
| | | 321 | | /// already passed it from command line. |
| | | 322 | | /// </summary> |
| | | 323 | | /// |
| | | 324 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 325 | | /// |
| | | 326 | | private static void PromptClientIdIfMissing(CommandLineArguments parsedArgs) |
| | | 327 | | { |
| | | 328 | | // Prompt for the Event Hub name, if it wasn't passed. |
| | | 329 | | |
| | 0 | 330 | | while (string.IsNullOrEmpty(parsedArgs.Client)) |
| | | 331 | | { |
| | 0 | 332 | | Console.Write("Please provide the Azure Active Directory client identifier of the service principal: "); |
| | 0 | 333 | | parsedArgs.Client = Console.ReadLine().Trim(); |
| | 0 | 334 | | Console.WriteLine(); |
| | | 335 | | } |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Prompt the user to insert the EventHubs fully qualified namespace, if not |
| | | 340 | | /// already passed it from command line. |
| | | 341 | | /// </summary> |
| | | 342 | | /// |
| | | 343 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 344 | | /// |
| | | 345 | | private static void PromptFullyQualifiedNamespaceIfMissing(CommandLineArguments parsedArgs) |
| | | 346 | | { |
| | | 347 | | // Prompt for the Fully Qualified Namespace, if it wasn't passed. |
| | | 348 | | |
| | 0 | 349 | | while (string.IsNullOrEmpty(parsedArgs.FullyQualifiedNamespace)) |
| | | 350 | | { |
| | 0 | 351 | | Console.Write("Please provide the fully qualified Event Hubs namespace. This is likely to be similar to |
| | 0 | 352 | | parsedArgs.FullyQualifiedNamespace = Console.ReadLine().Trim(); |
| | 0 | 353 | | Console.WriteLine(); |
| | | 354 | | } |
| | 0 | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Prompt the user to insert the Azure Active Directory service principal secret, if not |
| | | 359 | | /// already passed it from command line. |
| | | 360 | | /// </summary> |
| | | 361 | | /// |
| | | 362 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 363 | | /// |
| | | 364 | | private static void PromptSecretIfMissing(CommandLineArguments parsedArgs) |
| | | 365 | | { |
| | | 366 | | // Prompt for the Secret, if it wasn't passed. |
| | | 367 | | |
| | 0 | 368 | | while (string.IsNullOrEmpty(parsedArgs.Secret)) |
| | | 369 | | { |
| | 0 | 370 | | Console.Write("Please provide the Azure Active Directory secret of the service principal: "); |
| | 0 | 371 | | parsedArgs.Secret = Console.ReadLine().Trim(); |
| | 0 | 372 | | Console.WriteLine(); |
| | | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// Prompt the user to insert the Azure Active Directory tenant id, if not already passed it from |
| | | 378 | | /// command line. |
| | | 379 | | /// </summary> |
| | | 380 | | /// |
| | | 381 | | /// <param name="parsedArgs">The arguments passed from console.</param> |
| | | 382 | | /// |
| | | 383 | | private static void PromptTenantIdIfMissing(CommandLineArguments parsedArgs) |
| | | 384 | | { |
| | | 385 | | // The Azure Active Directory tenant that holds the service principal. |
| | | 386 | | |
| | 0 | 387 | | while (string.IsNullOrEmpty(parsedArgs.Tenant)) |
| | | 388 | | { |
| | 0 | 389 | | Console.Write("Please provide the Azure Active Directory tenant of the service principal: "); |
| | 0 | 390 | | parsedArgs.Tenant = Console.ReadLine().Trim(); |
| | 0 | 391 | | Console.WriteLine(); |
| | | 392 | | } |
| | 0 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Displays the help text for running the samples to the console output. |
| | | 397 | | /// </summary> |
| | | 398 | | /// |
| | | 399 | | private static void DisplayHelp() |
| | | 400 | | { |
| | 0 | 401 | | Console.WriteLine(); |
| | 0 | 402 | | Console.WriteLine($"{ typeof(Program).Namespace }"); |
| | 0 | 403 | | Console.WriteLine(); |
| | 0 | 404 | | Console.WriteLine("This executable allows for running the Azure Event Hubs client library samples. Because" |
| | 0 | 405 | | Console.WriteLine("the samples run against live Azure services, they require an Event Hubs namespace and an" |
| | 0 | 406 | | Console.WriteLine("Event Hub under it in order to run."); |
| | 0 | 407 | | Console.WriteLine(); |
| | 0 | 408 | | Console.WriteLine(); |
| | 0 | 409 | | Console.WriteLine("Arguments:"); |
| | 0 | 410 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.Help) }:"); |
| | 0 | 411 | | Console.WriteLine("\t\tDisplays this message."); |
| | 0 | 412 | | Console.WriteLine(); |
| | 0 | 413 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.ConnectionString) }:"); |
| | 0 | 414 | | Console.WriteLine("\t\tThe connection string to the Event Hubs namespace to use for the samples."); |
| | 0 | 415 | | Console.WriteLine(); |
| | 0 | 416 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.EventHub) }:"); |
| | 0 | 417 | | Console.WriteLine("\t\tThe name of the Event Hub under the namespace to use."); |
| | 0 | 418 | | Console.WriteLine(); |
| | 0 | 419 | | Console.WriteLine("Usage:"); |
| | 0 | 420 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Samples.exe"); |
| | 0 | 421 | | Console.WriteLine(); |
| | 0 | 422 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Samples.exe { CommandLineArguments.ArgumentPrefix }{ nameof( |
| | 0 | 423 | | Console.WriteLine(); |
| | 0 | 424 | | Console.WriteLine("\tAzure.Messaging.EventHubs.Samples.exe \"Endpoint=sb://fake.servicebus.windows.net/;Shar |
| | 0 | 425 | | Console.WriteLine(); |
| | 0 | 426 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Samples.exe { CommandLineArguments.ArgumentPrefix }{ nameof( |
| | 0 | 427 | | Console.WriteLine(); |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Reads the selection of the application's user from the console for samples. |
| | | 432 | | /// </summary> |
| | | 433 | | /// |
| | | 434 | | /// <param name="samples">The available samples.</param> |
| | | 435 | | /// |
| | | 436 | | /// <typeparam name="TSampleCategory">The interface associated with the category of samples.</typeparam> |
| | | 437 | | /// |
| | | 438 | | /// <returns>The validated selection that was made.</returns> |
| | | 439 | | /// |
| | 0 | 440 | | private static int? ReadSelection<TSampleCategory>(IReadOnlyList<TSampleCategory> samples) => samples switch |
| | 0 | 441 | | { |
| | 0 | 442 | | IReadOnlyList<IEventHubsSample> eventHubSamples => ReadSelection(eventHubSamples.Count + ExtraOptionsForSamp |
| | 0 | 443 | | IReadOnlyList<IEventHubsIdentitySample> identitySamples => ReadSelection(identitySamples.Count + ExtraOption |
| | 0 | 444 | | _ => throw new ArgumentException() |
| | 0 | 445 | | }; |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Reads the selection of the application's user from the console. |
| | | 449 | | /// </summary> |
| | | 450 | | /// |
| | | 451 | | /// <param name="optionsCount">The count of available options.</param> |
| | | 452 | | /// |
| | | 453 | | /// <returns>The validated selection that was made.</returns> |
| | | 454 | | /// |
| | | 455 | | private static int? ReadSelection(int optionsCount) |
| | | 456 | | { |
| | | 457 | | while (true) |
| | | 458 | | { |
| | 0 | 459 | | Console.Write("Please enter the number of a sample to run or press \"X\" to exit: "); |
| | | 460 | | |
| | 0 | 461 | | var value = Console.ReadLine(); |
| | | 462 | | |
| | 0 | 463 | | if (string.Equals(value, "X", StringComparison.OrdinalIgnoreCase)) |
| | | 464 | | { |
| | 0 | 465 | | return null; |
| | | 466 | | } |
| | | 467 | | |
| | 0 | 468 | | if (Int32.TryParse(value, out var choice)) |
| | | 469 | | { |
| | 0 | 470 | | --choice; |
| | | 471 | | |
| | 0 | 472 | | if ((choice >= 0) && (choice < optionsCount)) |
| | | 473 | | { |
| | 0 | 474 | | return choice; |
| | | 475 | | } |
| | | 476 | | } |
| | | 477 | | } |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | /// <summary> |
| | | 481 | | /// Parses the set of arguments read from the command line. |
| | | 482 | | /// </summary> |
| | | 483 | | /// |
| | | 484 | | /// <param name="args">The command line arguments.</param> |
| | | 485 | | /// |
| | | 486 | | /// <returns>The set of parsed arguments, with any values for known items captured and cleaned.</returns> |
| | | 487 | | /// |
| | | 488 | | private static CommandLineArguments ParseArguments(string[] args) |
| | | 489 | | { |
| | | 490 | | // If at least two arguments were passed with no argument designator, then assume they're values and |
| | | 491 | | // accept them positionally. |
| | | 492 | | |
| | 0 | 493 | | if ((args.Length >= 2) && (!args[0].StartsWith(CommandLineArguments.ArgumentPrefix)) && (!args[1].StartsWith |
| | | 494 | | { |
| | 0 | 495 | | return new CommandLineArguments { ConnectionString = args[0], EventHub = args[1] }; |
| | | 496 | | } |
| | | 497 | | |
| | 0 | 498 | | var parsedArgs = new CommandLineArguments(); |
| | | 499 | | |
| | | 500 | | // Enumerate the arguments that were passed, stopping one before the |
| | | 501 | | // end, since we're scanning forward by an item to retrieve values; if a |
| | | 502 | | // command was passed in the last position, there was no accompanying value, |
| | | 503 | | // so it isn't useful. |
| | | 504 | | |
| | 0 | 505 | | for (var index = 0; index < args.Length - 1; ++index) |
| | | 506 | | { |
| | | 507 | | // Remove any excess spaces to comparison purposes. |
| | | 508 | | |
| | 0 | 509 | | args[index] = args[index].Trim(); |
| | | 510 | | |
| | | 511 | | // Help is the only flag argument supported; check for it before making |
| | | 512 | | // assumptions about argument/value pairings that comprise the other tokens. |
| | | 513 | | |
| | 0 | 514 | | if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Help) }", |
| | | 515 | | { |
| | 0 | 516 | | parsedArgs.Help = true; |
| | 0 | 517 | | continue; |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | // Since we're evaluating the next token in sequence as a value in the |
| | | 521 | | // checks that follow, if it is an argument, we'll skip to the next iteration. |
| | | 522 | | |
| | 0 | 523 | | if (args[index + 1].StartsWith(CommandLineArguments.ArgumentPrefix)) |
| | | 524 | | { |
| | | 525 | | continue; |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | // If the current token is one of our known arguments, capture the next token in sequence as it's |
| | | 529 | | // value, since we've already ruled out that it is another argument name. |
| | | 530 | | |
| | 0 | 531 | | if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Connection |
| | | 532 | | { |
| | 0 | 533 | | parsedArgs.ConnectionString = args[index + 1].Trim(); |
| | | 534 | | } |
| | 0 | 535 | | else if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Event |
| | | 536 | | { |
| | 0 | 537 | | parsedArgs.EventHub = args[index + 1].Trim(); |
| | | 538 | | } |
| | 0 | 539 | | else if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Clien |
| | | 540 | | { |
| | 0 | 541 | | parsedArgs.Client = args[index + 1].Trim(); |
| | | 542 | | } |
| | 0 | 543 | | else if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Fully |
| | | 544 | | { |
| | 0 | 545 | | parsedArgs.FullyQualifiedNamespace = args[index + 1].Trim(); |
| | | 546 | | } |
| | 0 | 547 | | else if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Tenan |
| | | 548 | | { |
| | 0 | 549 | | parsedArgs.Tenant = args[index + 1].Trim(); |
| | | 550 | | } |
| | 0 | 551 | | else if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Secre |
| | | 552 | | { |
| | 0 | 553 | | parsedArgs.Secret = args[index + 1].Trim(); |
| | | 554 | | } |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | return parsedArgs; |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | /// <summary> |
| | | 561 | | /// Locates the samples within the solution and creates an instance |
| | | 562 | | /// that can be inspected and run. |
| | | 563 | | /// </summary> |
| | | 564 | | /// |
| | | 565 | | /// <typeparam name="TSampleCategory">The interface associated with the category of samples that should be locat |
| | | 566 | | /// |
| | | 567 | | /// <returns>The set of samples defined in the solution.</returns> |
| | | 568 | | /// |
| | | 569 | | private static IReadOnlyList<TSampleCategory> LocateSamples<TSampleCategory>() => |
| | 0 | 570 | | typeof(Program) |
| | 0 | 571 | | .Assembly |
| | 0 | 572 | | .ExportedTypes |
| | 0 | 573 | | .Where(type => (type.IsClass && typeof(TSampleCategory).IsAssignableFrom(type))) |
| | 0 | 574 | | .Select(type => (TSampleCategory)Activator.CreateInstance(type)) |
| | 0 | 575 | | .ToList(); |
| | | 576 | | |
| | | 577 | | /// <summary> |
| | | 578 | | /// Provides a local means of collecting and passing |
| | | 579 | | /// the command line arguments received. |
| | | 580 | | /// </summary> |
| | | 581 | | /// |
| | | 582 | | private class CommandLineArguments |
| | | 583 | | { |
| | | 584 | | /// <summary>The sequence of characters that prefix a command-line argument.</summary> |
| | | 585 | | public const string ArgumentPrefix = "--"; |
| | | 586 | | |
| | | 587 | | /// <summary>The connection string to the Azure Event Hubs namespace for samples.</summary> |
| | | 588 | | public string ConnectionString; |
| | | 589 | | |
| | | 590 | | /// <summary>The name of the Event Hub to use samples.</summary> |
| | | 591 | | public string EventHub; |
| | | 592 | | |
| | | 593 | | /// <summary>The fully qualified Event Hubs namespace. This is likely to be similar to <c>{yournamespace}.s |
| | | 594 | | public string FullyQualifiedNamespace; |
| | | 595 | | |
| | | 596 | | /// <summary>The Azure Active Directory tenant that holds the service principal.</summary> |
| | | 597 | | public string Tenant; |
| | | 598 | | |
| | | 599 | | /// <summary>The Azure Active Directory client identifier of the service principal.</summary> |
| | | 600 | | public string Client; |
| | | 601 | | |
| | | 602 | | /// <summary>The Azure Active Directory secret of the service principal.</summary> |
| | | 603 | | public string Secret; |
| | | 604 | | |
| | | 605 | | /// <summary>A flag indicating whether or not help was requested.</summary> |
| | | 606 | | public bool Help; |
| | | 607 | | } |
| | | 608 | | } |
| | | 609 | | } |