-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
133 lines (116 loc) · 4.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// <copyright file="Program.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace EventViewer
{
using EventViewer.Aws;
using EventViewer.Pipeline;
using McMaster.Extensions.CommandLineUtils;
/// <summary>
/// CommandOption.
/// </summary>
public enum CommandOption
{
/// <summary>
/// Help.
/// </summary>
Help,
/// <summary>
/// Subscriptions list.
/// </summary>
Subscriptions,
/// <summary>
/// Lists topics.
/// </summary>
ListTopics,
/// <summary>
/// Subscribes topics to queue.
/// </summary>
Subscribe,
/// <summary>
/// Listens to queue.
/// </summary>
Listen,
/// <summary>
/// Unsubscribes topics to queue.
/// </summary>
Unsubscribe,
}
/// <summary>
/// Program.
/// </summary>
internal class Program
{
/// <summary>
/// Main Program.
/// </summary>
/// <param name="args">Arguments.</param>
/// <returns>Integer.</returns>
public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
/// <summary>
/// Gets Command.
/// </summary>
[Argument(0, Description = "Command one of [ListTopics, Subscriptions, Subscribe [--topic-arn or --all], Unsubscribe [--topic-arn or --all], Listen [Option: --pretty]")]
#pragma warning disable SA1201 // Supress due to special decorator
public CommandOption Command { get; } = CommandOption.Help;
/// <summary>
/// Gets Topic Arn.
/// </summary>
[Option("--topic-arn", Description = "Topic arn")]
public string TopicArn { get; } = Environment.GetEnvironmentVariable("TOPIC_ARN") ?? string.Empty;
/// <summary>
/// Gets EndpointUrl.
/// </summary>
[Option("--endpoint-url", Description = "Aws endpoint url")]
public string EndpointUrl { get; } = Environment.GetEnvironmentVariable("ENDPOINT_URL") ?? "localhost:4566";
/// <summary>
/// Gets a value indicating whether the item is enabled.
/// </summary>
[Option("--pretty", Description = "Pretty JSON view")]
public bool Pretty { get; } = bool.Parse(Environment.GetEnvironmentVariable("PRETTY") ?? "false");
/// <summary>
/// Gets a value indicating whether all.
/// </summary>
[Option("--all", Description = "Param to get all")]
public bool All { get; } = false;
#pragma warning disable IDE0051 // Special method comming from CommandLineUtils
#pragma warning disable IDE0060 // Special parameter comming from CommandLineUtils
private async Task<int> OnExecuteAsync(
CommandLineApplication app,
CancellationToken cancellationToken = default)
{
var client = new Aws.Client(this.EndpointUrl);
var queueUrl = await PipelineQueue.CreateAsync(sqsClient, "DevListener");
switch (this.Command)
{
case CommandOption.ListTopics:
await PipelineTopic.PrintTopicListAsync(client.Sns);
break;
case CommandOption.Subscribe:
if (this.All)
{
await PipelineTopic.SubscribeAllQueueAsync(client.Sns, queueUrl);
break;
}
await PipelineTopic.SubscribeQueueAsync(client.Sns, this.TopicArn, queueUrl);
break;
case CommandOption.Subscriptions:
await PipelineTopic.GetListSubscriptions(client.Sns, cancellationToken);
break;
case CommandOption.Unsubscribe:
await PipelineTopic.UnsubscribeAsync(client.Sns, this.TopicArn, queueUrl, this.All, cancellationToken);
break;
case CommandOption.Listen:
await PipelineQueue.ListenToSqsQueue(client.Sqs, queueUrl, this.Pretty);
break;
case CommandOption.Help:
app.ShowHelp();
break;
default:
app.ShowHelp();
break;
}
return 0;
}
}
}