| | 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.Processor.Samples.Infrastructure; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs.Processor.Samples |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The main entry point for executing the samples. |
| | 14 | | /// </summary> |
| | 15 | | /// |
| | 16 | | public static class Program |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Serves as the main entry point of the application. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| | 22 | | /// <param name="args">The set of command line arguments passed.</param> |
| | 23 | | /// |
| | 24 | | public static async Task Main(string[] args) |
| | 25 | | { |
| | 26 | | // Parse the command line arguments determine if help was explicitly requested or if the |
| | 27 | | // needed information was passed. |
| | 28 | |
|
| 0 | 29 | | CommandLineArguments parsedArgs = ParseArguments(args); |
| | 30 | |
|
| 0 | 31 | | if (parsedArgs.Help) |
| | 32 | | { |
| 0 | 33 | | DisplayHelp(); |
| 0 | 34 | | return; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | // Display the welcome message. |
| | 38 | |
|
| 0 | 39 | | Console.WriteLine(); |
| 0 | 40 | | Console.WriteLine("============================================================================="); |
| 0 | 41 | | Console.WriteLine("Welcome to the Event Hubs Checkpoint Storage client library for Blob Storage!"); |
| 0 | 42 | | Console.WriteLine("============================================================================="); |
| 0 | 43 | | Console.WriteLine(); |
| | 44 | |
|
| | 45 | | // Prompt for the Event Hubs connection string, if it wasn't passed. |
| | 46 | |
|
| 0 | 47 | | while (string.IsNullOrEmpty(parsedArgs.EventHubsConnectionString)) |
| | 48 | | { |
| 0 | 49 | | Console.Write("Please provide the connection string for the Event Hubs namespace that you'd like to use |
| 0 | 50 | | parsedArgs.EventHubsConnectionString = Console.ReadLine().Trim(); |
| 0 | 51 | | Console.WriteLine(); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | // Prompt for the Event Hub name, if it wasn't passed. |
| | 55 | |
|
| 0 | 56 | | while (string.IsNullOrEmpty(parsedArgs.EventHub)) |
| | 57 | | { |
| 0 | 58 | | Console.Write("Please provide the name of the Event Hub that you'd like to use and then press Enter: "); |
| 0 | 59 | | parsedArgs.EventHub = Console.ReadLine().Trim(); |
| 0 | 60 | | Console.WriteLine(); |
| | 61 | | } |
| | 62 | |
|
| | 63 | | // Prompt for the storage connection string, if it wasn't passed. |
| | 64 | |
|
| 0 | 65 | | while (string.IsNullOrEmpty(parsedArgs.StorageConnectionString)) |
| | 66 | | { |
| 0 | 67 | | Console.Write("Please provide the connection string for the Azure storage account that you'd like to use |
| 0 | 68 | | parsedArgs.StorageConnectionString = Console.ReadLine().Trim(); |
| 0 | 69 | | Console.WriteLine(); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | // Prompt for the blob container name, if it wasn't passed. |
| | 73 | |
|
| 0 | 74 | | while (string.IsNullOrEmpty(parsedArgs.BlobContainer)) |
| | 75 | | { |
| 0 | 76 | | Console.Write("Please provide the name of the blob container that you'd like to use and then press Enter |
| 0 | 77 | | parsedArgs.BlobContainer = Console.ReadLine().Trim(); |
| 0 | 78 | | Console.WriteLine(); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // Display the set of available samples and allow them to be run. |
| | 82 | |
|
| 0 | 83 | | IReadOnlyList<IEventHubsBlobCheckpointSample> samples = LocateSamples(); |
| | 84 | |
|
| 0 | 85 | | Console.WriteLine(); |
| 0 | 86 | | Console.WriteLine("Available Samples:"); |
| 0 | 87 | | Console.WriteLine(); |
| | 88 | |
|
| 0 | 89 | | for (var index = 0; index < samples.Count; ++index) |
| | 90 | | { |
| 0 | 91 | | Console.WriteLine($"{ index + 1 }) { samples[index].Name }"); |
| 0 | 92 | | Console.WriteLine($"\t{ samples[index].Description }"); |
| 0 | 93 | | Console.WriteLine(); |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | Console.WriteLine(); |
| 0 | 97 | | var choice = ReadSelection(samples.Count); |
| | 98 | |
|
| 0 | 99 | | if (choice == null) |
| | 100 | | { |
| 0 | 101 | | Console.WriteLine(); |
| 0 | 102 | | Console.WriteLine(); |
| 0 | 103 | | Console.WriteLine("Quitting..."); |
| 0 | 104 | | Console.WriteLine(); |
| 0 | 105 | | return; |
| | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 0 | 109 | | Console.WriteLine(); |
| 0 | 110 | | Console.WriteLine(); |
| 0 | 111 | | Console.WriteLine(); |
| 0 | 112 | | Console.WriteLine("-------------------------------------------------------------------------"); |
| 0 | 113 | | Console.WriteLine($"Running: { samples[choice.Value].Name }"); |
| 0 | 114 | | Console.WriteLine("-------------------------------------------------------------------------"); |
| 0 | 115 | | Console.WriteLine(); |
| | 116 | |
|
| 0 | 117 | | await samples[choice.Value].RunAsync(parsedArgs.EventHubsConnectionString, parsedArgs.EventHub, parsedAr |
| 0 | 118 | | return; |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Displays the help text for running the samples to the console |
| | 124 | | /// output. |
| | 125 | | /// </summary> |
| | 126 | | /// |
| | 127 | | private static void DisplayHelp() |
| | 128 | | { |
| 0 | 129 | | var eventHubsSample = "Endpoint=sb://fake.servicebus.windows.net/;SharedAccessKeyName=NotReal;SharedAccessKe |
| 0 | 130 | | var storageSample = "DefaultEndpointsProtocol=https;AccountName=NotReal;AccountKey=[FAKE];EndpointSuffix=cor |
| | 131 | |
|
| 0 | 132 | | Console.WriteLine(); |
| 0 | 133 | | Console.WriteLine($"{ typeof(Program).Namespace }"); |
| 0 | 134 | | Console.WriteLine(); |
| 0 | 135 | | Console.WriteLine("This executable allows for running the Azure Event Hubs Blob Checkpoint Store library sam |
| 0 | 136 | | Console.WriteLine("the samples run against live Azure services, they require an Event Hubs namespace, an Eve |
| 0 | 137 | | Console.WriteLine("Azure storage account, and a blob storage container to run."); |
| 0 | 138 | | Console.WriteLine(); |
| 0 | 139 | | Console.WriteLine(); |
| 0 | 140 | | Console.WriteLine("Arguments:"); |
| 0 | 141 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.Help) }:"); |
| 0 | 142 | | Console.WriteLine("\t\tDisplays this message."); |
| 0 | 143 | | Console.WriteLine(); |
| 0 | 144 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.EventHubsConnectionString) }:"); |
| 0 | 145 | | Console.WriteLine("\t\tThe connection string to the Event Hubs namespace to use for the samples."); |
| 0 | 146 | | Console.WriteLine(); |
| 0 | 147 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.EventHub) }:"); |
| 0 | 148 | | Console.WriteLine("\t\tThe name of the Event Hub under the namespace to use."); |
| 0 | 149 | | Console.WriteLine(); |
| 0 | 150 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.StorageConnectionString) }:"); |
| 0 | 151 | | Console.WriteLine("\t\tThe connection string to the Azure storage account to use for the samples."); |
| 0 | 152 | | Console.WriteLine(); |
| 0 | 153 | | Console.WriteLine($"\t{ nameof(CommandLineArguments.BlobContainer) }:"); |
| 0 | 154 | | Console.WriteLine("\t\tThe name of the blob container under the storage account to use for checkpoints."); |
| 0 | 155 | | Console.WriteLine(); |
| 0 | 156 | | Console.WriteLine("Usage:"); |
| 0 | 157 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe"); |
| 0 | 158 | | Console.WriteLine(); |
| 0 | 159 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe { CommandLineArguments.ArgumentPrefix |
| 0 | 160 | | Console.WriteLine(); |
| 0 | 161 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe \"{ eventHubsSample}\" \"SomeHub\" \"{ |
| 0 | 162 | | Console.WriteLine(); |
| 0 | 163 | | Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe { CommandLineArguments.ArgumentPrefix |
| 0 | 164 | | Console.WriteLine(); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <summary> |
| | 168 | | /// Reads the selection of the application's user from the |
| | 169 | | /// console. |
| | 170 | | /// </summary> |
| | 171 | | /// |
| | 172 | | /// <param name="sampleCount">The count of available samples.</param> |
| | 173 | | /// |
| | 174 | | /// <returns>The validated selection that was made.</returns> |
| | 175 | | /// |
| | 176 | | private static int? ReadSelection(int sampleCount) |
| | 177 | | { |
| | 178 | | while (true) |
| | 179 | | { |
| 0 | 180 | | Console.Write("Please enter the number of a sample to run or press \"X\" to exit: "); |
| | 181 | |
|
| 0 | 182 | | var value = Console.ReadLine(); |
| | 183 | |
|
| 0 | 184 | | if (string.Equals(value, "X", StringComparison.OrdinalIgnoreCase)) |
| | 185 | | { |
| 0 | 186 | | return null; |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | if (int.TryParse(value, out var choice)) |
| | 190 | | { |
| 0 | 191 | | --choice; |
| | 192 | |
|
| 0 | 193 | | if ((choice >= 0) && (choice < sampleCount)) |
| | 194 | | { |
| 0 | 195 | | return choice; |
| | 196 | | } |
| | 197 | | } |
| | 198 | | } |
| | 199 | | } |
| | 200 | |
|
| | 201 | | /// <summary> |
| | 202 | | /// Parses the set of arguments read from the command line. |
| | 203 | | /// </summary> |
| | 204 | | /// |
| | 205 | | /// <param name="args">The command line arguments.</param> |
| | 206 | | /// |
| | 207 | | /// <returns>The set of parsed arguments, with any values for known items captured and cleaned.</returns> |
| | 208 | | /// |
| | 209 | | private static CommandLineArguments ParseArguments(string[] args) |
| | 210 | | { |
| | 211 | | // If at least four arguments were passed with no argument designator, then assume they're values and |
| | 212 | | // accept them positionally. |
| | 213 | |
|
| 0 | 214 | | if ((args.Length >= 4) && (!args[0].StartsWith(CommandLineArguments.ArgumentPrefix)) && (!args[1].StartsWith |
| | 215 | | { |
| 0 | 216 | | return new CommandLineArguments |
| 0 | 217 | | { |
| 0 | 218 | | EventHubsConnectionString = args[0], |
| 0 | 219 | | EventHub = args[1], |
| 0 | 220 | | StorageConnectionString = args[2], |
| 0 | 221 | | BlobContainer = args[3] |
| 0 | 222 | | }; |
| | 223 | | } |
| | 224 | |
|
| 0 | 225 | | var parsedArgs = new CommandLineArguments(); |
| | 226 | |
|
| | 227 | | // Enumerate the arguments that were passed, stopping one before the |
| | 228 | | // end, since we're scanning forward by an item to retrieve values; if a |
| | 229 | | // command was passed in the last position, there was no accompanying value, |
| | 230 | | // so it isn't useful. |
| | 231 | |
|
| 0 | 232 | | for (var index = 0; index < args.Length - 1; ++index) |
| | 233 | | { |
| | 234 | | // Remove any excess spaces to comparison purposes. |
| | 235 | |
|
| 0 | 236 | | args[index] = args[index].Trim(); |
| | 237 | |
|
| | 238 | | // Help is the only flag argument supported; check for it before making |
| | 239 | | // assumptions about argument/value pairings that comprise the other tokens. |
| | 240 | |
|
| 0 | 241 | | if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Help) }", |
| | 242 | | { |
| 0 | 243 | | parsedArgs.Help = true; |
| 0 | 244 | | continue; |
| | 245 | | } |
| | 246 | |
|
| | 247 | | // Since we're evaluating the next token in sequence as a value in the |
| | 248 | | // checks that follow, if it is an argument, we'll skip to the next iteration. |
| | 249 | |
|
| 0 | 250 | | if (args[index + 1].StartsWith(CommandLineArguments.ArgumentPrefix)) |
| | 251 | | { |
| | 252 | | continue; |
| | 253 | | } |
| | 254 | |
|
| | 255 | | // If the current token is one of our known arguments, capture the next token in sequence as it's |
| | 256 | | // value, since we've already ruled out that it is another argument name. |
| | 257 | |
|
| 0 | 258 | | if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.EventHubsC |
| | 259 | | { |
| 0 | 260 | | parsedArgs.EventHubsConnectionString = args[index + 1].Trim(); |
| | 261 | | } |
| 0 | 262 | | else if (args[index].Equals($"--{ nameof(CommandLineArguments.EventHub) }", StringComparison.OrdinalIgno |
| | 263 | | { |
| 0 | 264 | | parsedArgs.EventHub = args[index + 1].Trim(); |
| | 265 | | } |
| 0 | 266 | | else if (args[index].Equals($"--{ nameof(CommandLineArguments.StorageConnectionString) }", StringCompari |
| | 267 | | { |
| 0 | 268 | | parsedArgs.StorageConnectionString = args[index + 1].Trim(); |
| | 269 | | } |
| 0 | 270 | | else if (args[index].Equals($"--{ nameof(CommandLineArguments.BlobContainer) }", StringComparison.Ordina |
| | 271 | | { |
| 0 | 272 | | parsedArgs.BlobContainer = args[index + 1].Trim(); |
| | 273 | | } |
| | 274 | | } |
| | 275 | |
|
| 0 | 276 | | return parsedArgs; |
| | 277 | | } |
| | 278 | |
|
| | 279 | | /// <summary> |
| | 280 | | /// Locates the samples within the solution and creates an instance |
| | 281 | | /// that can be inspected and run. |
| | 282 | | /// </summary> |
| | 283 | | /// |
| | 284 | | /// <returns>The set of samples defined in the solution.</returns> |
| | 285 | | /// |
| | 286 | | private static IReadOnlyList<IEventHubsBlobCheckpointSample> LocateSamples() => |
| 0 | 287 | | typeof(Program) |
| 0 | 288 | | .Assembly |
| 0 | 289 | | .ExportedTypes |
| 0 | 290 | | .Where(type => (type.IsClass && typeof(IEventHubsBlobCheckpointSample).IsAssignableFrom(type))) |
| 0 | 291 | | .Select(type => (IEventHubsBlobCheckpointSample)Activator.CreateInstance(type)) |
| 0 | 292 | | .ToList(); |
| | 293 | |
|
| | 294 | | /// <summary> |
| | 295 | | /// Provides a local means of collecting and passing |
| | 296 | | /// the command line arguments received. |
| | 297 | | /// </summary> |
| | 298 | | /// |
| | 299 | | private class CommandLineArguments |
| | 300 | | { |
| | 301 | | /// <summary>The sequence of characters that prefix a command-line argument.</summary> |
| | 302 | | public const string ArgumentPrefix = "--"; |
| | 303 | |
|
| | 304 | | /// <summary>The connection string to the Azure Event Hubs namespace for samples.</summary> |
| | 305 | | public string EventHubsConnectionString; |
| | 306 | |
|
| | 307 | | /// <summary>The name of the Event Hub to use samples.</summary> |
| | 308 | | public string EventHub; |
| | 309 | |
|
| | 310 | | /// <summary>The connection string to the Azure storage account for samples.</summary> |
| | 311 | | public string StorageConnectionString; |
| | 312 | |
|
| | 313 | | /// <summary>The name of the blob container to use samples.</summary> |
| | 314 | | public string BlobContainer; |
| | 315 | |
|
| | 316 | | /// <summary>A flag indicating whether or not help was requested.</summary> |
| | 317 | | public bool Help; |
| | 318 | | } |
| | 319 | | } |
| | 320 | | } |