-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (99 loc) · 4.41 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
using System.Text;
using YamlDotNet.Serialization;
void printUsage() {
var f = ConfigProvider.ConfigFilePath;
Console.WriteLine(@$"Usage:
-d / --deliver <project-name>[ <project-name>] Delivers the project(s)
--d-all Delivers all configured projects
--d-git-repo <git-repo-path> [<branch>] Delivers all projects from the given git repo.
<branch> is for the hooking script to trigger
on a specific branch.
--hook Hooks all configured projects.
If there is already a hook present,
simply call this executable with the -g param.
--example-config Prints the example config
ddleiver --example-config > {f}");
}
if (!args.Any()) {
Console.WriteLine("No Arguments provided!");
printUsage();
return;
}
var serializer = new SerializerBuilder().Build();
var deserializer = new DeserializerBuilder().Build();
if (args[0] == "--example-config") {
var yaml = serializer.Serialize(ConfigProvider.ExampleConfig);
Console.WriteLine(yaml);
return;
}
ServerConfig serverConfig;
try {
serverConfig = ConfigProvider.GetConfig(deserializer);
} catch (Exception ex) {
Console.WriteLine(@$"Could not load configuration
Expection: {ex.Message}
({ex.InnerException?.Message})");
return;
}
var deliverer = new DeliveryService(serverConfig);
switch (args[0]) {
case "-h":
case "--help":
printUsage();
return;
case "-d":
case "--deliver":
if (args.Length == 1)
throw new Exception("No prject(s) provided to deliver!");
foreach (var projectName in args[1..])
deliverer.Deliver(projectName);
return;
case "--d-all":
foreach (var projectName in serverConfig.Projects.Keys)
deliverer.Deliver(projectName);
return;
case "--d-git-repo":
if (args.Length == 1)
throw new Exception("No prject provided to deliver!");
var effectedRepo = args[1];
Console.WriteLine($"[INFO] Deliver holy(!) repo '{effectedRepo}'");
var projects = serverConfig.Projects.Where(w => effectedRepo == w.Value.GitRepo);
if (args.Length >= 3) {
Console.WriteLine($"[INFO] Only on branch '{args[2]}'");
projects = projects.Where(w => args[2].Contains(w.Value.OnBranch));
}
foreach (var (projectName, _) in projects)
deliverer.Deliver(projectName);
return;
case "--hook":
var location = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (location is null) {
Console.Error.WriteLine("[ERRO] Couldn find Folder of executing file");
}
Console.WriteLine($"[INFO] Start hooking repos to {location}");
foreach (var projectPath in serverConfig.Projects.Select(s => s.Value.GitRepo).Distinct()) {
Console.WriteLine($"[INFO] Hooking {projectPath}");
var scriptPath = $"{projectPath}/hooks/post-receive";
var script = $"#!/bin/bash\n" +
$"### Autogenerated from DDeliver: {{{location}}}\n" +
$"read oldrev newrev ref\n" +
$"{location} --d-git-repo {ExtensionMethods.EscapeForCommand(projectPath)} $ref\n";
if (File.Exists(scriptPath)) {
Console.WriteLine("[INFO] " +
(File.ReadAllText(scriptPath) == script ?
$"SCRIPT ALREADY HOOKED at {projectPath}! Psst, dont wake the bugs!" :
$"There already exists a post-receive script in repo {projectPath}!") +
" Skipping for now..."
);
continue;
}
File.WriteAllText(scriptPath, script, new UTF8Encoding(false));
Chmod.SetPerimissions(scriptPath, "775");
Console.WriteLine("[INFO] Hooked repo");
}
break;
default:
Console.WriteLine("Command not found!");
printUsage();
break;
}