< Summary

Class:Azure.Messaging.EventHubs.Processor.Samples.Program
Assembly:Azure.Messaging.EventHubs.Processor.Samples
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Processor\samples\Program.cs
Covered lines:0
Uncovered lines:125
Coverable lines:125
Total lines:320
Line coverage:0% (0 of 125)
Covered branches:0
Total branches:46
Branch coverage:0% (0 of 46)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Main()-0%0%
DisplayHelp()-0%0%
ReadSelection(...)-0%0%
ParseArguments(...)-0%0%
LocateSamples()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Processor\samples\Program.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Threading.Tasks;
 8using Azure.Messaging.EventHubs.Processor.Samples.Infrastructure;
 9
 10namespace 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
 029            CommandLineArguments parsedArgs = ParseArguments(args);
 30
 031            if (parsedArgs.Help)
 32            {
 033                DisplayHelp();
 034                return;
 35            }
 36
 37            // Display the welcome message.
 38
 039            Console.WriteLine();
 040            Console.WriteLine("=============================================================================");
 041            Console.WriteLine("Welcome to the Event Hubs Checkpoint Storage client library for Blob Storage!");
 042            Console.WriteLine("=============================================================================");
 043            Console.WriteLine();
 44
 45            // Prompt for the Event Hubs connection string, if it wasn't passed.
 46
 047            while (string.IsNullOrEmpty(parsedArgs.EventHubsConnectionString))
 48            {
 049                Console.Write("Please provide the connection string for the Event Hubs namespace that you'd like to use 
 050                parsedArgs.EventHubsConnectionString = Console.ReadLine().Trim();
 051                Console.WriteLine();
 52            }
 53
 54            // Prompt for the Event Hub name, if it wasn't passed.
 55
 056            while (string.IsNullOrEmpty(parsedArgs.EventHub))
 57            {
 058                Console.Write("Please provide the name of the Event Hub that you'd like to use and then press Enter: ");
 059                parsedArgs.EventHub = Console.ReadLine().Trim();
 060                Console.WriteLine();
 61            }
 62
 63            // Prompt for the storage connection string, if it wasn't passed.
 64
 065            while (string.IsNullOrEmpty(parsedArgs.StorageConnectionString))
 66            {
 067                Console.Write("Please provide the connection string for the Azure storage account that you'd like to use
 068                parsedArgs.StorageConnectionString = Console.ReadLine().Trim();
 069                Console.WriteLine();
 70            }
 71
 72            // Prompt for the blob container name, if it wasn't passed.
 73
 074            while (string.IsNullOrEmpty(parsedArgs.BlobContainer))
 75            {
 076                Console.Write("Please provide the name of the blob container that you'd like to use and then press Enter
 077                parsedArgs.BlobContainer = Console.ReadLine().Trim();
 078                Console.WriteLine();
 79            }
 80
 81            // Display the set of available samples and allow them to be run.
 82
 083            IReadOnlyList<IEventHubsBlobCheckpointSample> samples = LocateSamples();
 84
 085            Console.WriteLine();
 086            Console.WriteLine("Available Samples:");
 087            Console.WriteLine();
 88
 089            for (var index = 0; index < samples.Count; ++index)
 90            {
 091                Console.WriteLine($"{ index + 1 }) { samples[index].Name }");
 092                Console.WriteLine($"\t{ samples[index].Description }");
 093                Console.WriteLine();
 94            }
 95
 096            Console.WriteLine();
 097            var choice = ReadSelection(samples.Count);
 98
 099            if (choice == null)
 100            {
 0101                Console.WriteLine();
 0102                Console.WriteLine();
 0103                Console.WriteLine("Quitting...");
 0104                Console.WriteLine();
 0105                return;
 106            }
 107            else
 108            {
 0109                Console.WriteLine();
 0110                Console.WriteLine();
 0111                Console.WriteLine();
 0112                Console.WriteLine("-------------------------------------------------------------------------");
 0113                Console.WriteLine($"Running: { samples[choice.Value].Name }");
 0114                Console.WriteLine("-------------------------------------------------------------------------");
 0115                Console.WriteLine();
 116
 0117                await samples[choice.Value].RunAsync(parsedArgs.EventHubsConnectionString, parsedArgs.EventHub, parsedAr
 0118                return;
 119            }
 0120        }
 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        {
 0129            var eventHubsSample = "Endpoint=sb://fake.servicebus.windows.net/;SharedAccessKeyName=NotReal;SharedAccessKe
 0130            var storageSample = "DefaultEndpointsProtocol=https;AccountName=NotReal;AccountKey=[FAKE];EndpointSuffix=cor
 131
 0132            Console.WriteLine();
 0133            Console.WriteLine($"{ typeof(Program).Namespace }");
 0134            Console.WriteLine();
 0135            Console.WriteLine("This executable allows for running the Azure Event Hubs Blob Checkpoint Store library sam
 0136            Console.WriteLine("the samples run against live Azure services, they require an Event Hubs namespace, an Eve
 0137            Console.WriteLine("Azure storage account, and a blob storage container to run.");
 0138            Console.WriteLine();
 0139            Console.WriteLine();
 0140            Console.WriteLine("Arguments:");
 0141            Console.WriteLine($"\t{ nameof(CommandLineArguments.Help) }:");
 0142            Console.WriteLine("\t\tDisplays this message.");
 0143            Console.WriteLine();
 0144            Console.WriteLine($"\t{ nameof(CommandLineArguments.EventHubsConnectionString) }:");
 0145            Console.WriteLine("\t\tThe connection string to the Event Hubs namespace to use for the samples.");
 0146            Console.WriteLine();
 0147            Console.WriteLine($"\t{ nameof(CommandLineArguments.EventHub) }:");
 0148            Console.WriteLine("\t\tThe name of the Event Hub under the namespace to use.");
 0149            Console.WriteLine();
 0150            Console.WriteLine($"\t{ nameof(CommandLineArguments.StorageConnectionString) }:");
 0151            Console.WriteLine("\t\tThe connection string to the Azure storage account to use for the samples.");
 0152            Console.WriteLine();
 0153            Console.WriteLine($"\t{ nameof(CommandLineArguments.BlobContainer) }:");
 0154            Console.WriteLine("\t\tThe name of the blob container under the storage account to use for checkpoints.");
 0155            Console.WriteLine();
 0156            Console.WriteLine("Usage:");
 0157            Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe");
 0158            Console.WriteLine();
 0159            Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe { CommandLineArguments.ArgumentPrefix 
 0160            Console.WriteLine();
 0161            Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe \"{ eventHubsSample}\" \"SomeHub\" \"{
 0162            Console.WriteLine();
 0163            Console.WriteLine($"\tAzure.Messaging.EventHubs.Processor.Samples.exe { CommandLineArguments.ArgumentPrefix 
 0164            Console.WriteLine();
 0165        }
 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            {
 0180                Console.Write("Please enter the number of a sample to run or press \"X\" to exit: ");
 181
 0182                var value = Console.ReadLine();
 183
 0184                if (string.Equals(value, "X", StringComparison.OrdinalIgnoreCase))
 185                {
 0186                    return null;
 187                }
 188
 0189                if (int.TryParse(value, out var choice))
 190                {
 0191                    --choice;
 192
 0193                    if ((choice >= 0) && (choice < sampleCount))
 194                    {
 0195                        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
 0214            if ((args.Length >= 4) && (!args[0].StartsWith(CommandLineArguments.ArgumentPrefix)) && (!args[1].StartsWith
 215            {
 0216                return new CommandLineArguments
 0217                {
 0218                    EventHubsConnectionString = args[0],
 0219                    EventHub = args[1],
 0220                    StorageConnectionString = args[2],
 0221                    BlobContainer = args[3]
 0222                };
 223            }
 224
 0225            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
 0232            for (var index = 0; index < args.Length - 1; ++index)
 233            {
 234                // Remove any excess spaces to comparison purposes.
 235
 0236                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
 0241                if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.Help) }", 
 242                {
 0243                    parsedArgs.Help = true;
 0244                    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
 0250                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
 0258                if (args[index].Equals($"{ CommandLineArguments.ArgumentPrefix }{ nameof(CommandLineArguments.EventHubsC
 259                {
 0260                    parsedArgs.EventHubsConnectionString = args[index + 1].Trim();
 261                }
 0262                else if (args[index].Equals($"--{ nameof(CommandLineArguments.EventHub) }", StringComparison.OrdinalIgno
 263                {
 0264                    parsedArgs.EventHub = args[index + 1].Trim();
 265                }
 0266                else if (args[index].Equals($"--{ nameof(CommandLineArguments.StorageConnectionString) }", StringCompari
 267                {
 0268                    parsedArgs.StorageConnectionString = args[index + 1].Trim();
 269                }
 0270                else if (args[index].Equals($"--{ nameof(CommandLineArguments.BlobContainer) }", StringComparison.Ordina
 271                {
 0272                    parsedArgs.BlobContainer = args[index + 1].Trim();
 273                }
 274            }
 275
 0276            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() =>
 0287            typeof(Program)
 0288              .Assembly
 0289              .ExportedTypes
 0290              .Where(type => (type.IsClass && typeof(IEventHubsBlobCheckpointSample).IsAssignableFrom(type)))
 0291              .Select(type => (IEventHubsBlobCheckpointSample)Activator.CreateInstance(type))
 0292              .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}