feat(porttracker): add DB access layer and init command
Add Database.cs with SQLite connection management and helper methods (Execute, ExecuteScalar, Query), Schema.cs for schema initialization, and replace default Program.cs with System.CommandLine v3 CLI featuring global --db/--schema options and an init command.
This commit is contained in:
@@ -1,2 +1,36 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
using System.CommandLine;
|
||||
using NatsNet.PortTracker.Data;
|
||||
|
||||
var dbOption = new Option<string>("--db")
|
||||
{
|
||||
Description = "Path to the SQLite database file",
|
||||
DefaultValueFactory = _ => Path.Combine(Directory.GetCurrentDirectory(), "porting.db"),
|
||||
Recursive = true
|
||||
};
|
||||
|
||||
var schemaOption = new Option<string>("--schema")
|
||||
{
|
||||
Description = "Path to the SQL schema file",
|
||||
DefaultValueFactory = _ => Path.Combine(Directory.GetCurrentDirectory(), "porting-schema.sql"),
|
||||
Recursive = true
|
||||
};
|
||||
|
||||
var rootCommand = new RootCommand("NATS .NET Porting Tracker");
|
||||
rootCommand.Add(dbOption);
|
||||
rootCommand.Add(schemaOption);
|
||||
|
||||
// init command
|
||||
var initCommand = new Command("init", "Create or reset the database schema");
|
||||
initCommand.SetAction(parseResult =>
|
||||
{
|
||||
var dbPath = parseResult.GetValue(dbOption)!;
|
||||
var schemaPath = parseResult.GetValue(schemaOption)!;
|
||||
using var db = new Database(dbPath);
|
||||
Schema.Initialize(db, schemaPath);
|
||||
Console.WriteLine($"Database initialized at {dbPath}");
|
||||
});
|
||||
|
||||
rootCommand.Add(initCommand);
|
||||
|
||||
var parseResult = rootCommand.Parse(args);
|
||||
return await parseResult.InvokeAsync();
|
||||
|
||||
Reference in New Issue
Block a user