Skip to content

Commit

Permalink
Implemented optional tables parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Feb 1, 2024
1 parent 354dbe5 commit e0bb5d0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
6 changes: 5 additions & 1 deletion source/ditjson/Options.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommandLine;
using System.Collections.Generic;
using CommandLine;

namespace ditjson
{
Expand All @@ -9,5 +10,8 @@ internal class Options
{
[Option('n', "ntds", Required = true, Default = "", HelpText = "Path to ntds.dit file")]
public string Ntds { get; set; }

[Option('t', "tables", Required = false, Default = "", HelpText = "ntds.dit tables to include. Default: datatable, link_table")]
public IEnumerable<string> Tables { get; set; }
}
}
41 changes: 37 additions & 4 deletions source/ditjson/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using CommandLine;
Expand All @@ -20,6 +21,8 @@ internal static class Program
WriteIndented = true,
};

private static readonly string[] defaultTables = ["datatable", "link_table"];

/// <summary>
/// Application entry point
/// </summary>
Expand Down Expand Up @@ -61,11 +64,13 @@ internal static void RunOptions(Options opts)
Api.JetAttachDatabase(session, opts.Ntds, AttachDatabaseGrbit.ReadOnly);
Api.JetOpenDatabase(session, opts.Ntds, null, out var dbid, OpenDatabaseGrbit.ReadOnly);

var ntdsDictionary = new Dictionary<string, object>
var tablesToQuery = GetTablesToQuery(opts.Tables, session, dbid);

var ntdsDictionary = new Dictionary<string, object>();
foreach (var table in tablesToQuery)
{
["datatable"] = TableToList(session, dbid, "datatable"),
["linktable"] = TableToList(session, dbid, "link_table")
};
ntdsDictionary.Add(table, TableToList(session, dbid, table));
}

string json;
try
Expand All @@ -87,6 +92,34 @@ internal static void RunOptions(Options opts)
}
}

private static List<string> GetTablesToQuery(IEnumerable<string>? tablesInOptions, Session session, JET_DBID dbid)
{
var tablesInDb = Api.GetTableNames(session, dbid);

List<string> tablesToQuery;

if (tablesInOptions == null)
{
// Add only the default tables
tablesToQuery = new List<string>(defaultTables);
}
else
{
// If user asks all
if (tablesInOptions.Count() == 1 && tablesInOptions.First().Equals("*", StringComparison.Ordinal))
{
tablesToQuery = new List<string>(tablesInDb);
}
else
{
// if user asks oly specific tables
tablesToQuery = new List<string>(tablesInOptions.Where(t => tablesInDb.Contains(t)));
}
}

return tablesToQuery;
}

/// <summary>
/// Return the string format of a byte array.
/// </summary>
Expand Down

0 comments on commit e0bb5d0

Please sign in to comment.