mirror of
https://github.com/yaakov-h/Shamir.git
synced 2025-01-18 08:46:33 +00:00
Abstract console access away behind an interface
This commit is contained in:
parent
5d72217e61
commit
034545df83
9 changed files with 64 additions and 29 deletions
|
@ -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<int> ExecuteAsync(IServiceProvider serviceProvider)
|
||||
{
|
||||
Console.Error.WriteLine(GetHelpText());
|
||||
serviceProvider.GetRequiredService<IConsole>().Error.WriteLine(GetHelpText());
|
||||
return ValueTask.FromResult(1); // TODO: const
|
||||
}
|
||||
|
||||
|
|
11
src/abstractions/IConsole.cs
Normal file
11
src/abstractions/IConsole.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System.IO;
|
||||
|
||||
namespace Shamir.Abstractions
|
||||
{
|
||||
public interface IConsole
|
||||
{
|
||||
TextWriter Output { get; }
|
||||
TextWriter Error { get; }
|
||||
Stream OpenInput();
|
||||
}
|
||||
}
|
|
@ -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<IConsole>().Error.WriteLine(helpText);
|
||||
return ValueTask.FromResult(1);
|
||||
}
|
||||
);
|
||||
|
|
|
@ -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<IConsole>().Output.WriteLine(uri.Uri.AbsoluteUri);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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<int> ExecuteAsync(IServiceProvider serviceProvider, StorageLsOptions options)
|
||||
{
|
||||
var connectionString = options.ConnectionString ?? Environment.GetEnvironmentVariable("AZURE_CONNECTION_STRING");
|
||||
var console = serviceProvider.GetRequiredService<IConsole>();
|
||||
|
||||
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
|
||||
{
|
||||
|
|
|
@ -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<IConsole>();
|
||||
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;
|
||||
|
|
|
@ -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<IConsole>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ namespace Shamir.Console
|
|||
with.EnableDashDash = true;
|
||||
with.IgnoreUnknownArguments = false;
|
||||
}))
|
||||
.AddSingleton<IConsole, SystemConsole>()
|
||||
.AddAzureCommandTree()
|
||||
.AddRadioCommandTree()
|
||||
.AddSteamCommandTree()
|
||||
|
|
14
src/console/SystemConsole.cs
Normal file
14
src/console/SystemConsole.cs
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue