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;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Shamir.Abstractions
|
namespace Shamir.Abstractions
|
||||||
{
|
{
|
||||||
|
@ -22,7 +23,7 @@ namespace Shamir.Abstractions
|
||||||
|
|
||||||
public ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider)
|
public ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(GetHelpText());
|
serviceProvider.GetRequiredService<IConsole>().Error.WriteLine(GetHelpText());
|
||||||
return ValueTask.FromResult(1); // TODO: const
|
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);
|
var helpText = HelpText.AutoBuild(result);
|
||||||
helpText.Heading = string.Empty;
|
helpText.Heading = string.Empty;
|
||||||
helpText.Copyright = string.Empty;
|
helpText.Copyright = string.Empty;
|
||||||
Console.Error.WriteLine(helpText);
|
serviceProvider.GetRequiredService<IConsole>().Error.WriteLine(helpText);
|
||||||
return ValueTask.FromResult(1);
|
return ValueTask.FromResult(1);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,6 +7,7 @@ using Azure.Storage.Blobs.Models;
|
||||||
using Azure.Storage.Sas;
|
using Azure.Storage.Sas;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
using Microsoft.Azure.Storage;
|
using Microsoft.Azure.Storage;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Shamir.Abstractions;
|
using Shamir.Abstractions;
|
||||||
|
|
||||||
namespace Shamir.Commands.Azure
|
namespace Shamir.Commands.Azure
|
||||||
|
@ -67,7 +68,7 @@ namespace Shamir.Commands.Azure
|
||||||
uri.Query = parameters.ToString();
|
uri.Query = parameters.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine(uri.Uri.AbsoluteUri);
|
serviceProvider.GetRequiredService<IConsole>().Output.WriteLine(uri.Uri.AbsoluteUri);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Azure.Storage.Blobs;
|
using Azure.Storage.Blobs;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Shamir.Abstractions;
|
using Shamir.Abstractions;
|
||||||
|
|
||||||
namespace Shamir.Commands.Azure
|
namespace Shamir.Commands.Azure
|
||||||
|
@ -27,6 +28,7 @@ namespace Shamir.Commands.Azure
|
||||||
public override async ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider, StorageLsOptions options)
|
public override async ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider, StorageLsOptions options)
|
||||||
{
|
{
|
||||||
var connectionString = options.ConnectionString ?? Environment.GetEnvironmentVariable("AZURE_CONNECTION_STRING");
|
var connectionString = options.ConnectionString ?? Environment.GetEnvironmentVariable("AZURE_CONNECTION_STRING");
|
||||||
|
var console = serviceProvider.GetRequiredService<IConsole>();
|
||||||
|
|
||||||
if (options.Path is null)
|
if (options.Path is null)
|
||||||
{
|
{
|
||||||
|
@ -39,9 +41,9 @@ namespace Shamir.Commands.Azure
|
||||||
|
|
||||||
await foreach (var blob in containerClient.GetBlobsAsync())
|
await foreach (var blob in containerClient.GetBlobsAsync())
|
||||||
{
|
{
|
||||||
Console.Write(container.Name);
|
console.Output.Write(container.Name);
|
||||||
Console.Write('/');
|
console.Output.Write('/');
|
||||||
Console.WriteLine(blob.Name);
|
console.Output.WriteLine(blob.Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +52,7 @@ namespace Shamir.Commands.Azure
|
||||||
var client = new BlobServiceClient(connectionString);
|
var client = new BlobServiceClient(connectionString);
|
||||||
await foreach (var container in client.GetBlobContainersAsync())
|
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())
|
await foreach (var blob in client.GetBlobsAsync())
|
||||||
{
|
{
|
||||||
Console.Write(containerName);
|
console.Output.Write(containerName);
|
||||||
Console.Write('/');
|
console.Output.Write('/');
|
||||||
Console.WriteLine(blob.Name);
|
console.Output.WriteLine(blob.Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -76,16 +78,16 @@ namespace Shamir.Commands.Azure
|
||||||
var client = new BlobContainerClient(connectionString, containerName);
|
var client = new BlobContainerClient(connectionString, containerName);
|
||||||
await foreach (var blob in client.GetBlobsByHierarchyAsync(default, default, delimiter: "/", prefix))
|
await foreach (var blob in client.GetBlobsByHierarchyAsync(default, default, delimiter: "/", prefix))
|
||||||
{
|
{
|
||||||
Console.Write(containerName);
|
console.Output.Write(containerName);
|
||||||
Console.Write('/');
|
console.Output.Write('/');
|
||||||
|
|
||||||
if (blob.IsPrefix)
|
if (blob.IsPrefix)
|
||||||
{
|
{
|
||||||
Console.WriteLine(blob.Prefix);
|
console.Output.WriteLine(blob.Prefix);
|
||||||
}
|
}
|
||||||
else if (blob.IsBlob)
|
else if (blob.IsBlob)
|
||||||
{
|
{
|
||||||
Console.WriteLine(blob.Blob.Name);
|
console.Output.WriteLine(blob.Blob.Name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Net.Http;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Shamir.Abstractions;
|
using Shamir.Abstractions;
|
||||||
|
|
||||||
namespace Shamir.Commands.Radio
|
namespace Shamir.Commands.Radio
|
||||||
|
@ -25,6 +26,7 @@ namespace Shamir.Commands.Radio
|
||||||
{
|
{
|
||||||
Debug.Assert(options.Callsign != null, "Callsign should be populated.");
|
Debug.Assert(options.Callsign != null, "Callsign should be populated.");
|
||||||
|
|
||||||
|
var console = serviceProvider.GetRequiredService<IConsole>();
|
||||||
using var client = new HttpClient();
|
using var client = new HttpClient();
|
||||||
|
|
||||||
// API courtesy of VK3FUR: https://vklookup.info
|
// API courtesy of VK3FUR: https://vklookup.info
|
||||||
|
@ -35,7 +37,7 @@ namespace Shamir.Commands.Radio
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{options.Callsign}: No such callsign found.");
|
console.Error.WriteLine($"{options.Callsign}: No such callsign found.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,32 +48,32 @@ namespace Shamir.Commands.Radio
|
||||||
|
|
||||||
if (json.RootElement.TryGetProperty("callsign", out var callsign))
|
if (json.RootElement.TryGetProperty("callsign", out var callsign))
|
||||||
{
|
{
|
||||||
Console.Write("Callsign : ");
|
console.Output.Write("Callsign : ");
|
||||||
Console.WriteLine(callsign.GetString());
|
console.Output.WriteLine(callsign.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.RootElement.TryGetProperty("name", out var name))
|
if (json.RootElement.TryGetProperty("name", out var name))
|
||||||
{
|
{
|
||||||
Console.Write("Name : ");
|
console.Output.Write("Name : ");
|
||||||
Console.WriteLine(name.GetString());
|
console.Output.WriteLine(name.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.RootElement.TryGetProperty("suburb", out var suburb))
|
if (json.RootElement.TryGetProperty("suburb", out var suburb))
|
||||||
{
|
{
|
||||||
Console.Write("Suburb : ");
|
console.Output.Write("Suburb : ");
|
||||||
Console.WriteLine(suburb.GetString());
|
console.Output.WriteLine(suburb.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.RootElement.TryGetProperty("state", out var state))
|
if (json.RootElement.TryGetProperty("state", out var state))
|
||||||
{
|
{
|
||||||
Console.Write("State : ");
|
console.Output.Write("State : ");
|
||||||
Console.WriteLine(state.GetString());
|
console.Output.WriteLine(state.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.RootElement.TryGetProperty("link", out var link))
|
if (json.RootElement.TryGetProperty("link", out var link))
|
||||||
{
|
{
|
||||||
Console.Write("Link : ");
|
console.Output.Write("Link : ");
|
||||||
Console.WriteLine(link.GetString());
|
console.Output.WriteLine(link.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Shamir.Abstractions;
|
using Shamir.Abstractions;
|
||||||
using SteamKit2;
|
using SteamKit2;
|
||||||
|
|
||||||
|
@ -22,10 +23,12 @@ namespace Shamir.Commands.Radio
|
||||||
{
|
{
|
||||||
var gid = new GlobalID(options.Gid);
|
var gid = new GlobalID(options.Gid);
|
||||||
|
|
||||||
Console.WriteLine($"Box ID : {gid.BoxID}");
|
var console = serviceProvider.GetRequiredService<IConsole>();
|
||||||
Console.WriteLine($"Process ID : {gid.ProcessID}");
|
|
||||||
Console.WriteLine($"Process Start Time : {gid.StartTime}");
|
console.Output.WriteLine($"Box ID : {gid.BoxID}");
|
||||||
Console.WriteLine($"Sequence : {gid.SequentialCount}");
|
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);
|
return ValueTask.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ namespace Shamir.Console
|
||||||
with.EnableDashDash = true;
|
with.EnableDashDash = true;
|
||||||
with.IgnoreUnknownArguments = false;
|
with.IgnoreUnknownArguments = false;
|
||||||
}))
|
}))
|
||||||
|
.AddSingleton<IConsole, SystemConsole>()
|
||||||
.AddAzureCommandTree()
|
.AddAzureCommandTree()
|
||||||
.AddRadioCommandTree()
|
.AddRadioCommandTree()
|
||||||
.AddSteamCommandTree()
|
.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