CodeLiturgy.Dashboard/CodeLiturgy.Views/Augeas/AugeasManager.cs

80 lines
2.2 KiB
C#
Raw Normal View History

2022-12-05 02:07:41 +03:00
using Sharp.Augeas;
namespace CodeLiturgy.Views.Augeas
{
public class AugeasManager
{
private readonly Sharp.Augeas.Augeas _augeas;
2022-12-06 17:33:12 +03:00
2022-12-05 02:07:41 +03:00
private AugSettings _augSettings;
2022-12-06 17:33:12 +03:00
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
private List<string> _currentApacheConfigurations;
private const string _apachePath = "/etc/apache2/sites-available";
2022-12-05 02:07:41 +03:00
public AugeasManager()
{
2022-12-06 17:33:12 +03:00
_augSettings = AugeasExtensions.GetSettings();
_augeas = new Sharp.Augeas.Augeas(_augSettings);
RefreshApacheConfigurations();
2022-12-05 02:07:41 +03:00
}
2022-12-06 17:33:12 +03:00
public string GetPreview(string site)
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
if (!_currentApacheConfigurations.Contains(site))
{
return string.Empty;
}
var siteFullPath = $"{_apachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var preview = _augeas.GetPreview($"/files{siteFullPath}");
2022-12-05 02:07:41 +03:00
return preview;
}
2022-12-06 17:33:12 +03:00
public List<string> GetApacheAvailableConfigurations(int skip, int take)
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
var result = _currentApacheConfigurations
.Skip(skip)
.Take(take)
.ToList();
return _currentApacheConfigurations;
2022-12-05 02:07:41 +03:00
2022-12-06 17:33:12 +03:00
}
2022-12-05 02:07:41 +03:00
2022-12-06 17:33:12 +03:00
public SuperNode GetVirtualHostTree(string site)
{
if (!_currentApacheConfigurations.Contains(site))
2022-12-05 02:07:41 +03:00
{
2022-12-06 17:33:12 +03:00
return null;
}
var siteFullPath = $"{_apachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var tree = _augeas.GetTree("VirtualHost",$"/files{siteFullPath}/VirtualHost/*");
return tree;
}
private void RefreshApacheConfigurations()
{
var apacheDir = _augSettings.root + _apachePath;
DirectoryInfo d = new DirectoryInfo(apacheDir);
FileInfo[] files = d.GetFiles("*.conf");
var result = files
.Select(x => x.Name)
.ToList();
_currentApacheConfigurations = result;
2022-12-05 02:07:41 +03:00
}
}
}