Skip to content

Commit

Permalink
Merge pull request #8 from martijnvanschie/publish-folder
Browse files Browse the repository at this point in the history
Implemented publishing of events from a folder
  • Loading branch information
martijnvanschie authored Mar 19, 2022
2 parents 67ee76a + de24b63 commit 015728d
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .gitignore
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
3 changes: 0 additions & 3 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# Local files
.local

# User-specific files
*.suo
*.user
Expand Down
8 changes: 7 additions & 1 deletion cli/Azure Event Grid Utilities.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32002.185
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventGrid.Publisher.ConsoleApp", "EventGrid.Publisher.ConsoleApp\EventGrid.Publisher.ConsoleApp.csproj", "{F80F7098-02E9-4C9E-B7CB-A2E3C285BDFA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventGrid.Publisher.ConsoleApp", "EventGrid.Publisher.ConsoleApp\EventGrid.Publisher.ConsoleApp.csproj", "{F80F7098-02E9-4C9E-B7CB-A2E3C285BDFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reduct.Azure.EventGrid", "Reduct.Azure.EventGrid\Reduct.Azure.EventGrid.csproj", "{05661C28-C52C-442E-B47F-E0CDCBFC0C6A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{F80F7098-02E9-4C9E-B7CB-A2E3C285BDFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F80F7098-02E9-4C9E-B7CB-A2E3C285BDFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F80F7098-02E9-4C9E-B7CB-A2E3C285BDFA}.Release|Any CPU.Build.0 = Release|Any CPU
{05661C28-C52C-442E-B47F-E0CDCBFC0C6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{05661C28-C52C-442E-B47F-E0CDCBFC0C6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05661C28-C52C-442E-B47F-E0CDCBFC0C6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05661C28-C52C-442E-B47F-E0CDCBFC0C6A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
54 changes: 4 additions & 50 deletions cli/EventGrid.Publisher.ConsoleApp/CommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Spectre.Console;
using EventGrid.Publisher.ConsoleApp.Commands;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.CommandLine;
Expand All @@ -11,60 +12,13 @@ namespace EventGrid.Publisher.ConsoleApp
internal class CommandLineParser
{
internal RootCommand Command { get; private set; }

public CommandLineParser()
{
Command = new RootCommand();
Command.Description = "Azure Event Grid Publisher";

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;
Command.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");
Command.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;
Command.AddOption(accessKey);

var filename = new Option<string>("--filename", () => "event.json", "The message that should be send to the Event Grid Topic.");
filename.AddAlias("-f");
Command.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.";
Command.AddOption(overrideEventId);

Command.SetHandler(async (string topic, string region, string accesskey, string filename, bool overrideId) =>
{
AnsiConsole.MarkupLine($"Publishing event to topic [cyan1]{topic}[/].");
FileInfo file = new FileInfo(filename);
if (!file.Exists)
{
AnsiConsole.MarkupLine($"File with name [cyan1]{filename}[/] was not found.");
return;
}
var binary = BinaryData.FromBytes(File.ReadAllBytes(file.FullName));
AnsiConsole.MarkupLine($"Reading file [cyan1]{file.FullName}[/].");
EventPublisher publisher = new EventPublisher(topic, region, accesskey);
string? id = overrideId ? Guid.NewGuid().ToString() : null;
await publisher.PublishBinaryDataAsync(binary, id);
}, topicName, region, accessKey, filename, overrideEventId);
Command.AddCommand(new SendRootCommand());
}
}
}
49 changes: 49 additions & 0 deletions cli/EventGrid.Publisher.ConsoleApp/Commands/SendFileCommand.cs
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 cli/EventGrid.Publisher.ConsoleApp/Commands/SendFolderCommand.cs
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 cli/EventGrid.Publisher.ConsoleApp/Commands/SendRootCommand.cs
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."));
}
}
}
13 changes: 13 additions & 0 deletions cli/EventGrid.Publisher.ConsoleApp/ConsoleColors.cs
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
<AssemblyName>evgtpub</AssemblyName>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0.123</FileVersion>
<InformationalVersion>1.0.0-beta.1+204ff0a</InformationalVersion>
<PackageVersion>1.0.0-beta.1</PackageVersion>
<FileVersion>1.0.1.1</FileVersion>
<InformationalVersion>1.0.1-beta.1+204ff0a</InformationalVersion>
<PackageVersion>1.0.1-beta.1</PackageVersion>
<PublishSingleFile>false</PublishSingleFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.9.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta3.22114.1" />
<PackageReference Include="Spectre.Console" Version="0.43.1-preview.0.43" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reduct.Azure.EventGrid\Reduct.Azure.EventGrid.csproj" />
</ItemGroup>

</Project>
15 changes: 0 additions & 15 deletions cli/EventGrid.Publisher.ConsoleApp/MetaData.cs

This file was deleted.

20 changes: 20 additions & 0 deletions cli/EventGrid.Publisher.ConsoleApp/Options/TopicOption.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion cli/EventGrid.Publisher.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Spectre.Console;
using System.CommandLine;
using System.Diagnostics;
using System.Reflection;

//Debugger.Launch();
VersionInfo.PrintVerionInfo();
Console.WriteLine();

Expand Down
39 changes: 39 additions & 0 deletions cli/EventGrid.Publisher.ConsoleApp/Utils/EventSender.cs
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($"");
}
}
}
}
Loading

0 comments on commit 015728d

Please sign in to comment.