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

80 lines
2.2 KiB
C#

using Sharp.Augeas;
namespace CodeLiturgy.Views.Augeas
{
public class AugeasManager
{
private readonly Sharp.Augeas.Augeas _augeas;
private AugSettings _augSettings;
public List<string> CurrentApacheConfigurations => _currentApacheConfigurations;
private List<string> _currentApacheConfigurations;
private const string _apachePath = "/etc/apache2/sites-available";
public AugeasManager()
{
_augSettings = AugeasExtensions.GetSettings();
_augeas = new Sharp.Augeas.Augeas(_augSettings);
RefreshApacheConfigurations();
}
public string GetPreview(string site)
{
if (!_currentApacheConfigurations.Contains(site))
{
return string.Empty;
}
var siteFullPath = $"{_apachePath}/{site}";
_augeas.LoadFile(siteFullPath);
var preview = _augeas.GetPreview($"/files{siteFullPath}");
return preview;
}
public List<string> GetApacheAvailableConfigurations(int skip, int take)
{
var result = _currentApacheConfigurations
.Skip(skip)
.Take(take)
.ToList();
return _currentApacheConfigurations;
}
public SuperNode GetVirtualHostTree(string site)
{
if (!_currentApacheConfigurations.Contains(site))
{
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;
}
}
}