diff --git a/src/abstractions/CommandTree/DefaultHelpTextCommand.cs b/src/abstractions/CommandTree/DefaultHelpTextCommand.cs index 7b85e26..6358d16 100644 --- a/src/abstractions/CommandTree/DefaultHelpTextCommand.cs +++ b/src/abstractions/CommandTree/DefaultHelpTextCommand.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Immutable; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; namespace Shamir.Abstractions { @@ -22,7 +23,7 @@ namespace Shamir.Abstractions public ValueTask ExecuteAsync(IServiceProvider serviceProvider) { - Console.Error.WriteLine(GetHelpText()); + serviceProvider.GetRequiredService().Error.WriteLine(GetHelpText()); return ValueTask.FromResult(1); // TODO: const } diff --git a/src/abstractions/IConsole.cs b/src/abstractions/IConsole.cs new file mode 100644 index 0000000..e110817 --- /dev/null +++ b/src/abstractions/IConsole.cs @@ -0,0 +1,11 @@ +using System.IO; + +namespace Shamir.Abstractions +{ + public interface IConsole + { + TextWriter Output { get; } + TextWriter Error { get; } + Stream OpenInput(); + } +} \ No newline at end of file diff --git a/src/abstractions/ParsedArgumentsCommand.cs b/src/abstractions/ParsedArgumentsCommand.cs index 2218289..3c8e6f0 100644 --- a/src/abstractions/ParsedArgumentsCommand.cs +++ b/src/abstractions/ParsedArgumentsCommand.cs @@ -29,7 +29,7 @@ namespace Shamir.Abstractions var helpText = HelpText.AutoBuild(result); helpText.Heading = string.Empty; helpText.Copyright = string.Empty; - Console.Error.WriteLine(helpText); + serviceProvider.GetRequiredService().Error.WriteLine(helpText); return ValueTask.FromResult(1); } ); diff --git a/src/commands/azure/StorageGetUrlCommand.cs b/src/commands/azure/StorageGetUrlCommand.cs index d7c86ae..3b0fc10 100644 --- a/src/commands/azure/StorageGetUrlCommand.cs +++ b/src/commands/azure/StorageGetUrlCommand.cs @@ -7,6 +7,7 @@ using Azure.Storage.Blobs.Models; using Azure.Storage.Sas; using CommandLine; using Microsoft.Azure.Storage; +using Microsoft.Extensions.DependencyInjection; using Shamir.Abstractions; namespace Shamir.Commands.Azure @@ -67,7 +68,7 @@ namespace Shamir.Commands.Azure uri.Query = parameters.ToString(); } - Console.WriteLine(uri.Uri.AbsoluteUri); + serviceProvider.GetRequiredService().Output.WriteLine(uri.Uri.AbsoluteUri); return 0; } diff --git a/src/commands/azure/StorageLsCommand.cs b/src/commands/azure/StorageLsCommand.cs index bc3248f..8ac3cd1 100644 --- a/src/commands/azure/StorageLsCommand.cs +++ b/src/commands/azure/StorageLsCommand.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using Azure.Storage.Blobs; using CommandLine; +using Microsoft.Extensions.DependencyInjection; using Shamir.Abstractions; namespace Shamir.Commands.Azure @@ -27,6 +28,7 @@ namespace Shamir.Commands.Azure public override async ValueTask ExecuteAsync(IServiceProvider serviceProvider, StorageLsOptions options) { var connectionString = options.ConnectionString ?? Environment.GetEnvironmentVariable("AZURE_CONNECTION_STRING"); + var console = serviceProvider.GetRequiredService(); if (options.Path is null) { @@ -39,9 +41,9 @@ namespace Shamir.Commands.Azure await foreach (var blob in containerClient.GetBlobsAsync()) { - Console.Write(container.Name); - Console.Write('/'); - Console.WriteLine(blob.Name); + console.Output.Write(container.Name); + console.Output.Write('/'); + console.Output.WriteLine(blob.Name); } } } @@ -50,7 +52,7 @@ namespace Shamir.Commands.Azure var client = new BlobServiceClient(connectionString); await foreach (var container in client.GetBlobContainersAsync()) { - Console.WriteLine(container.Name); + console.Output.WriteLine(container.Name); } } } @@ -61,9 +63,9 @@ namespace Shamir.Commands.Azure await foreach (var blob in client.GetBlobsAsync()) { - Console.Write(containerName); - Console.Write('/'); - Console.WriteLine(blob.Name); + console.Output.Write(containerName); + console.Output.Write('/'); + console.Output.WriteLine(blob.Name); } } else @@ -76,16 +78,16 @@ namespace Shamir.Commands.Azure var client = new BlobContainerClient(connectionString, containerName); await foreach (var blob in client.GetBlobsByHierarchyAsync(default, default, delimiter: "/", prefix)) { - Console.Write(containerName); - Console.Write('/'); + console.Output.Write(containerName); + console.Output.Write('/'); if (blob.IsPrefix) { - Console.WriteLine(blob.Prefix); + console.Output.WriteLine(blob.Prefix); } else if (blob.IsBlob) { - Console.WriteLine(blob.Blob.Name); + console.Output.WriteLine(blob.Blob.Name); } else { diff --git a/src/commands/radio/VKLookupCommand.cs b/src/commands/radio/VKLookupCommand.cs index 0cc7d6b..aa42c34 100644 --- a/src/commands/radio/VKLookupCommand.cs +++ b/src/commands/radio/VKLookupCommand.cs @@ -5,6 +5,7 @@ using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using CommandLine; +using Microsoft.Extensions.DependencyInjection; using Shamir.Abstractions; namespace Shamir.Commands.Radio @@ -25,6 +26,7 @@ namespace Shamir.Commands.Radio { Debug.Assert(options.Callsign != null, "Callsign should be populated."); + var console = serviceProvider.GetRequiredService(); using var client = new HttpClient(); // API courtesy of VK3FUR: https://vklookup.info @@ -35,7 +37,7 @@ namespace Shamir.Commands.Radio if (response.StatusCode == HttpStatusCode.NotFound) { - Console.WriteLine($"{options.Callsign}: No such callsign found."); + console.Error.WriteLine($"{options.Callsign}: No such callsign found."); return 1; } @@ -46,32 +48,32 @@ namespace Shamir.Commands.Radio if (json.RootElement.TryGetProperty("callsign", out var callsign)) { - Console.Write("Callsign : "); - Console.WriteLine(callsign.GetString()); + console.Output.Write("Callsign : "); + console.Output.WriteLine(callsign.GetString()); } if (json.RootElement.TryGetProperty("name", out var name)) { - Console.Write("Name : "); - Console.WriteLine(name.GetString()); + console.Output.Write("Name : "); + console.Output.WriteLine(name.GetString()); } if (json.RootElement.TryGetProperty("suburb", out var suburb)) { - Console.Write("Suburb : "); - Console.WriteLine(suburb.GetString()); + console.Output.Write("Suburb : "); + console.Output.WriteLine(suburb.GetString()); } if (json.RootElement.TryGetProperty("state", out var state)) { - Console.Write("State : "); - Console.WriteLine(state.GetString()); + console.Output.Write("State : "); + console.Output.WriteLine(state.GetString()); } if (json.RootElement.TryGetProperty("link", out var link)) { - Console.Write("Link : "); - Console.WriteLine(link.GetString()); + console.Output.Write("Link : "); + console.Output.WriteLine(link.GetString()); } return 0; diff --git a/src/commands/steam/DescribeGidCommand.cs b/src/commands/steam/DescribeGidCommand.cs index c579ec8..6f39e50 100644 --- a/src/commands/steam/DescribeGidCommand.cs +++ b/src/commands/steam/DescribeGidCommand.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using CommandLine; +using Microsoft.Extensions.DependencyInjection; using Shamir.Abstractions; using SteamKit2; @@ -22,10 +23,12 @@ namespace Shamir.Commands.Radio { var gid = new GlobalID(options.Gid); - Console.WriteLine($"Box ID : {gid.BoxID}"); - Console.WriteLine($"Process ID : {gid.ProcessID}"); - Console.WriteLine($"Process Start Time : {gid.StartTime}"); - Console.WriteLine($"Sequence : {gid.SequentialCount}"); + var console = serviceProvider.GetRequiredService(); + + console.Output.WriteLine($"Box ID : {gid.BoxID}"); + console.Output.WriteLine($"Process ID : {gid.ProcessID}"); + console.Output.WriteLine($"Process Start Time : {gid.StartTime}"); + console.Output.WriteLine($"Sequence : {gid.SequentialCount}"); return ValueTask.FromResult(0); } diff --git a/src/console/Program.cs b/src/console/Program.cs index f36fea5..99f608e 100644 --- a/src/console/Program.cs +++ b/src/console/Program.cs @@ -21,6 +21,7 @@ namespace Shamir.Console with.EnableDashDash = true; with.IgnoreUnknownArguments = false; })) + .AddSingleton() .AddAzureCommandTree() .AddRadioCommandTree() .AddSteamCommandTree() diff --git a/src/console/SystemConsole.cs b/src/console/SystemConsole.cs new file mode 100644 index 0000000..8e1763c --- /dev/null +++ b/src/console/SystemConsole.cs @@ -0,0 +1,14 @@ +using System.IO; +using Shamir.Abstractions; + +namespace Shamir.Console +{ + public sealed class SystemConsole : IConsole + { + public TextWriter Output => System.Console.Out; + + public TextWriter Error => System.Console.Error; + + public Stream OpenInput() => System.Console.OpenStandardInput(); + } +} \ No newline at end of file