From 6868e16d6ee0d428fc7d75edc7b81392eb5034e8 Mon Sep 17 00:00:00 2001 From: code liturgy Date: Sun, 6 Nov 2022 16:09:23 -0500 Subject: [PATCH] Proto: Typed Tree implementation for VirtualHost --- Sharp.Augeas/Augeas.cs | 29 +++++++++-- Sharp.Augeas/AugeasExtern.cs | 4 +- Sharp.Augeas/Types/AugConfig.cs | 2 +- Sharp.Augeas/VirtualHost/Argument.cs | 20 ++++++++ Sharp.Augeas/VirtualHost/Directive.cs | 7 +++ Sharp.Augeas/VirtualHost/DirectiveArg.cs | 6 --- Sharp.Augeas/VirtualHost/Node.cs | 50 +++++++++++++++++++ Sharp.Augeas/VirtualHost/Proxy.cs | 8 +++ Sharp.Augeas/VirtualHost/ProxyArg.cs | 6 --- Sharp.Augeas/VirtualHost/ProxyDirective.cs | 6 --- Sharp.Augeas/VirtualHost/VirtualHost.cs | 9 ++++ Sharp.Augeas/VirtualHost/VirtualHostArg.cs | 6 --- .../VirtualHost/VirtualHostDirective.cs | 6 --- Sharp.Augeas/VirtualHostTreeGenerator.cs | 38 ++++++++++++++ Sharp.Augeas/copyLibrary.sh | 2 +- 15 files changed, 160 insertions(+), 39 deletions(-) create mode 100644 Sharp.Augeas/VirtualHost/Argument.cs create mode 100644 Sharp.Augeas/VirtualHost/Directive.cs delete mode 100644 Sharp.Augeas/VirtualHost/DirectiveArg.cs create mode 100644 Sharp.Augeas/VirtualHost/Node.cs create mode 100644 Sharp.Augeas/VirtualHost/Proxy.cs delete mode 100644 Sharp.Augeas/VirtualHost/ProxyArg.cs delete mode 100644 Sharp.Augeas/VirtualHost/ProxyDirective.cs create mode 100644 Sharp.Augeas/VirtualHost/VirtualHost.cs delete mode 100644 Sharp.Augeas/VirtualHost/VirtualHostArg.cs delete mode 100644 Sharp.Augeas/VirtualHost/VirtualHostDirective.cs create mode 100644 Sharp.Augeas/VirtualHostTreeGenerator.cs diff --git a/Sharp.Augeas/Augeas.cs b/Sharp.Augeas/Augeas.cs index f82a7a3..477e170 100644 --- a/Sharp.Augeas/Augeas.cs +++ b/Sharp.Augeas/Augeas.cs @@ -31,6 +31,10 @@ namespace Sharp.Augeas public Augeas(AugSettings augSettings) { _augeas = init_aug(augSettings, NO_STDINC | NO_LOAD); + if (_augeas == IntPtr.Zero) + { + throw new InvalidOperationException("Augeas is not a valid instance."); + } } /// @@ -44,20 +48,35 @@ namespace Sharp.Augeas } /// - /// Finalize the augeas instance. + /// Get the exact value of the matchPath. + /// Returns a empty string if no match is valid. /// - public void Close() + /// Augeas path to filter out the configuration. + public string GetNode(string matchPath) + { + return new string(get_node(_augeas, matchPath)); + } + + /// + /// Close augeas pointer. + /// + private void Close() { close_aug(_augeas); } + /// - /// Finalize the augeas instance. + /// Loads a file. /// + /// Full path to the configuration file public void LoadFile(string configurationFilePath) { - if (_augeas == IntPtr.Zero) return; - load_file(_augeas, configurationFilePath); + bool success = load_file(_augeas, configurationFilePath); + if (!success) + { + throw new InvalidOperationException($"Unable to load {configurationFilePath}"); + } } /// diff --git a/Sharp.Augeas/AugeasExtern.cs b/Sharp.Augeas/AugeasExtern.cs index 35c5dae..81dcfb8 100644 --- a/Sharp.Augeas/AugeasExtern.cs +++ b/Sharp.Augeas/AugeasExtern.cs @@ -21,7 +21,7 @@ namespace Sharp.Augeas [DllImport(_libName)] public static extern void print_preview(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); [DllImport(_libName)] public static extern void print_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); [DllImport(_libName)] public static extern char* get_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern void defNode( AugSettings settings, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern void load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath); + [DllImport(_libName)] public static extern char* get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); + [DllImport(_libName)] public static extern bool load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath); } } diff --git a/Sharp.Augeas/Types/AugConfig.cs b/Sharp.Augeas/Types/AugConfig.cs index b0661a6..34088c8 100644 --- a/Sharp.Augeas/Types/AugConfig.cs +++ b/Sharp.Augeas/Types/AugConfig.cs @@ -14,6 +14,6 @@ public interface IProxyArg : IArg, IProxyData { } public interface IDirectiveConfig : IAugConfig { } -public interface IVirtualHostConfig : IAugConfig { } +public interface IVirtualHost : IAugConfig, IVirtualHostData { } public interface IVirtualHostArg : IArg, IVirtualHostData { }; \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/Argument.cs b/Sharp.Augeas/VirtualHost/Argument.cs new file mode 100644 index 0000000..05af516 --- /dev/null +++ b/Sharp.Augeas/VirtualHost/Argument.cs @@ -0,0 +1,20 @@ +namespace Sharp.Augeas; + +public class Argument : Node where T: Node +{ + public new T Parent; + + public string Value; + + public Argument(T parent, string value) + { + Parent = parent; + Children = null; + Value = value; + } + + public Argument() + { + + } +} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/Directive.cs b/Sharp.Augeas/VirtualHost/Directive.cs new file mode 100644 index 0000000..6e00548 --- /dev/null +++ b/Sharp.Augeas/VirtualHost/Directive.cs @@ -0,0 +1,7 @@ +namespace Sharp.Augeas; + +public class Directive : Node where T: Node +{ + public string Value; + public List>> Arguments; +} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/DirectiveArg.cs b/Sharp.Augeas/VirtualHost/DirectiveArg.cs deleted file mode 100644 index 78cb8e5..0000000 --- a/Sharp.Augeas/VirtualHost/DirectiveArg.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Sharp.Augeas.VirtualHost; - -public class DirectiveArg -{ - -} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/Node.cs b/Sharp.Augeas/VirtualHost/Node.cs new file mode 100644 index 0000000..3babfe9 --- /dev/null +++ b/Sharp.Augeas/VirtualHost/Node.cs @@ -0,0 +1,50 @@ +namespace Sharp.Augeas; + +public class Node +{ + public Node Parent; + public List Children; + + + public Node() + { + Parent = null; + Children = new List(); + } + public Node(Node parent) + { + Parent = parent; + Children = new List(); + } + + public T[] GetChildren() + { + return Children + .OfType() + .ToArray(); + } + + public Node(Node parent, List children) + { + Parent = parent; + Children = children; + } + + public void AddChild(Node node) + { + Children.Add(node); + } + + public void RemoveChild(Node child) + { + if (Children.Contains(child)) + { + Children.Remove(child); + } + } + + public void RemoveAllChildren() + { + Children = new List(); + } +} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/Proxy.cs b/Sharp.Augeas/VirtualHost/Proxy.cs new file mode 100644 index 0000000..7d8f0aa --- /dev/null +++ b/Sharp.Augeas/VirtualHost/Proxy.cs @@ -0,0 +1,8 @@ +namespace Sharp.Augeas; + +public class Proxy : Node +{ + public new VirtualHost Parent; + public List> Directives; + public List> Arguments; +} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/ProxyArg.cs b/Sharp.Augeas/VirtualHost/ProxyArg.cs deleted file mode 100644 index 0ff9153..0000000 --- a/Sharp.Augeas/VirtualHost/ProxyArg.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Sharp.Augeas.VirtualHost; - -public class ProxyArg -{ - -} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/ProxyDirective.cs b/Sharp.Augeas/VirtualHost/ProxyDirective.cs deleted file mode 100644 index 1895c56..0000000 --- a/Sharp.Augeas/VirtualHost/ProxyDirective.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Sharp.Augeas.VirtualHost; - -public class ProxyDirective -{ - -} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/VirtualHost.cs b/Sharp.Augeas/VirtualHost/VirtualHost.cs new file mode 100644 index 0000000..5b57c78 --- /dev/null +++ b/Sharp.Augeas/VirtualHost/VirtualHost.cs @@ -0,0 +1,9 @@ +namespace Sharp.Augeas +{ + public class VirtualHost + { + public List> Directives; + public List> Arguments; + } +} + diff --git a/Sharp.Augeas/VirtualHost/VirtualHostArg.cs b/Sharp.Augeas/VirtualHost/VirtualHostArg.cs deleted file mode 100644 index ffc273e..0000000 --- a/Sharp.Augeas/VirtualHost/VirtualHostArg.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Sharp.Augeas.VirtualHost; - -public class VirtualHostArg -{ - -} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHost/VirtualHostDirective.cs b/Sharp.Augeas/VirtualHost/VirtualHostDirective.cs deleted file mode 100644 index f93aeff..0000000 --- a/Sharp.Augeas/VirtualHost/VirtualHostDirective.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Sharp.Augeas.VirtualHost; - -public class VirtualHostDirective -{ - -} \ No newline at end of file diff --git a/Sharp.Augeas/VirtualHostTreeGenerator.cs b/Sharp.Augeas/VirtualHostTreeGenerator.cs new file mode 100644 index 0000000..073129a --- /dev/null +++ b/Sharp.Augeas/VirtualHostTreeGenerator.cs @@ -0,0 +1,38 @@ +namespace Sharp.Augeas; + +public static class VirtualHostTreeGenerator +{ + public static VirtualHost Generate(Dictionary dic) + { + var keys = dic.Keys; + var virtualHost = new VirtualHost(); + foreach (var key in keys) + { + + } + + return virtualHost; + } + + private static bool IsProxy(string key) + { + return key.Contains("f/VirtualHost/Proxy/"); + } + private static bool IsVirtualHostArg(string key) + { + return key.Contains("f/VirtualHost/arg;"); + } + private static bool IsProxyDirective(string key) + { + return key.Contains("f/VirtualHost/Proxy/directive"); + } + private static bool IsVirtualHostDirective(string key) + { + return key.Contains("f/VirtualHost/directive"); + } + private static bool IsDirectiveArg(string key) + { + return key.Contains("f/VirtualHost/directive"); + } + +} \ No newline at end of file diff --git a/Sharp.Augeas/copyLibrary.sh b/Sharp.Augeas/copyLibrary.sh index bd60c6d..4552d99 100755 --- a/Sharp.Augeas/copyLibrary.sh +++ b/Sharp.Augeas/copyLibrary.sh @@ -10,7 +10,7 @@ DESTIN=$SCRIPT_DIR/bin/Debug/net6.0 # Set the $CLAUG_LIB_PATH to the PATH where the claugeas library is located before running this script if [[ -z "${CLAUG_LIB_PATH}" ]]; then - echo "error: set the CLAUG_LIB_PATH in the ${LIB_FILE} location." + echo "error: set the CLAUG_LIB_PATH with the ${LIB_FILE} location." echo exit 2 fi