-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProgram.cs
75 lines (64 loc) · 2.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using CommandLine;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Aliencube.GitHubActions.Teams.ConsoleApp
{
/// <summary>
/// This represents the console app entity.
/// </summary>
public static class Program
{
static Program() {
HttpClient = new HttpClient(new RetryHandler(new HttpClientHandler()))
{
Timeout = TimeSpan.FromSeconds(100) // Default one, just to be easier to customize
};
}
/// <summary>
/// Gets or sets the <see cref="IMessageHandler"/> instance.
/// </summary>
public static IMessageHandler MessageHandler { get; set; } = new MessageHandler();
/// <summary>
/// Gets or sets the <see cref="HttpClient"/> instance.
/// </summary>
public static HttpClient HttpClient { get; set; }
private static JsonSerializerSettings settings { get; } =
new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver() { NamingStrategy = new CamelCaseNamingStrategy() },
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
Converters = new List<JsonConverter> { new ActionConverter() },
};
/// <summary>
/// Invokes the console app.
/// </summary>
/// <param name="args">List of arguments passed.</param>
public static int Main(string[] args)
{
using (var parser = new Parser(with => { with.EnableDashDash = true; with.HelpWriter = Console.Out; }))
{
var result = parser.ParseArguments<Options>(args)
.MapResult(options => OnParsed(options), errors => OnNotParsed(errors))
;
return result;
}
}
private static int OnParsed(Options options)
{
var result = MessageHandler.BuildMessage(options, settings)
.SendMessageAsync(HttpClient)
.Result;
return result;
}
private static int OnNotParsed(IEnumerable<Error> errors)
{
return errors.Count();
}
}
}