Skip to content

Instantly share code, notes, and snippets.

@Kralizek
Last active August 2, 2022 00:57
Show Gist options
  • Save Kralizek/b0b205221879dc6f39758c5c849c41cd to your computer and use it in GitHub Desktop.
Save Kralizek/b0b205221879dc6f39758c5c849c41cd to your computer and use it in GitHub Desktop.
Console application created using `System.CommandLine`
using System.CommandLine;
using Amazon;
using Amazon.S3;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var profileOption = new Option<string>("--profile", getDefaultValue: () => "default", description: "The name of the profile to use to access AWS services.");
var regionOption = new Option<string>("--region", getDefaultValue: () => RegionEndpoint.EUWest1.SystemName, description: "The region where AWS services are located.");
var bucketNameOption = new Option<string>("--bucket-name", "The name of the S3 bucket where to fetch the files from") { IsRequired = true };
var prefixOption = new Option<string>("--prefix", description: "The prefix to use when fetching items from S3", getDefaultValue: () => "feeds/");
var openSearchNodeOption = new Option<Uri>("--node", description: "The address of the OpenSearch cluster to push documents into", getDefaultValue: () => new Uri("http://localhost:9200"));
var rootCommand = new RootCommand("Fetches files from S3 and loads them into an OpenSearch cluster")
{
profileOption, regionOption,
bucketNameOption, prefixOption,
openSearchNodeOption
};
rootCommand.SetHandler(async (string profile, string region, string bucketName, string prefix, Uri node) =>
{
var obj = new
{
AWS = new
{
Profile = profile,
Region = region
},
Service1 = new
{
Bucket = bucketName,
Prefix = prefix
},
Service2 = new
{
Nodes = new[] { node }
}
};
var configuration = new ConfigurationBuilder()
.AddObject(obj)
.Build();
var services = new ServiceCollection()
.AddService3()
.AddService1(configuration.GetSection("Service1"))
.AddService2(configuration.GetSection("Service2"));
services.AddLogging(logging => logging.AddConsole());
services.AddAWSService<IAmazonS3>(configuration.GetAWSOptions());
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
var service1 = serviceProvider.GetRequiredService<IService1>();
var service2 = serviceProvider.GetRequiredService<IService2>();
var service3 = serviceProvider.GetRequiredService<IService3>();
await service2.EnsureIndexesExist(default);
await foreach (var feedName in service1.ListFeeds(default))
{
logger.LogInformation("Indexing {Feed}", feedName.FileName);
await service3.IndexFeed(feedName, default);
}
}, profileOption, regionOption, bucketNameOption, prefixOption, openSearchNodeOption);
await rootCommand.InvokeAsync(args);
class Program
{
static async Task Main(string[] args)
{
await BuildCommandLine()
.UseHost(_ => Host.CreateDefaultBuilder(), host =>
{
IConfiguration configuration = null!;
host.ConfigureAppConfiguration(builder =>
{
// Add configuration providers
configuration = builder.Build();
});
host.ConfigureServices(services =>
{
services.AddAWSService<IAmazonS3>(configuration.GetAWSOptions());
services
.AddService3()
.AddService1(configuration.GetSection("Service1"))
.AddService2(configuration.GetSection("Service2"));
});
})
.UseDefaults()
.Build()
.InvokeAsync(args);
}
static CommandLineBuilder BuildCommandLine()
{
var profileOption = new Option<string>("--profile", getDefaultValue: () => "default", description: "The name of the profile to use to access AWS services.");
var regionOption = new Option<string>("--region", getDefaultValue: () => RegionEndpoint.EUWest1.SystemName, description: "The region where AWS services are located.");
var bucketNameOption = new Option<string>("--bucket-name", "The name of the S3 bucket where to fetch the files from") { IsRequired = true };
var prefixOption = new Option<string>("--prefix", description: "The prefix to use when fetching items from S3", getDefaultValue: () => "feeds/");
var openSearchNodeOption = new Option<Uri>("--node", description: "The address of the OpenSearch cluster to push documents into", getDefaultValue: () => new Uri("http://localhost:9200"));
var rootCommand = new RootCommand("Fetches files from S3 and loads them into an OpenSearch cluster")
{
profileOption, regionOption,
bucketNameOption, prefixOption,
openSearchNodeOption
};
rootCommand.Handler = CommandHandler.Create<IService1, IService2, IService3, ILogger>(RunAsync);
return new CommandLineBuilder(rootCommand);
}
static async Task RunAsync(IService1 Service1, IService2 Service2, IService3 Service3, ILogger logger)
{
await Service2.EnsureIndexesExist(default);
await foreach (var feedName in Service1.ListFeeds(default))
{
logger.LogInformation("Indexing {Feed}", feedName.FileName);
await Service3.IndexFeed(feedName, default);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment