diff --git a/Sharp.Augeas.Test/AugeasTests.cs b/Sharp.Augeas.Test/AugeasTests.cs index 7e14622..03efea3 100644 --- a/Sharp.Augeas.Test/AugeasTests.cs +++ b/Sharp.Augeas.Test/AugeasTests.cs @@ -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); - } diff --git a/Sharp.Augeas.Test/copyLibrary.sh b/Sharp.Augeas.Test/copy_claugeas.sh similarity index 100% rename from Sharp.Augeas.Test/copyLibrary.sh rename to Sharp.Augeas.Test/copy_claugeas.sh diff --git a/Sharp.Augeas/AugFlags.cs b/Sharp.Augeas/AugFlags.cs deleted file mode 100644 index 90b2193..0000000 --- a/Sharp.Augeas/AugFlags.cs +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/Sharp.Augeas/AugSettings.cs b/Sharp.Augeas/Augeas/AugSettings.cs similarity index 100% rename from Sharp.Augeas/AugSettings.cs rename to Sharp.Augeas/Augeas/AugSettings.cs diff --git a/Sharp.Augeas/Augeas.cs b/Sharp.Augeas/Augeas/Augeas.cs similarity index 80% rename from Sharp.Augeas/Augeas.cs rename to Sharp.Augeas/Augeas/Augeas.cs index 2b2b5ba..4e43d78 100644 --- a/Sharp.Augeas/Augeas.cs +++ b/Sharp.Augeas/Augeas/Augeas.cs @@ -6,10 +6,21 @@ namespace Sharp.Augeas { /// /// Augeas Core containing the settings of this instance. - /// We can have multiple augeas cores during the execution. /// 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 _loadedFiles = new(); @@ -93,20 +104,18 @@ namespace Sharp.Augeas /// Loads a file. /// /// Full path to the configuration file - 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; } /// @@ -118,7 +127,14 @@ namespace Sharp.Augeas { print_tree(_augeas, matchPath); } - + + private void FreeString(IntPtr str) + { + if (str != IntPtr.Zero) + { + free_str(str); + } + } /// /// Returns the tree given the augeas configuration path. @@ -130,8 +146,6 @@ namespace Sharp.Augeas var result = new Dictionary(); 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; } + + + /// + /// Gets a preview of the configuration segment of . + /// The file must be loaded first. + /// + /// Augeas path of the configuration + /// + 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); } - } } diff --git a/Sharp.Augeas/AugeasExtern.cs b/Sharp.Augeas/Augeas/AugeasExtern.cs similarity index 89% rename from Sharp.Augeas/AugeasExtern.cs rename to Sharp.Augeas/Augeas/AugeasExtern.cs index 8904bce..ec1c89b 100644 --- a/Sharp.Augeas/AugeasExtern.cs +++ b/Sharp.Augeas/Augeas/AugeasExtern.cs @@ -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); diff --git a/Sharp.Augeas/HostManager.cs b/Sharp.Augeas/HostManager.cs deleted file mode 100644 index 4e17bd0..0000000 --- a/Sharp.Augeas/HostManager.cs +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/Sharp.Augeas/Types/AugArg.cs b/Sharp.Augeas/Types/AugArg.cs deleted file mode 100644 index 5c77c3c..0000000 --- a/Sharp.Augeas/Types/AugArg.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Sharp.Augeas.Types; - -public class AugArg -{ - public string Value; - public T Parent; -} \ No newline at end of file diff --git a/Sharp.Augeas/Types/AugConfig.cs b/Sharp.Augeas/Types/AugConfig.cs deleted file mode 100644 index 34088c8..0000000 --- a/Sharp.Augeas/Types/AugConfig.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Sharp.Augeas.Types; - -public interface IAugConfig{}; - -public interface IArg { }; - -public interface IProxyData { }; - -public interface IVirtualHostData { }; - -public interface IDirectiveArg : IArg { } - -public interface IProxyArg : IArg, IProxyData { } - -public interface IDirectiveConfig : IAugConfig { } - -public interface IVirtualHost : IAugConfig, IVirtualHostData { } - -public interface IVirtualHostArg : IArg, IVirtualHostData { }; \ No newline at end of file diff --git a/Sharp.Augeas/test/AugSettings_Wrapper.cs b/Sharp.Augeas/test/AugSettings_Wrapper.cs deleted file mode 100644 index 3c402c4..0000000 --- a/Sharp.Augeas/test/AugSettings_Wrapper.cs +++ /dev/null @@ -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 -} \ No newline at end of file