-
diff --git a/Seenginx/Pages/Nginx.razor.cs b/Seenginx/Pages/Nginx.razor.cs
index 794e3d1..9223f31 100644
--- a/Seenginx/Pages/Nginx.razor.cs
+++ b/Seenginx/Pages/Nginx.razor.cs
@@ -101,6 +101,7 @@ namespace Seenginx.Pages
}
SelectedFile.Name = result.Data.ToString();
+ SelectedFile.FullPath = renameResult.Data.FullPath;
}
public async Task SaveFileAsync()
diff --git a/Seenginx/Pages/Systemd.razor b/Seenginx/Pages/Systemd.razor
index a857faf..9036d2d 100644
--- a/Seenginx/Pages/Systemd.razor
+++ b/Seenginx/Pages/Systemd.razor
@@ -1,7 +1,7 @@
-@page "/systemd"
-
-
SystemD
-
-@code {
-
-}
+@inherits SystemdBase
+@page "/systemd"
+
+
\ No newline at end of file
diff --git a/Seenginx/Pages/Systemd.razor.cs b/Seenginx/Pages/Systemd.razor.cs
new file mode 100644
index 0000000..13bcd42
--- /dev/null
+++ b/Seenginx/Pages/Systemd.razor.cs
@@ -0,0 +1,166 @@
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using Microsoft.AspNetCore.Components;
+using Seenginx.Models;
+using Seenginx.Services;
+using Seenginx.Shared;
+using Seenginx.Utility;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Seenginx.Pages
+{
+ public class SystemdBase : ComponentBase
+ {
+ [Inject] public ISystemDService SystemDService { get; set; }
+ [CascadingParameter] public IModalService Modal { get; set; }
+
+ public string InputSearch { get; set; }
+
+ public List
ConfigFiles { get; set; } = new List();
+ public ConfigFile SelectedFile { get; set; }
+ public List FilteredOutFiles { get; set; } = new List();
+ public NotificationSettings GeneralNotificationSettings { get; set; } = null;
+ public List Templates { get; set; } = new List();
+
+ protected override async Task OnParametersSetAsync()
+ {
+ try
+ {
+ ConfigFiles.AddRange(await SystemDService.GetFilesAsync());
+ Templates.AddRange(await SystemDService.GetTemplates());
+ }
+ catch (Exception ex)
+ {
+ throw ex;
+ }
+ await base.OnParametersSetAsync();
+ }
+
+ public void SelectedFileChanged(ConfigFile configFile)
+ {
+ SelectedFile = configFile;
+ }
+
+ public async Task AddFileModal()
+ {
+ var parameters = new ModalParameters();
+ parameters.Add(nameof(Templates), Templates);
+
+ var resultAwait = Modal.Show(string.Empty, parameters);
+ var result = await resultAwait.Result;
+ if (result.Cancelled) return;
+
+
+ var validationResult = await SystemDService.ValidateForAddFileAsync((NewFileForm)result.Data);
+
+ if (!validationResult.AllOk)
+ {
+ var validationPopupParameters = new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage);
+ var validationPopup = Modal.Show(string.Empty, validationPopupParameters);
+ await validationPopup.Result;
+ validationPopup.Close();
+ return;
+ }
+
+ var addFileResult = await SystemDService.AddFileAsync((NewFileForm)result.Data);
+ if (SelectedFile != null)
+ SelectedFile.Deselect();
+ ConfigFiles.Add(addFileResult.Data);
+ ConfigFiles = ConfigFiles.OrderBy(cf => cf.Name).ToList();
+ SelectedFile = ConfigFiles.Find(cf => cf.Name == addFileResult.Data.Name);
+ SelectedFile.Select();
+ }
+
+ public async Task RenameFileModal()
+ {
+ var parameters = new ModalParameters();
+ parameters.Add(nameof(RenameForm.Name), SelectedFile.Name);
+
+ var resultAwait = Modal.Show(string.Empty, parameters);
+ var result = await resultAwait.Result;
+ if (result.Cancelled) return;
+
+ var validationResult = await SystemDService.ValidateForRenameFileAsync(ConfigFiles, SelectedFile, result.Data.ToString());
+ if (!validationResult.AllOk)
+ {
+ var popupAwait = Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage));
+ await popupAwait.Result;
+ return;
+ }
+
+ var renameResult = await SystemDService.RenameFileAsync(SelectedFile, $"{result.Data}.service");
+ if (!renameResult.AllOk)
+ {
+ var popupAwait = Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, renameResult.ErrorMessage));
+ await popupAwait.Result;
+ return;
+ }
+
+ SelectedFile.Name = result.Data.ToString();
+ SelectedFile.FullPath = renameResult.Data.FullPath;
+ }
+
+ public async Task SaveFileAsync()
+ {
+ var validationResult = await SystemDService.ValidateForSaveFileAsync(SelectedFile);
+
+ if (!validationResult.AllOk)
+ {
+ await Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage)).Result;
+ return;
+ }
+
+ var saveResult = await SystemDService.SaveFileAsync(SelectedFile);
+
+ if (!saveResult.AllOk)
+ {
+ await Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, saveResult.ErrorMessage)).Result;
+ return;
+ }
+ }
+
+ public async Task SaveDraftFileAsync()
+ {
+ //var validationResult = await NginxService.ValidateForSaveDraftFileAsync(SelectedFile);
+
+ //if (!validationResult.AllOk)
+ //{
+ // await Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, validationResult.ErrorMessage)).Result;
+ // return;
+ //}
+
+ var saveDraftResult = await SystemDService.SaveDraftFileAsync(SelectedFile);
+
+ if (!saveDraftResult.AllOk)
+ {
+ await Modal.Show(string.Empty, new ModalParameters().Setup(PopupType.Ok, saveDraftResult.ErrorMessage)).Result;
+ return;
+ }
+ }
+
+ public async Task DeleteFile()
+ {
+ var parameters = new ModalParameters().Setup(PopupType.YesNo, $"Are you sure you want to delete '{SelectedFile.Name}'?");
+
+ var resultAwait = Modal.Show(string.Empty, parameters);
+ var result = await resultAwait.Result;
+ if ((PopupAnswer)result.Data == PopupAnswer.No) return;
+
+ var deleteFileResult = await SystemDService.DeleteFileAsync(SelectedFile);
+
+ if (!deleteFileResult.AllOk)
+ {
+ var errorParameters = new ModalParameters().Setup(PopupType.Ok, $"Something went wrong, here's the error message: '{deleteFileResult.ErrorMessage}'?");
+ var errorReportModalAwait = Modal.Show(string.Empty, errorParameters);
+ await errorReportModalAwait.Result;
+ return;
+ }
+
+ ConfigFiles.Remove(SelectedFile);
+ SelectedFile = null;
+ }
+ }
+}
diff --git a/Seenginx/Seenginx.csproj b/Seenginx/Seenginx.csproj
index 8ab619f..7fc1985 100644
--- a/Seenginx/Seenginx.csproj
+++ b/Seenginx/Seenginx.csproj
@@ -6,6 +6,7 @@
+
@@ -15,6 +16,7 @@
+
diff --git a/Seenginx/Services/INginxService.cs b/Seenginx/Services/INginxService.cs
index e9913a0..ebb419d 100644
--- a/Seenginx/Services/INginxService.cs
+++ b/Seenginx/Services/INginxService.cs
@@ -21,7 +21,7 @@ namespace Seenginx.Services
Task> SaveDraftFileAsync(ConfigFile configFile);
Task ValidateForRenameFileAsync(List configFiles, ConfigFile configFile, string newName);
- Task RenameFileAsync(ConfigFile configFile, string newName);
+ Task> RenameFileAsync(ConfigFile configFile, string newName);
Task ValidateForDeleteFileAsync(ConfigFile configFile);
Task DeleteFileAsync(ConfigFile configFile);
diff --git a/Seenginx/Services/ISystemDService.cs b/Seenginx/Services/ISystemDService.cs
index 1f19fd1..a78a2d0 100644
--- a/Seenginx/Services/ISystemDService.cs
+++ b/Seenginx/Services/ISystemDService.cs
@@ -6,7 +6,23 @@ namespace Seenginx.Services
{
public interface ISystemDService
{
- Task> GetFiles(SystemDFilter filter = null);
- Task> GetLogMessages(SystemDLogsFilter filter = null);
+ Task> GetFilesAsync();
+ Task> GetTemplates();
+
+ Task ValidateForAddFileAsync(NewFileForm newFileForm);
+ Task> AddFileAsync(NewFileForm newFileForm);
+
+ Task ValidateForSaveFileAsync(ConfigFile configFile);
+ Task> SaveFileAsync(ConfigFile configFile);
+
+ Task ValidateForSaveDraftFileAsync(ConfigFile configFile);
+ Task> SaveDraftFileAsync(ConfigFile configFile);
+
+ Task ValidateForRenameFileAsync(List configFiles, ConfigFile configFile, string newName);
+ Task> RenameFileAsync(ConfigFile configFile, string newName);
+
+ Task ValidateForDeleteFileAsync(ConfigFile configFile);
+ Task DeleteFileAsync(ConfigFile configFile);
+
}
}
\ No newline at end of file
diff --git a/Seenginx/Services/Models/ConfigPaths.cs b/Seenginx/Services/Models/ConfigPaths.cs
deleted file mode 100644
index c73ce82..0000000
--- a/Seenginx/Services/Models/ConfigPaths.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-
-namespace Seenginx.Services.Models
-{
- public class ConfigPaths
- {
- public string NginxPath { get; set; }
- public string SystemDPath { get; set; }
- }
-}
diff --git a/Seenginx/Services/Models/SeenginxSettings.cs b/Seenginx/Services/Models/SeenginxSettings.cs
new file mode 100644
index 0000000..7f3af79
--- /dev/null
+++ b/Seenginx/Services/Models/SeenginxSettings.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Seenginx.Services.Models
+{
+ public class SeenginxSettings
+ {
+ public NginxSettings nginx { get; set; }
+ public SystemdSettings systemd { get; set; }
+ }
+
+ public class NginxSettings
+ {
+ public string rootPath { get; set; }
+ }
+
+ public class SystemdSettings
+ {
+ public string rootPath { get; set; }
+ }
+}
diff --git a/Seenginx/Services/NginxService.cs b/Seenginx/Services/NginxService.cs
index c5669e9..6897599 100644
--- a/Seenginx/Services/NginxService.cs
+++ b/Seenginx/Services/NginxService.cs
@@ -14,27 +14,26 @@ namespace Seenginx.Services
{
public class NginxService : INginxService
{
- private readonly ConfigPaths ConfigPaths;
-
+ private readonly SeenginxSettings Settings;
private readonly CommandService CommandService;
- public NginxService(ConfigPaths configPaths, CommandService commandService)
+ public NginxService(SeenginxSettings configPaths, CommandService commandService)
{
- ConfigPaths = configPaths;
+ Settings = configPaths;
CommandService = commandService;
}
public async Task> GetFilesAsync()
{
await Task.Run(() => { });
- var rootConfigs = Directory.GetFiles(ConfigPaths.NginxPath, "*.conf");
- var rootDraftConfigs = Directory.GetFiles(ConfigPaths.NginxPath, "*.conf.draft");
+ var rootConfigs = Directory.GetFiles(Settings.nginx.rootPath, "*.conf");
+ var rootDraftConfigs = Directory.GetFiles(Settings.nginx.rootPath, "*.conf.draft");
- var confdConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "conf.d"), "*.conf");
- var confDraftConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "conf.d"), "*.conf.draft");
+ var confdConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "conf.d"), "*.conf");
+ var confDraftConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "conf.d"), "*.conf.draft");
- var sitesAvailableConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-available"), "*.conf");
- var sitesAvailableDraftConfigs = Directory.GetFiles(Path.Combine(ConfigPaths.NginxPath, "sites-available"), "*.conf.draft");
+ var sitesAvailableConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "sites-available"), "*.conf");
+ var sitesAvailableDraftConfigs = Directory.GetFiles(Path.Combine(Settings.nginx.rootPath, "sites-available"), "*.conf.draft");
var rootConfigFiles = rootConfigs.Select(fp =>
{
@@ -44,7 +43,7 @@ namespace Seenginx.Services
configFile.Folder = string.Empty;
configFile.LastUpdated = File.GetLastWriteTime(fp);
configFile.Name = name;
- configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, $"{configFile.Name}.conf");
+ configFile.FullPath = Path.Combine(Settings.nginx.rootPath, $"{configFile.Name}.conf");
configFile.Body = File.ReadAllText(fp);
if (rootDraftConfigs.Any(cfp => cfp.Contains(name)))
configFile.DraftBody = File.ReadAllText(rootDraftConfigs.First(cfp => cfp.Contains(name)));
@@ -59,7 +58,7 @@ namespace Seenginx.Services
configFile.Folder = "conf.d";
configFile.LastUpdated = File.GetLastWriteTime(fp);
configFile.Name = fileName;
- configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
+ configFile.FullPath = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
configFile.Body = File.ReadAllText(fp);
if (confDraftConfigs.Any(cfp => cfp.Contains(fileName)))
configFile.DraftBody = File.ReadAllText(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
@@ -74,37 +73,18 @@ namespace Seenginx.Services
configFile.Folder = "sites-available";
configFile.LastUpdated = File.GetLastWriteTime(fp);
configFile.Name = fileName;
- configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
+ configFile.FullPath = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
configFile.Body = File.ReadAllText(fp);
if (sitesAvailableDraftConfigs.Any(cfp => cfp.Contains(fileName)))
configFile.DraftBody = File.ReadAllText(sitesAvailableDraftConfigs.First(cfp => cfp.Contains(fileName)));
return configFile;
});
- //var sitesEnabledConfigFiles = sitesEnabledConfigs.Select(fp =>
- //{
- // var fileName = Path.GetFileName(fp);
- // var configFile = new ConfigFile();
- // configFile.CanBeDeleted = true;
- // configFile.Folder = "/sites-enabled";
- // configFile.LastUpdated = File.GetLastWriteTime(fp);
- // configFile.Name = fileName;
- // configFile.FullPath = Path.Combine(ConfigPaths.NginxPath, configFile.Folder.Replace("/", string.Empty), configFile.Name);
- // configFile.Body = File.ReadAllText(fp);
- // configFile.DraftName = fileName;
- // configFile.DraftBody = configFile.Body;
- // if (confDraftConfigs.Any(cfp => cfp.Contains(fileName)))
- // {
- // configFile.DraftName = Path.GetFileName(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
- // configFile.DraftBody = File.ReadAllText(confDraftConfigs.First(cfp => cfp.Contains(fileName)));
- // }
- // return configFile;
- //});
+
var finalList = new List();
finalList.AddRange(rootConfigFiles);
finalList.AddRange(confdConfigFiles);
finalList.AddRange(sitesAvailableConfigFiles);
- //finalList.AddRange(sitesEnabledConfigFiles);
finalList = finalList.OrderBy(cf => cf.Name).ToList();
@@ -142,7 +122,7 @@ namespace Seenginx.Services
var validationResult = new Result();
try
{
- var filePath = Path.Combine(ConfigPaths.NginxPath, "conf.d", $"{newFileForm.Name}.conf");
+ var filePath = Path.Combine(Settings.nginx.rootPath, "conf.d", $"{newFileForm.Name}.conf");
if (File.Exists(filePath))
return validationResult.Invalidate($"There's already a file with the '{newFileForm.Name}.conf' name.");
@@ -161,9 +141,9 @@ namespace Seenginx.Services
try
{
var newFile = new ConfigFile();
- newFile.Name = $"{newFileForm.Name}.conf";
+ newFile.Name = newFileForm.Name;
newFile.Folder = "conf.d";
- newFile.FullPath = Path.Combine(ConfigPaths.NginxPath, newFile.Folder, newFile.Name);
+ newFile.FullPath = Path.Combine(Settings.nginx.rootPath, newFile.Folder, $"{newFileForm.Name}.conf");
newFile.Body = newFileForm.SelectedTemplate == 0.ToString() ? string.Empty : (await GetTemplates()).SingleOrDefault(t => t.Name == newFileForm.SelectedTemplate)?.Code;
newFile.LastUpdated = DateTime.UtcNow;
@@ -205,6 +185,8 @@ namespace Seenginx.Services
return result.Invalidate($"File '{configFile.FullPath}' not found.");
File.Delete(configFile.FullPath);
+ if (configFile.DraftBody != null)
+ File.Delete($"{configFile.FullPath}.draft");
return result;
}
@@ -236,7 +218,7 @@ namespace Seenginx.Services
var saveResult = new Result();
try
{
- await File.WriteAllTextAsync(Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf"), configFile.Body, Encoding.UTF8);
+ await File.WriteAllTextAsync(Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf"), configFile.Body, Encoding.UTF8);
return saveResult;
}
@@ -270,7 +252,7 @@ namespace Seenginx.Services
var saveDraftResult = new Result();
try
{
- await File.WriteAllTextAsync(Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf.draft"), configFile.DraftBody, Encoding.UTF8);
+ await File.WriteAllTextAsync(Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf.draft"), configFile.DraftBody, Encoding.UTF8);
return saveDraftResult;
}
@@ -288,7 +270,7 @@ namespace Seenginx.Services
{
- if(configFiles.Count(cf => cf.Name == newName) > 0)
+ if (configFiles.Count(cf => cf.Name == newName) > 0)
return renameResult.Invalidate($"File '{selectedConfigFile.FullPath}' already exists.");
@@ -297,7 +279,7 @@ namespace Seenginx.Services
if (!File.Exists(selectedConfigFile.FullPath))
return renameResult.Invalidate($"Original file '{selectedConfigFile.FullPath}' not found.");
- var newPathName = Path.Combine(ConfigPaths.NginxPath, selectedConfigFile.Folder, $"{newName}.conf");
+ var newPathName = Path.Combine(Settings.nginx.rootPath, selectedConfigFile.Folder, $"{newName}.conf");
if (File.Exists(newPathName))
return renameResult.Invalidate($"The file '{newPathName}' already exists.");
@@ -309,15 +291,20 @@ namespace Seenginx.Services
}
}
- public async Task RenameFileAsync(ConfigFile configFile, string newName)
+ public async Task> RenameFileAsync(ConfigFile configFile, string newName)
{
- var renameResult = new Result();
+ var renameResult = new Result();
try
{
- var originalPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
- var newPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, newName);
+ var originalPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
+ var newPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, newName);
File.Move(originalPathName, newPathName, overwrite: false);
+ if (configFile.DraftBody != null)
+ File.Move($"{originalPathName}.draft", $"{newPathName}.draft", overwrite: false);
+
+ configFile.FullPath = newPathName;
+ renameResult.SetData(configFile);
return renameResult;
}
@@ -334,7 +321,7 @@ namespace Seenginx.Services
var command1 = string.Empty;
var command2 = string.Empty;
var runResult = new Result();
- var originalPathName = Path.Combine(ConfigPaths.NginxPath, configFile.Folder, $"{configFile.Name}.conf");
+ var originalPathName = Path.Combine(Settings.nginx.rootPath, configFile.Folder, $"{configFile.Name}.conf");
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32S:
@@ -357,17 +344,5 @@ namespace Seenginx.Services
return runResult;
}
- //public async Task DeleteFileAsync(ConfigFile configFile)
- //{
- // var result = new Result(true);
- // try
- // {
- // return result;
- // }
- // catch (Exception ex)
- // {
- // return result.Invalidate(ex.Message, ex);
- // }
- //}
}
}
diff --git a/Seenginx/Services/SystemDService.cs b/Seenginx/Services/SystemDService.cs
index 6a261d9..94c9d72 100644
--- a/Seenginx/Services/SystemDService.cs
+++ b/Seenginx/Services/SystemDService.cs
@@ -1,23 +1,279 @@
using Seenginx.Models;
+using Seenginx.Services;
+using Seenginx.Services.Models;
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
+using System.Text;
using System.Threading.Tasks;
namespace Seenginx.Services
{
public class SystemDService : ISystemDService
{
- public async Task> GetFiles(SystemDFilter filter = null)
+
+ private readonly SeenginxSettings Settings;
+
+ private readonly CommandService CommandService;
+
+ public SystemDService(SeenginxSettings configPaths, CommandService commandService)
{
- await Task.Run(() => { });
- return new List();
+ Settings = configPaths;
+ CommandService = commandService;
}
- public async Task> GetLogMessages(SystemDLogsFilter filter = null)
+ public async Task> GetFilesAsync()
{
await Task.Run(() => { });
- return new List();
+ var rootConfigs = Directory.GetFiles(Settings.systemd.rootPath, "*.service");
+ var rootDraftConfigs = Directory.GetFiles(Settings.systemd.rootPath, "*.service.draft");
+
+ var rootConfigFiles = rootConfigs.Select(fp =>
+ {
+ var name = Path.GetFileNameWithoutExtension(fp);
+ var configFile = new ConfigFile();
+ configFile.CanBeDeleted = true;
+ configFile.Folder = string.Empty;
+ configFile.LastUpdated = File.GetLastWriteTime(fp);
+ configFile.Name = name;
+ configFile.FullPath = Path.Combine(Settings.systemd.rootPath, $"{configFile.Name}.service");
+ configFile.Body = File.ReadAllText(fp);
+ if (rootDraftConfigs.Any(cfp => cfp.Contains(name)))
+ configFile.DraftBody = File.ReadAllText(rootDraftConfigs.First(cfp => cfp.Contains(name)));
+
+ return configFile;
+ });
+
+ var finalList = new List();
+ finalList.AddRange(rootConfigFiles);
+
+ finalList = finalList.OrderBy(cf => cf.Name).ToList();
+
+ return finalList;
}
+
+ public async Task> GetTemplates()
+ {
+ var templates = new List();
+ try
+ {
+ var systemdTemplateDirectory = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "templates", "systemd");
+ var systemdTemplateFiles = Directory.GetFiles(systemdTemplateDirectory, "*.template");
+
+ foreach (var templateFilePath in systemdTemplateFiles)
+ {
+ var template = new Template();
+ var templateFileLines = await File.ReadAllLinesAsync(templateFilePath);
+ template.Name = templateFileLines.FirstOrDefault();
+ template.Code = string.Join(Environment.NewLine, templateFileLines.Skip(2));
+ templates.Add(template);
+ }
+
+ return templates;
+ }
+ catch (Exception ex)
+ {
+ throw ex;
+ }
+ }
+
+
+ public async Task ValidateForAddFileAsync(NewFileForm newFileForm)
+ {
+ var validationResult = new Result();
+ try
+ {
+ var filePath = Path.Combine(Settings.systemd.rootPath, $"{newFileForm.Name}.service");
+
+ if (File.Exists(filePath))
+ return validationResult.Invalidate($"There's already a file with the '{newFileForm.Name}.service' name.");
+
+ return validationResult;
+ }
+ catch (Exception ex)
+ {
+ return validationResult.Invalidate($"Exception at {nameof(ValidateForAddFileAsync)}()", ex);
+ }
+ }
+
+ public async Task> AddFileAsync(NewFileForm newFileForm)
+ {
+ var addResult = new Result();
+ try
+ {
+ var newFile = new ConfigFile();
+ newFile.Name = newFileForm.Name;
+ newFile.Folder = string.Empty;
+ newFile.FullPath = Path.Combine(Settings.systemd.rootPath, newFile.Folder, $"{newFileForm.Name}.service");
+ newFile.Body = newFileForm.SelectedTemplate == 0.ToString() ? string.Empty : (await GetTemplates()).SingleOrDefault(t => t.Name == newFileForm.SelectedTemplate)?.Code;
+ newFile.LastUpdated = DateTime.UtcNow;
+
+ await File.WriteAllTextAsync(newFile.FullPath, newFile.Body, Encoding.UTF8);
+
+ addResult.SetData(newFile);
+
+ return addResult;
+ }
+ catch (Exception ex)
+ {
+ return addResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+
+ public async Task ValidateForDeleteFileAsync(ConfigFile configFile)
+ {
+ var validationResult = new Result();
+ try
+ {
+ if (File.Exists(configFile.FullPath))
+ return validationResult.Invalidate($"File '{configFile.FullPath}' not found.");
+
+ return validationResult;
+ }
+ catch (Exception ex)
+ {
+ return validationResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+ public async Task DeleteFileAsync(ConfigFile configFile)
+ {
+ var result = new Result();
+ try
+ {
+ if (!File.Exists(configFile.FullPath))
+ return result.Invalidate($"File '{configFile.FullPath}' not found.");
+
+ File.Delete(configFile.FullPath);
+
+ return result;
+ }
+ catch (Exception ex)
+ {
+ return result.Invalidate(ex.Message, ex);
+ }
+ }
+
+
+ public async Task ValidateForSaveFileAsync(ConfigFile configFile)
+ {
+ var validationResult = new Result();
+ try
+ {
+ if (File.Exists(configFile.FullPath))
+ return validationResult.Invalidate($"File '{configFile.FullPath}' not found.");
+
+ return validationResult;
+ }
+ catch (Exception ex)
+ {
+ return validationResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+ public async Task> SaveFileAsync(ConfigFile configFile)
+ {
+ var saveResult = new Result();
+ try
+ {
+ await File.WriteAllTextAsync(Path.Combine(Settings.systemd.rootPath, $"{configFile.Name}.service"), configFile.Body, Encoding.UTF8);
+
+ return saveResult;
+ }
+ catch (Exception ex)
+ {
+ return saveResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+
+ public async Task ValidateForSaveDraftFileAsync(ConfigFile configFile)
+ {
+ var validationResult = new Result();
+ try
+ {
+ var draftPathName = $"{configFile.FullPath}.draft";
+
+ if (!File.Exists(draftPathName))
+ return validationResult.Invalidate($"File '{draftPathName}' not found.");
+
+ return validationResult;
+ }
+ catch (Exception ex)
+ {
+ return validationResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+ public async Task> SaveDraftFileAsync(ConfigFile configFile)
+ {
+ var saveDraftResult = new Result();
+ try
+ {
+ await File.WriteAllTextAsync(Path.Combine(Settings.systemd.rootPath, $"{configFile.Name}.service.draft"), configFile.DraftBody, Encoding.UTF8);
+
+ return saveDraftResult;
+ }
+ catch (Exception ex)
+ {
+ return saveDraftResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+
+ public async Task ValidateForRenameFileAsync(List configFiles, ConfigFile selectedConfigFile, string newName)
+ {
+ var renameResult = new Result();
+ try
+ {
+
+
+ if (configFiles.Count(cf => cf.Name == newName) > 0)
+ return renameResult.Invalidate($"File '{selectedConfigFile.FullPath}' already exists.");
+
+
+
+
+ if (!File.Exists(selectedConfigFile.FullPath))
+ return renameResult.Invalidate($"Original file '{selectedConfigFile.FullPath}' not found.");
+
+ var newPathName = Path.Combine(Settings.systemd.rootPath, $"{newName}.service");
+ if (File.Exists(newPathName))
+ return renameResult.Invalidate($"The file '{newPathName}' already exists.");
+
+ return renameResult;
+ }
+ catch (Exception ex)
+ {
+ return renameResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+ public async Task> RenameFileAsync(ConfigFile configFile, string newName)
+ {
+ var renameResult = new Result();
+ try
+ {
+ var originalPathName = Path.Combine(Settings.systemd.rootPath, $"{configFile.Name}.service");
+ var newPathName = Path.Combine(Settings.systemd.rootPath, newName);
+
+ File.Move(originalPathName, newPathName, overwrite: false);
+ if(configFile.DraftBody != null)
+ File.Move(originalPathName, newPathName, overwrite: false);
+
+ configFile.FullPath = newPathName;
+ renameResult.SetData(configFile);
+
+ return renameResult;
+ }
+ catch (Exception ex)
+ {
+ return renameResult.Invalidate(ex.Message, ex);
+ }
+ }
+
+
}
}
diff --git a/Seenginx/Settings/settings.json b/Seenginx/Settings/settings.json
new file mode 100644
index 0000000..e916b8a
--- /dev/null
+++ b/Seenginx/Settings/settings.json
@@ -0,0 +1,8 @@
+{
+ "nginx": {
+ "rootPath": "C:\\nginx\\"
+ },
+ "systemd": {
+ "rootPath": "C:\\systemd\\system\\"
+ }
+}
\ No newline at end of file
diff --git a/Seenginx/Shared/SystemdConfigForm.razor b/Seenginx/Shared/SystemdConfigForm.razor
new file mode 100644
index 0000000..a300036
--- /dev/null
+++ b/Seenginx/Shared/SystemdConfigForm.razor
@@ -0,0 +1,5 @@
+SystemdConfigForm
+
+@code {
+
+}
diff --git a/Seenginx/Startup.cs b/Seenginx/Startup.cs
index aab6541..391423d 100644
--- a/Seenginx/Startup.cs
+++ b/Seenginx/Startup.cs
@@ -1,10 +1,7 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
+using System.IO;
+using System.Text.Json;
using Blazored.Modal;
using Microsoft.AspNetCore.Builder;
-using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -33,11 +30,10 @@ namespace Seenginx
services.AddServerSideBlazor();
services.AddBlazoredModal();
- var configPaths = new ConfigPaths();
- configPaths.NginxPath = @"C:\nginx\";
- configPaths.SystemDPath = @"C:\systemd\system\";
+ var settingsJson = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Settings", "settings.json"));
+ var settings = JsonSerializer.Deserialize(settingsJson);
- services.AddSingleton(configPaths);
+ services.AddSingleton(settings);
services.AddTransient();
services.AddTransient();
services.AddTransient();