Proto: Typed Tree implementation for VirtualHost

This commit is contained in:
code liturgy 2022-11-06 16:09:23 -05:00
parent 492b37747d
commit 6868e16d6e
15 changed files with 160 additions and 39 deletions

View File

@ -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.");
}
}
/// <summary>
@ -44,20 +48,35 @@ namespace Sharp.Augeas
}
/// <summary>
/// Finalize the augeas instance.
/// Get the exact value of the matchPath.
/// Returns a empty string if no match is valid.
/// </summary>
public void Close()
/// <param name="matchPath">Augeas path to filter out the configuration.</param>
public string GetNode(string matchPath)
{
return new string(get_node(_augeas, matchPath));
}
/// <summary>
/// Close augeas pointer.
/// </summary>
private void Close()
{
close_aug(_augeas);
}
/// <summary>
/// Finalize the augeas instance.
/// Loads a file.
/// </summary>
/// <param name="configurationFilePath">Full path to the configuration file</param>
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}");
}
}
/// <summary>

View File

@ -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);
}
}

View File

@ -14,6 +14,6 @@ public interface IProxyArg : IArg, IProxyData { }
public interface IDirectiveConfig<T> : IAugConfig { }
public interface IVirtualHostConfig : IAugConfig { }
public interface IVirtualHost : IAugConfig, IVirtualHostData { }
public interface IVirtualHostArg : IArg, IVirtualHostData { };

View File

@ -0,0 +1,20 @@
namespace Sharp.Augeas;
public class Argument<T> : 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()
{
}
}

View File

@ -0,0 +1,7 @@
namespace Sharp.Augeas;
public class Directive<T> : Node where T: Node
{
public string Value;
public List<Argument<Directive<T>>> Arguments;
}

View File

@ -1,6 +0,0 @@
namespace Sharp.Augeas.VirtualHost;
public class DirectiveArg
{
}

View File

@ -0,0 +1,50 @@
namespace Sharp.Augeas;
public class Node
{
public Node Parent;
public List<Node> Children;
public Node()
{
Parent = null;
Children = new List<Node>();
}
public Node(Node parent)
{
Parent = parent;
Children = new List<Node>();
}
public T[] GetChildren<T>()
{
return Children
.OfType<T>()
.ToArray();
}
public Node(Node parent, List<Node> 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<Node>();
}
}

View File

@ -0,0 +1,8 @@
namespace Sharp.Augeas;
public class Proxy : Node
{
public new VirtualHost Parent;
public List<Directive<Proxy>> Directives;
public List<Argument<Proxy>> Arguments;
}

View File

@ -1,6 +0,0 @@
namespace Sharp.Augeas.VirtualHost;
public class ProxyArg
{
}

View File

@ -1,6 +0,0 @@
namespace Sharp.Augeas.VirtualHost;
public class ProxyDirective
{
}

View File

@ -0,0 +1,9 @@
namespace Sharp.Augeas
{
public class VirtualHost
{
public List<Directive<Proxy>> Directives;
public List<Argument<Proxy>> Arguments;
}
}

View File

@ -1,6 +0,0 @@
namespace Sharp.Augeas.VirtualHost;
public class VirtualHostArg
{
}

View File

@ -1,6 +0,0 @@
namespace Sharp.Augeas.VirtualHost;
public class VirtualHostDirective
{
}

View File

@ -0,0 +1,38 @@
namespace Sharp.Augeas;
public static class VirtualHostTreeGenerator
{
public static VirtualHost Generate(Dictionary<string,string> 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");
}
}

View File

@ -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