|
|
@@ -0,0 +1,52 @@
|
|
|
+using CronAlarm.Config;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace CronAlarm
|
|
|
+{
|
|
|
+ public class CronSectionHandler : ICronSectionHandler
|
|
|
+ {
|
|
|
+ public const string AlertsSection = "ALERTS";
|
|
|
+ private readonly CronApiClient _cronApi;
|
|
|
+ private readonly char[] _whitespace = new char[] { ' ', '\t' };
|
|
|
+
|
|
|
+ public CronSectionHandler(CronApiClient cronApi)
|
|
|
+ {
|
|
|
+ _cronApi = cronApi;
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable<string> GetExpressions()
|
|
|
+ {
|
|
|
+ return ProcessCronText(_cronApi.GetSection(AlertsSection).Result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable<string> Update(IEnumerable<CronEntry> entries)
|
|
|
+ {
|
|
|
+ using (var writer = new StringWriter())
|
|
|
+ {
|
|
|
+ writer.NewLine = "\n";
|
|
|
+ foreach (var entry in entries)
|
|
|
+ {
|
|
|
+ writer.WriteLine($"# {entry.Comment}");
|
|
|
+ writer.Write(entry.Expression);
|
|
|
+ writer.Write(new String(' ', 25 - entry.Expression.Length));
|
|
|
+ writer.WriteLine(entry.Command);
|
|
|
+ }
|
|
|
+
|
|
|
+ writer.Flush();
|
|
|
+ return ProcessCronText(_cronApi.SetSection(AlertsSection, writer.ToString()).Result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private IEnumerable<string> ProcessCronText(Stream textStream)
|
|
|
+ {
|
|
|
+ using (var reader = new StreamReader(textStream))
|
|
|
+ {
|
|
|
+ var lines = reader.ReadToEnd().Split('\n');
|
|
|
+ return lines.Select(line => line.Trim()).Where(line => !String.IsNullOrEmpty(line) && !line.StartsWith("#")).Select(line => string.Join(" ", line.Split(_whitespace, StringSplitOptions.RemoveEmptyEntries).Take(5)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|