private async Task OnExecuteAsync()
        {
            var fullPath = Path.GetFullPath(OutputFilePath);

            var getRangeResult = RangeParser.TryGetRange(Last, From, To, out var range);

            if (!string.IsNullOrEmpty(getRangeResult?.ErrorMessage))
            {
                ConsoleHelper.WriteError(getRangeResult.ErrorMessage);
                return;
            }

            var sas = Prompt.GetPassword("Shared Access Signature:", ConsoleColor.White, ConsoleColor.DarkBlue);

            Console.CancelKeyPress += ConsoleOnCancelKeyPress;

            try
            {
                ConsoleHelper.WriteDebug(
                    $"Querying 'WADLogsTable' from storage account '{StorageAccountHelper.GetStorageAccountName(sas)}' from {range}...");

                var repository = new Repository(sas);
                var logs       = await repository.GetLogsAsync(range, Token);

                ConsoleHelper.WriteDebug($"Writing {logs.Count} log event(s)...");

                using (var outputFile = File.CreateText(fullPath))
                {
                    outputFile.WriteLine("Generated,Level,Message");
                    foreach (var log in logs)
                    {
                        outputFile.WriteLine(log);
                    }
                }

                Console.WriteLine();
                ConsoleHelper.WriteDebug("Done");
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Exception: {0}", e.GetType());
                Console.WriteLine("Message: {0}", e.Message);
                Console.WriteLine("StackTrace:");
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Console.ResetColor();
            }
        }
        public void GivenToWithoutFrom_WhenTryGetRange_ThenInvalid()
        {
            // Arrange

            const string last = null;
            const string from = null;
            const string to   = "2018-06-24";

            // Act

            var actual = RangeParser.TryGetRange(last, from, to, out _);

            // Assert

            Assert.NotNull(actual);
        }
        public void GivenNoLastAndNoFrom_WhenTryGetRange_ThenInvalid()
        {
            // Arrange

            const string last = null;
            const string from = null;
            const string to   = null;

            // Act

            var actual = RangeParser.TryGetRange(last, from, to, out _);

            // Assert

            Assert.NotNull(actual);
        }
        public void GivenFrom_WhenTryGetRange_ThenExpected()
        {
            // Arrange

            const string last = null;
            const string from = "2018-06-24";
            const string to   = null;

            // Act

            var actual = RangeParser.TryGetRange(last, from, to, out var actualRange);

            // Assert

            Assert.Null(actual);
            Assert.NotNull(actualRange);

            actualRange.From.Should().NotBe(default(DateTime));
            actualRange.To.Should().BeNull();
        }
        private async Task OnExecuteAsync()
        {
            var outputFilePath = Path.GetFullPath(OutputFilePath);

            var getRangeResult = RangeParser.TryGetRange(Last, From, To, out var range);

            if (!string.IsNullOrEmpty(getRangeResult?.ErrorMessage))
            {
                ConsoleHelper.WriteError(getRangeResult.ErrorMessage);
                return;
            }

            var sas = Prompt.GetPassword("Shared Access Signature:", ConsoleColor.White, ConsoleColor.DarkBlue);

            Console.CancelKeyPress += ConsoleOnCancelKeyPress;

            try
            {
                if (Prefix.Last() != '/')
                {
                    Prefix += "/";
                }

                ConsoleHelper.WriteDebug($"Querying storage account '{StorageAccountHelper.GetStorageAccountName(sas)}' from {range}");

                var from = range.From;
                var to   = range.To ?? DateTime.UtcNow;

                var datePrefixes = PrefixService.BuildBlobPrefixes(from, to, Prefix);

                var repository = new Repository(sas, Container);

                var blobs = new List <CloudBlockBlob>();

                foreach (var datePrefix in datePrefixes)
                {
                    blobs.AddRange(await repository.ListLogBlobsAsync(datePrefix, CancellationToken.None));
                }

                var tempDirectory = Path.Combine(Path.GetTempPath(), "wad-to-csv",
                                                 Path.GetRandomFileName().Replace(".", string.Empty));
                Directory.CreateDirectory(tempDirectory);

                var filtered = PrefixService.Filter(blobs, from, to, Prefix);

                await repository.DownloadLogBlobsAsync(filtered, tempDirectory, CancellationToken.None);

                CsvWriter.Write(from, to, tempDirectory, outputFilePath);

                Console.WriteLine();
                ConsoleHelper.WriteDebug("Done");
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Exception: {0}", e.GetType());
                Console.WriteLine("Message: {0}", e.Message);
                Console.WriteLine("StackTrace:");
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Console.ResetColor();
            }
        }