-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from martijnvanschie/publish-folder
Implemented publishing of events from a folder
- Loading branch information
Showing
21 changed files
with
379 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Ignore Visual Studio temporary files, build results, and | ||
## files generated by popular Visual Studio add-ons. | ||
|
||
# Local files | ||
.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
cli/EventGrid.Publisher.ConsoleApp/Commands/SendFileCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using EventGrid.Publisher.ConsoleApp.Utils; | ||
using Reduct.Azure.EventGrid; | ||
using Spectre.Console; | ||
using System.CommandLine; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp.Commands | ||
{ | ||
internal class SendFileCommand : Command | ||
{ | ||
internal SendFileCommand(string? description = null) : base("file", description) | ||
{ | ||
AddOptions(); | ||
} | ||
|
||
private void AddOptions() | ||
{ | ||
var topicName = new Option<string>("--topic", "The name of the topic where the event should be sennd to."); | ||
topicName.AddAlias("-t"); | ||
topicName.IsRequired = true; | ||
AddOption(topicName); | ||
|
||
var region = new Option<string>("--region", () => "westeurope-1", "The region where the topic is located. Used to format the url to publish the event."); | ||
region.AddAlias("-r"); | ||
AddOption(region); | ||
|
||
var accessKey = new Option<string>("--accesskey", "The access keys used to authenticate the application to publishing events to this Azure Event Grid Topic."); | ||
accessKey.IsRequired = true; | ||
AddOption(accessKey); | ||
|
||
var filename = new Option<string>("--filename", () => "event.json", "The message that should be send to the Event Grid Topic."); | ||
filename.AddAlias("-f"); | ||
AddOption(filename); | ||
|
||
var overrideEventId = new Option<bool>("--override-eventid", () => false, "Indicates if the event id is overwritten with a new id."); | ||
overrideEventId.ArgumentHelpName = "A new guid is generated as an id."; | ||
AddOption(overrideEventId); | ||
|
||
this.SetHandler(async (string topic, string region, string accesskey, string filename, bool overrideId) => | ||
{ | ||
AnsiConsole.MarkupLine($"Publishing event to topic [cyan1]{topic}[/]."); | ||
AnsiConsole.MarkupLine($"Reading file [cyan1]{filename}[/]."); | ||
var file = new FileInfo(filename); | ||
await EventSender.SendFileToEventGridAsync(file, topic, accesskey, region, overrideId); | ||
}, topicName, region, accessKey, filename, overrideEventId); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
cli/EventGrid.Publisher.ConsoleApp/Commands/SendFolderCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using EventGrid.Publisher.ConsoleApp.Options; | ||
using EventGrid.Publisher.ConsoleApp.Utils; | ||
using Reduct.Azure.EventGrid; | ||
using Spectre.Console; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp.Commands | ||
{ | ||
internal class SendFolderCommand : Command | ||
{ | ||
internal SendFolderCommand(string? description = null) : base("folder", description) | ||
{ | ||
AddOptions(); | ||
} | ||
|
||
private void AddOptions() | ||
{ | ||
var topicName = new Option<string>("--topic", "The name of the topic where the event should be sennd to."); | ||
topicName.AddAlias("-t"); | ||
topicName.IsRequired = true; | ||
AddOption(topicName); | ||
|
||
|
||
var accessKey = new Option<string>("--accesskey", "The access keys used to authenticate the application to publishing events to this Azure Event Grid Topic."); | ||
accessKey.IsRequired = true; | ||
AddOption(accessKey); | ||
|
||
var folder = new Option<string>("--folder", () => "events/", "The folder containing the messages that should be send to the Event Grid Topic."); | ||
folder.AddAlias("-f"); | ||
AddOption(folder); | ||
|
||
var pattern = new Option<string>("--search-pattern", () => "*.json", "The search string to match against the names of files."); | ||
AddOption(pattern); | ||
|
||
var overrideEventId = new Option<bool>("--override-eventid", () => false, "Indicates if the event id is overwritten with a new id."); | ||
overrideEventId.ArgumentHelpName = "A new guid is generated as an id."; | ||
AddOption(overrideEventId); | ||
|
||
var region = new Option<string>("--region", () => "westeurope-1", "The region where the topic is located. Used to format the url to publish the event."); | ||
region.AddAlias("-r"); | ||
AddOption(region); | ||
|
||
this.SetHandler(async (string topic, string region, string accesskey, string folder, bool overrideId, string pattern) => | ||
{ | ||
AnsiConsole.MarkupLine($"Publishing events from folder [[{folder}]] to topic [cyan1]{topic}[/]."); | ||
DirectoryInfo di = new DirectoryInfo(folder); | ||
if (!di.Exists) | ||
{ | ||
AnsiConsole.MarkupLine($"[{ConsoleColors.Warning}]The message folder [[{di.FullName}]] does not exist.[/]."); | ||
await Task.Delay(100); | ||
return; | ||
} | ||
AnsiConsole.MarkupLine($"Searching folder [cyan1]{di.FullName}[/]."); | ||
var files = di.GetFiles(pattern); | ||
foreach (var file in files) | ||
{ | ||
await EventSender.SendFileToEventGridAsync(file, topic, accesskey, region, overrideId); | ||
} | ||
}, topicName, region, accessKey, folder, overrideEventId, pattern); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
cli/EventGrid.Publisher.ConsoleApp/Commands/SendRootCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.CommandLine; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp.Commands | ||
{ | ||
internal class SendRootCommand : Command | ||
{ | ||
internal SendRootCommand() : base("send", "Send events to the event grid.") | ||
{ | ||
this.AddCommand(new SendFileCommand("Send messages, based on a file, to the event grid.")); | ||
this.AddCommand(new SendFolderCommand("Send all messages from a folder to the event grid.")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp | ||
{ | ||
internal class ConsoleColors | ||
{ | ||
internal static string Warning = "gold3_1"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp.Options | ||
{ | ||
internal class TopicOption : Option | ||
{ | ||
public TopicOption(Type? argumentType = null, Func<object?>? getDefaultValue = null, ArgumentArity arity = default) : | ||
base("--topic", "The name of the topic where the event should be sennd to.", argumentType, getDefaultValue, arity) | ||
{ | ||
var topicName = new Option<string>("--topic", "The name of the topic where the event should be sennd to."); | ||
AddAlias("-t"); | ||
IsRequired = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Reduct.Azure.EventGrid; | ||
using Spectre.Console; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EventGrid.Publisher.ConsoleApp.Utils | ||
{ | ||
internal class EventSender | ||
{ | ||
internal static async Task SendFileToEventGridAsync(FileInfo file, string topic, string accesskey, string region = "westeurope-1", bool overrideId = false) | ||
{ | ||
try | ||
{ | ||
AnsiConsole.MarkupLine($"File found [cyan1]{file.FullName}[/]."); | ||
var binary = FileReader.ReadFile(file.FullName); | ||
string? id = overrideId ? Guid.NewGuid().ToString() : null; | ||
EventPublisher publisher = new EventPublisher(topic, region, accesskey); | ||
|
||
AnsiConsole.MarkupLine($"Publishing message."); | ||
await publisher.PublishBinaryDataAsync(binary, id); | ||
} | ||
catch (ArgumentNullException ex) | ||
{ | ||
AnsiConsole.MarkupLine($"[{ConsoleColors.Warning}]File [[{file.FullName}]] containts invalid event message. [[{ex.Message}]][/]."); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
AnsiConsole.MarkupLine($"[{ConsoleColors.Warning}]Unable to process file [[{file.FullName}]]. [[{ex.Message}]][/]."); | ||
} | ||
finally | ||
{ | ||
AnsiConsole.MarkupLine($""); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.