From 3bbf36e69f1d3dc5510f46cdd562ef899f96121f Mon Sep 17 00:00:00 2001 From: Michal Checinski Date: Fri, 1 Sep 2023 17:20:36 +0200 Subject: [PATCH] Add optins parsing nuget for msSqlMigratorUtility --- .../MsSqlMigratorUtility.csproj | 1 + util/MsSqlMigratorUtility/Program.cs | 75 ++++++------------- util/MsSqlMigratorUtility/packages.lock.json | 9 +++ 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj b/util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj index 0e423b150d..7f19d67459 100644 --- a/util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj +++ b/util/MsSqlMigratorUtility/MsSqlMigratorUtility.csproj @@ -10,6 +10,7 @@ + diff --git a/util/MsSqlMigratorUtility/Program.cs b/util/MsSqlMigratorUtility/Program.cs index 9886164a09..0207d450d0 100644 --- a/util/MsSqlMigratorUtility/Program.cs +++ b/util/MsSqlMigratorUtility/Program.cs @@ -1,5 +1,6 @@ using Bit.Migrator; using Microsoft.Extensions.Logging; +using CommandDotNet; internal class Program { @@ -7,56 +8,20 @@ internal class Program private static int Main(string[] args) { - if (args.Length == 0) - { - Console.WriteLine("Please enter a database connection string argument."); - WriteUsageToConsole(); - return 1; - } - - if (args.Length == 1 && (args[0] == "--verbose" || args[0] == "-v")) - { - Console.WriteLine($"Please enter a database connection string argument before {args[0]} option."); - WriteUsageToConsole(); - return 1; - } - - var databaseConnectionString = args[0]; - - ParseParameters(args); - - var verbose = false; - - if (Parameters.ContainsKey("--verbose") || Parameters.ContainsKey("-v")) - { - verbose = true; - } - - var rerunable = false; - - if (Parameters.ContainsKey("--rerunable") || Parameters.ContainsKey("-r")) - { - rerunable = true; - } - - - var folderName = ""; - - if (Parameters.ContainsKey("--folder") || Parameters.ContainsKey("-f")) - { - folderName = Parameters["--folder"] ?? Parameters["-f"]; - } - - var success = MigrateDatabase(databaseConnectionString, verbose, rerunable, folderName); - - if (!success) - { - return -1; - } - - return 0; + return new AppRunner().Run(args); } + [DefaultCommand] + public void Execute( + [Operand(Description = "Database connection string")] + string databaseConnectionString, + [Option('v', "verbose", Description = "Enable verbose output of migrator logs")] + bool verbose = false, + [Option('r', "rerunable", Description = "Mark scripts as rerunable")] + bool rerunable = false, + [Option('f', "folder", Description = "Folder name of database scripts")] + string folderName = "DbScripts") => MigrateDatabase(databaseConnectionString, verbose, rerunable, folderName); + private static void WriteUsageToConsole() { Console.WriteLine("Usage: MsSqlMigratorUtility "); @@ -67,11 +32,13 @@ internal class Program private static bool MigrateDatabase(string databaseConnectionString, bool verbose = false, bool rerunable = false, string folderName = "") { + Console.WriteLine($"rerunable: {rerunable}"); + Console.WriteLine($"folderName: {folderName}"); var logger = CreateLogger(verbose); var migrator = new DbMigrator(databaseConnectionString, logger); bool success = false; - if (string.IsNullOrWhiteSpace(folderName)) + if (!string.IsNullOrWhiteSpace(folderName)) { success = migrator.MigrateMsSqlDatabaseWithRetries(verbose, rerunable, folderName); } @@ -114,8 +81,14 @@ internal class Program { continue; } - - Parameters.Add(args[i].Substring(1), args[i + 1]); + if (i + 1 <= args.Length && !args[i + 1].StartsWith("-")) + { + Parameters.Add(args[i].Substring(1), args[i + 1]); + } + else + { + Parameters.Add(args[i].Substring(1), null); + } } } } diff --git a/util/MsSqlMigratorUtility/packages.lock.json b/util/MsSqlMigratorUtility/packages.lock.json index bbe3aebd9b..93979ee680 100644 --- a/util/MsSqlMigratorUtility/packages.lock.json +++ b/util/MsSqlMigratorUtility/packages.lock.json @@ -2,6 +2,15 @@ "version": 1, "dependencies": { "net6.0": { + "CommandDotNet": { + "type": "Direct", + "requested": "[7.0.2, )", + "resolved": "7.0.2", + "contentHash": "sFfqn4T6Ux4AbGnhS+BZvf9iVeP6b9p9bwaMT8nsefVcYH2tC9MHj3AoY+GG0nnBPmFawRqdgud08cBjBdPByQ==", + "dependencies": { + "Microsoft.CSharp": "4.7.0" + } + }, "Microsoft.Extensions.Logging": { "type": "Direct", "requested": "[6.0.0, )",