-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
140 lines (111 loc) · 4.74 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
134
135
136
137
138
139
// See https://aka.ms/new-console-template for more information
namespace cdndeploy;
using BunnyCDN.Net.Storage;
using System;
using System.Threading.Tasks;
using System.CommandLine;
using bunnydeploy;
using BunnyCDN.Net.Storage.Models;
using System.Net.Http.Json;
using System.Net;
public class CdnDeploy {
public static async Task Main(string[] argv) {
//get arguments
var storagezone = new Option<string>(
CommandParams.StorageZone,
CommandHelp.StorageZone
);
var storagekey = new Option<string>(
CommandParams.StorageKey,
CommandHelp.StorageKey
);
var contentpath = new Argument<string>(
CommandParams.ContentPath,
CommandHelp.ContentPath
);
var accesskey = new Option<string>(
CommandParams.AccessKey,
CommandParams.AccessKey
);
var pullZoneId = new Option<string>(
CommandParams.PullZoneId,
CommandParams.PullZoneId
);
//default command
var rootcommand = new RootCommand();
rootcommand.AddGlobalOption(storagezone);
rootcommand.AddGlobalOption(storagekey);
rootcommand.AddGlobalOption(accesskey);
rootcommand.AddGlobalOption(pullZoneId);
//purge command
var purgecommand = new Command(CommandNames.Purge, CommandHelp.Purge);
rootcommand.AddCommand(purgecommand);
purgecommand.SetHandler(PurgeCDNAsync, accesskey, pullZoneId);
//deploy command
var deploycommand = new Command(CommandNames.Deploy, CommandHelp.Deploy);
rootcommand.AddCommand(deploycommand);
deploycommand.AddArgument(contentpath);
deploycommand.SetHandler(async (string storagekey, string storagezone, string contentpath) => {
var context = GetContext(storagezone, storagekey);
await DeployContentAsync(context, storagezone, contentpath);
}, storagekey, storagezone, contentpath);
if(argv.Length == 0) {
await rootcommand.InvokeAsync(CommandParams.Help);
}
else {
await rootcommand.InvokeAsync(argv);
}
}
private static BunnyCDNStorage GetContext(string storagezone, string accesskey) {
var context = new BunnyCDNStorage(storagezone, accesskey, string.Empty);
return context;
}
private static async Task PurgeCDNAsync(string accesskey, string zoneId) {
using var http = new HttpClient();
{
Console.WriteLine("Purging url...");
var message = new HttpRequestMessage(HttpMethod.Post, "https://api.bunny.net/purge?async=false&url=https%3A%2F%2Fwww.fluffydice.cloud")
{
Content = new StringContent(string.Empty),
};
message.Headers.Add("AccessKey", accesskey);
using var response = await http.SendAsync(message);
Console.Write($"Response: {await response.Content.ReadAsStringAsync()}");
if(response.StatusCode == HttpStatusCode.OK) {
Console.WriteLine("200 OK");
Console.WriteLine("URL purged.");
}
else {
throw new Exception($"failed to purge url: {response.StatusCode}, {response.ReasonPhrase}");
}
}
{
Console.WriteLine($"Purging Pull Zone '{zoneId}'...");
var message = new HttpRequestMessage(HttpMethod.Post, $"https://api.bunny.net/pullzone/{zoneId}/purgeCache")
{
Content = new StringContent(string.Empty)
};
message.Headers.Add("AccessKey", accesskey);
using var response = await http.SendAsync(message);
Console.Write($"Response: {await response.Content.ReadAsStringAsync()}");
if(response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.NoContent) { //request succeeded but no response given
Console.WriteLine("200 OK");
Console.WriteLine("Pull Zone purged.");
}
else {
throw new Exception($"failed to purge pullzone: {response.StatusCode}");
}
}
}
private static async Task DeployContentAsync(BunnyCDNStorage context, string storagezone, string contentpath) {
Console.WriteLine($"Deploying static content. contentpath: {contentpath}");
var files = Directory.GetFiles(contentpath);
for(var i = 0; i < files.Length; i++) {
var current = Path.GetFileName(files[i]);
Console.WriteLine($"Uploading file: {current}");
await context.UploadAsync(files[i], $"/{storagezone}/{current}");
}
Console.WriteLine("Content deployed.");
}
}