/// <summary> /// Checks that the all the arguments are valid /// </summary> /// <returns><c>true</c>, if all arguments are valid <c>false</c> otherwise.</returns> /// <param name="arguments">User Settings</param> private static bool IsValid(ProcUpdaterSettings settings) { return (!string.IsNullOrEmpty(settings.ConnectionString) && !string.IsNullOrEmpty(settings.StoredProceduresPath) && new DirectoryInfo(settings.StoredProceduresPath).Exists); }
/// <summary> /// Creates a <see cref="ProcUpdaterSettings"/> based on command line arguments and json config file /// </summary> /// <returns>ARguments set by the user</returns> /// <param name="commandArgs">Command arguments received by the main method</param> private static ProcUpdaterSettings LoadArguments(string[] commandArgs) { try { //Setup the configuration sources var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddCommandLine(commandArgs, new Dictionary <string, string> { { "-conn", ProcUpdaterSettings.ConnectionStringKey }, { "-path", ProcUpdaterSettings.StoredProceduresPathKey }, { "-alive", ProcUpdaterSettings.StayAliveKey }, { "-watch", ProcUpdaterSettings.FileWatchKey }, { "-verbose", ProcUpdaterSettings.VerboseKey } }); var configuration = builder.Build(); var procArguments = new ProcUpdaterSettings(); configuration.GetSection("ProcUpdater").Bind(procArguments); return(procArguments); } catch (FormatException) { return(null); } }
static void Main(string[] args) { try { _settings = LoadArguments(args); if (_settings != null && IsValid(_settings)) { RunDirectoryScripts(new DirectoryInfo(_settings.StoredProceduresPath)); //Setup Watcher if (_settings.FileWatch) { SetupWatcher(); } else if (_settings.StayAlive) { while (true) { Console.WriteLine("Waiting for new commands (run or quit)"); switch (Console.ReadLine()) { case ProcUpdaterSettings.RunKeyword: RunDirectoryScripts(new DirectoryInfo(_settings.StoredProceduresPath)); break; case ProcUpdaterSettings.ExitKeyword: return; } } } } else { PrintHelp(); } } catch (Exception ex) { Console.WriteLine("ERROR"); Console.WriteLine(ex.GetType().Name); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Console.ReadLine(); } }