Remove old dependencies

This commit is contained in:
code liturgy 2022-11-08 15:42:09 -05:00
parent 5adc8c28ef
commit d484feb8a6
10 changed files with 58 additions and 156 deletions

View File

@ -29,6 +29,19 @@ public class AugeasTests
Assert.Pass();
}
[Test]
public void GetPreviewReturnsValid()
{
_ = _augeas.LoadFile("/etc/apache2/sites-available/00-ci.codeliturgy.com.conf");
var preview = _augeas.GetPreview("/files/etc/apache2/sites-available/00-ci.codeliturgy.com.conf/*");
var stringInvalid = string.IsNullOrEmpty(preview);
Assert.That(!stringInvalid);
}
[Test]
public void GetTreeVirtualHostReturnsDictionaryWithKeys()
@ -36,7 +49,6 @@ public class AugeasTests
var virtualHostConfig = "/etc/apache2/sites-available/00-ci.codeliturgy.com.conf";
var tree = _augeas.GetVirtualHostTree(virtualHostConfig);
Assert.That(tree.Count > 0);
}

View File

@ -1,44 +0,0 @@
namespace Sharp.Augeas;
public static class AugFlags
{
public static int NONE = 0;
/**
* Keep the original file with a .augsave extension
*/
public static int SAVE_BACKUP = (1 << 0);
/**
* Save changes into a file with extension .augnew, and do not overwrite the
* original file. Takes precedence over AUG_SAVE_BACKUP
*/
public static int SAVE_NEWFILE = (1 << 1);
/**
* Typecheck lenses; since it can be very expensive it is not done by
* default
*/
public static int TYPE_CHECK = (1 << 2);
/**
* Do not use the builtin load path for modules
*/
public static int NO_STDINC = (1 << 3);
/**
* Make save a no-op process, just record what would have changed
*/
public static int SAVE_NOOP = (1 << 4);
/**
* Do not load the tree from AUG_INIT
*/
public static int NO_LOAD = (1 << 5);
public static int NO_MODL_AUTOLOAD = (1 << 6);
/**
* Enable span on load
*/
public static int AUG_ENABLE_SPAN = (1 << 7);
}

View File

@ -6,10 +6,21 @@ namespace Sharp.Augeas
{
/// <summary>
/// Augeas Core containing the settings of this instance.
/// We can have multiple augeas cores during the execution.
/// </summary>
public sealed class Augeas
{
#region Flags
public static int NONE = 0;
public static int SAVE_BACKUP = (1 << 0);
public static int SAVE_NEWFILE = (1 << 1);
public static int TYPE_CHECK = (1 << 2);
public static int NO_STDINC = (1 << 3);
public static int SAVE_NOOP = (1 << 4);
public static int NO_LOAD = (1 << 5);
public static int NO_MODL_AUTOLOAD = (1 << 6);
public static int AUG_ENABLE_SPAN = (1 << 7);
#endregion Flags
private readonly IntPtr _augeas;
private HashSet<string> _loadedFiles = new();
@ -93,20 +104,18 @@ namespace Sharp.Augeas
/// Loads a file.
/// </summary>
/// <param name="configurationFilePath">Full path to the configuration file</param>
public void LoadFile(string configurationFilePath)
public bool LoadFile(string configurationFilePath)
{
if (_loadedFiles.Contains(configurationFilePath))
{
return;
return true;
}
bool success = load_file(_augeas, configurationFilePath);
if (!success)
{
throw new InvalidOperationException($"Unable to load {configurationFilePath}.");
}
_loadedFiles.Add(configurationFilePath);
return success;
}
/// <summary>
@ -118,7 +127,14 @@ namespace Sharp.Augeas
{
print_tree(_augeas, matchPath);
}
private void FreeString(IntPtr str)
{
if (str != IntPtr.Zero)
{
free_str(str);
}
}
/// <summary>
/// Returns the tree given the augeas configuration path.
@ -130,8 +146,6 @@ namespace Sharp.Augeas
var result = new Dictionary<string, string>();
var raw = get_tree(_augeas, matchPath);
string sb = Marshal.PtrToStringAnsi(raw);
if (sb == null) return result;
free_str(raw);
var lines = sb.Split(";ENDL;");
foreach (var line in lines)
{
@ -141,9 +155,24 @@ namespace Sharp.Augeas
result.Add(pair[0].Remove(0, 3), pair[1]);
}
}
FreeString(raw);
return result;
}
/// <summary>
/// Gets a preview of the configuration segment of <see cref="matchPath"/>.
/// The file must be loaded first.
/// </summary>
/// <param name="matchPath">Augeas path of the configuration</param>
/// <returns></returns>
public string GetPreview(string matchPath)
{
var raw = get_preview(_augeas, matchPath);
string sb = Marshal.PtrToStringAnsi(raw);
FreeString(raw);
return sb;
}
#endregion
@ -170,6 +199,5 @@ namespace Sharp.Augeas
PrintTree(virtualHostProxyMatchPath);
}
}
}

View File

@ -32,6 +32,11 @@ namespace Sharp.Augeas
[DllImport(_libName)]
public static extern IntPtr get_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
[DllImport(_libName)]
public static extern IntPtr get_preview(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
[DllImport(_libName)]
public static extern IntPtr get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);

View File

@ -1,30 +0,0 @@
using System.Runtime.InteropServices;
using Sharp.Augeas.Test;
namespace Sharp.Augeas;
public class HostManager
{
private Augeas _augeas;
public void Initialize()
{
var path = Environment.CurrentDirectory;
var root = $"{path}/root/";
var lensPath = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
lensPath = "/usr/share/augeas/lenses/dist";
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
lensPath = "/opt/homebrew/share/augeas/lenses/dist";
}
AugSettings augSettings = new AugSettings(root, lensPath);
_augeas = new Augeas(augSettings);
}
}

View File

@ -1,7 +0,0 @@
namespace Sharp.Augeas.Types;
public class AugArg<T>
{
public string Value;
public T Parent;
}

View File

@ -1,19 +0,0 @@
namespace Sharp.Augeas.Types;
public interface IAugConfig{};
public interface IArg { };
public interface IProxyData { };
public interface IVirtualHostData { };
public interface IDirectiveArg<T> : IArg { }
public interface IProxyArg : IArg, IProxyData { }
public interface IDirectiveConfig<T> : IAugConfig { }
public interface IVirtualHost : IAugConfig, IVirtualHostData { }
public interface IVirtualHostArg : IArg, IVirtualHostData { };

View File

@ -1,43 +0,0 @@
using System.Runtime.InteropServices;
namespace Sharp.Augeas.Test;
class AugSettings_Wrapper : IDisposable
{
private GCHandle m_loadPath_hand;
private string m_loadPath;
public AugSettings_Wrapper( string loadPath)
{
m_loadPath = new string(loadPath);
m_loadPath_hand = GCHandle.Alloc(m_loadPath, GCHandleType.Pinned);
}
public AugSettings GetUnamangedStruct()
{
AugSettings ret = new AugSettings();
ret.loadPath = (string) m_loadPath_hand.Target;
return ret;
}
#region IDisposable Members
~AugSettings_Wrapper()
{
Dispose();
}
bool disposed = false;
public void Dispose()
{
lock (this)
{
if (!disposed)
{
m_loadPath_hand.Free();
disposed = true;
}
}
}
#endregion
}