refactor example
This commit is contained in:
parent
6868e16d6e
commit
63976544e5
|
@ -15,13 +15,16 @@ namespace Sharp.Augeas
|
||||||
{
|
{
|
||||||
private readonly IntPtr _augeas;
|
private readonly IntPtr _augeas;
|
||||||
|
|
||||||
|
private HashSet<string> _loadedFiles = new HashSet<string>();
|
||||||
|
|
||||||
|
#region Constructor / Destructor
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Augeas Core Destructor
|
/// Augeas Core Destructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
~Augeas()
|
~Augeas()
|
||||||
{
|
{
|
||||||
// Effectively free the pointer
|
close_aug(_augeas);
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -37,6 +40,12 @@ namespace Sharp.Augeas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Augeas Internal Api
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prints a preview of the desired segment in <see cref="matchPath"/>
|
/// Prints a preview of the desired segment in <see cref="matchPath"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -54,29 +63,28 @@ namespace Sharp.Augeas
|
||||||
/// <param name="matchPath">Augeas path to filter out the configuration.</param>
|
/// <param name="matchPath">Augeas path to filter out the configuration.</param>
|
||||||
public string GetNode(string matchPath)
|
public string GetNode(string matchPath)
|
||||||
{
|
{
|
||||||
return new string(get_node(_augeas, matchPath));
|
var res = get_node(_augeas, matchPath);
|
||||||
|
return res != IntPtr.Zero ? Marshal.PtrToStringAnsi(res) : string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Close augeas pointer.
|
|
||||||
/// </summary>
|
|
||||||
private void Close()
|
|
||||||
{
|
|
||||||
close_aug(_augeas);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads a file.
|
/// Loads a file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configurationFilePath">Full path to the configuration file</param>
|
/// <param name="configurationFilePath">Full path to the configuration file</param>
|
||||||
public void LoadFile(string configurationFilePath)
|
public void LoadFile(string configurationFilePath)
|
||||||
{
|
{
|
||||||
|
if (_loadedFiles.Contains(configurationFilePath))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool success = load_file(_augeas, configurationFilePath);
|
bool success = load_file(_augeas, configurationFilePath);
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Unable to load {configurationFilePath}");
|
throw new InvalidOperationException($"Unable to load {configurationFilePath}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_loadedFiles.Add(configurationFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -99,7 +107,7 @@ namespace Sharp.Augeas
|
||||||
{
|
{
|
||||||
var result = new Dictionary<string, string>();
|
var result = new Dictionary<string, string>();
|
||||||
var raw = get_tree(_augeas, matchPath);
|
var raw = get_tree(_augeas, matchPath);
|
||||||
string sb = Marshal.PtrToStringAnsi((IntPtr)raw);
|
string sb = Marshal.PtrToStringAnsi(raw);
|
||||||
if (sb == null) return result;
|
if (sb == null) return result;
|
||||||
free_str(raw);
|
free_str(raw);
|
||||||
var lines = sb.Split(";ENDL;");
|
var lines = sb.Split(";ENDL;");
|
||||||
|
@ -116,18 +124,9 @@ namespace Sharp.Augeas
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void Benchmark(Action act, int iterations)
|
|
||||||
{
|
|
||||||
GC.Collect();
|
|
||||||
act.Invoke(); // run once outside of loop to avoid initialization costs
|
|
||||||
Stopwatch sw = Stopwatch.StartNew();
|
|
||||||
for (int i = 0; i < iterations; i++)
|
|
||||||
{
|
|
||||||
act.Invoke();
|
|
||||||
}
|
|
||||||
sw.Stop();
|
|
||||||
Console.WriteLine((sw.ElapsedMilliseconds / iterations).ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,19 +9,34 @@ namespace Sharp.Augeas
|
||||||
[SuppressUnmanagedCodeSecurity]
|
[SuppressUnmanagedCodeSecurity]
|
||||||
public static unsafe partial class AugeasExtern
|
public static unsafe partial class AugeasExtern
|
||||||
{
|
{
|
||||||
[DllImport(_libName)] public static extern IntPtr init_aug( AugSettings settings, int flags);
|
[DllImport(_libName)]
|
||||||
[DllImport(_libName)] public static extern void close_aug (IntPtr aug);
|
public static extern IntPtr init_aug(AugSettings settings, int flags);
|
||||||
[DllImport(_libName)] public static extern void free_str (char* str);
|
|
||||||
|
[DllImport(_libName)]
|
||||||
|
public static extern void close_aug(IntPtr aug);
|
||||||
|
|
||||||
|
[DllImport(_libName)]
|
||||||
|
public static extern void free_str(IntPtr str);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Used by DllImport to load the native library
|
/// Used by DllImport to load the native library
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private const string _libName = "clAugeas";
|
private const string _libName = "clAugeas";
|
||||||
|
|
||||||
/// <summary>Test calling</summary>
|
/// <summary>Test calling</summary>
|
||||||
[DllImport(_libName)] public static extern void print_preview(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
[DllImport(_libName)]
|
||||||
[DllImport(_libName)] public static extern void print_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
public static extern void print_preview(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 char* get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
[DllImport(_libName)]
|
||||||
[DllImport(_libName)] public static extern bool load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath);
|
public static extern void print_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
||||||
|
|
||||||
|
[DllImport(_libName)]
|
||||||
|
public static extern IntPtr get_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
||||||
|
|
||||||
|
[DllImport(_libName)]
|
||||||
|
public static extern IntPtr get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath);
|
||||||
|
|
||||||
|
[DllImport(_libName)]
|
||||||
|
public static extern bool load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Sharp.Augeas;
|
||||||
|
|
||||||
|
public static class Benchmark
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void Function(Action act, int iterations)
|
||||||
|
{
|
||||||
|
GC.Collect();
|
||||||
|
act.Invoke(); // run once outside of loop to avoid initialization costs
|
||||||
|
Stopwatch sw = Stopwatch.StartNew();
|
||||||
|
for (int i = 0; i < iterations; i++)
|
||||||
|
{
|
||||||
|
act.Invoke();
|
||||||
|
}
|
||||||
|
sw.Stop();
|
||||||
|
Console.WriteLine((sw.ElapsedMilliseconds / iterations).ToString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,17 +19,24 @@ if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
AugSettings augSettings = new AugSettings(root, lensPath);
|
AugSettings augSettings = new AugSettings(root, lensPath);
|
||||||
|
|
||||||
var augeas = new Augeas(augSettings);
|
var augeas = new Augeas(augSettings);
|
||||||
// Calling extern functions
|
|
||||||
|
|
||||||
string clSiteMatchPath = "/files/etc/apache2/sites-available/00-ci.codeliturgy.com.conf";
|
var virtualHostConfig = "/etc/apache2/sites-available/00-ci.codeliturgy.com.conf";
|
||||||
|
|
||||||
//AugeasCore.Benchmark( () => augeasCore.PrintAugTree(clSiteMatchPath , clSiteFilepath), 10);
|
augeas.LoadFile(virtualHostConfig);
|
||||||
//AugeasCore.Benchmark( () => augeasCore.PrintPreview(clSiteMatchPath , clSiteFilepath), 10);
|
|
||||||
|
|
||||||
augeas.LoadFile("/etc/apache2/sites-available/00-ci.codeliturgy.com.conf");
|
void PrintVirtualHostTree(string configFilePath)
|
||||||
|
{
|
||||||
|
string virtualHostTree = $"/files{configFilePath}/VirtualHost/*";
|
||||||
|
augeas.PrintAugTree(virtualHostTree);
|
||||||
|
}
|
||||||
|
|
||||||
augeas.PrintAugTree(clSiteMatchPath);
|
void PrintVirtualHostProxyTree(string configFilePath)
|
||||||
var tree = augeas.GetTree(clSiteMatchPath);
|
{
|
||||||
|
string virtualHostProxyMatchPath = $"/files{configFilePath}/VirtualHost/Proxy/*";
|
||||||
|
augeas.PrintAugTree(virtualHostProxyMatchPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintVirtualHostTree(virtualHostConfig);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue