diff --git a/yuniql-cli/BaseConfigOption.cs b/yuniql-cli/BaseConfigOption.cs new file mode 100644 index 00000000..a45c3dd9 --- /dev/null +++ b/yuniql-cli/BaseConfigOption.cs @@ -0,0 +1,17 @@ +using CommandLine; + +namespace Yuniql.CLI +{ + public class BaseConfigOption : BasePlatformOption + { + //yuniql -o json + [Option('o', "output", Required = false, HelpText = "The choosen Output format")] + public string Output { get; set; } + + //yuniql -a true | --auto-create-db true + [Option('a', "auto-create-db", Required = false, HelpText = "Create database automatically.")] + public bool? IsAutoCreateDatabase { get; set; } + } +} + + diff --git a/yuniql-cli/BaseOption.cs b/yuniql-cli/BaseOption.cs index 950ce958..fa4661cd 100644 --- a/yuniql-cli/BaseOption.cs +++ b/yuniql-cli/BaseOption.cs @@ -10,7 +10,7 @@ public class BaseOption //yuniql -d | --debug [Option('d', "debug", Required = false, HelpText = "Print debug information including all raw scripts.")] - public bool IsDebug { get; set; } + public bool? IsDebug { get; set; } //yuniql --trace-sensitive-data [Option("trace-sensitive-data", Required = false, HelpText = "Include sensitive data like connection string in the log messages.", Default = false)] diff --git a/yuniql-cli/BasePlatformOption.cs b/yuniql-cli/BasePlatformOption.cs index 3d6d7c5d..d1b3ee67 100644 --- a/yuniql-cli/BasePlatformOption.cs +++ b/yuniql-cli/BasePlatformOption.cs @@ -3,8 +3,8 @@ namespace Yuniql.CLI { public class BasePlatformOption : BaseOption - { - //yuniql -d | --debug + { + //yuniql -p | --platform [Option(longName: "platform", Required = false, HelpText = "Target database platform. Default is sqlserver.")] public string Platform { get; set; } diff --git a/yuniql-cli/CommandLineService.cs b/yuniql-cli/CommandLineService.cs index b5a8ae6c..45473a4a 100644 --- a/yuniql-cli/CommandLineService.cs +++ b/yuniql-cli/CommandLineService.cs @@ -40,7 +40,7 @@ private Configuration SetupRunConfiguration(BaseRunPlatformOption opts, bool isV var tokens = opts.Tokens.Select(t => new KeyValuePair(t.Split("=")[0], t.Split("=")[1])).ToList(); configuration.Workspace = opts.Workspace; - configuration.IsDebug = opts.IsDebug; + configuration.IsDebug = opts.IsDebug?? false; configuration.Platform = platform; configuration.ConnectionString = opts.ConnectionString; @@ -82,7 +82,7 @@ public int RunCheckOption(CheckOption opts) } catch (Exception ex) { - return OnException(ex, "Failed to execute ping function", opts.IsDebug); + return OnException(ex, "Failed to execute ping function", opts.IsDebug??false); } return 0; @@ -171,7 +171,7 @@ public int RunListOption(ListOption opts) var configuration = Configuration.Instance; configuration.Workspace = opts.Workspace; - configuration.IsDebug = opts.IsDebug; + configuration.IsDebug = opts.IsDebug??false; configuration.Platform = platform; configuration.ConnectionString = opts.ConnectionString; @@ -222,7 +222,7 @@ public int RunEraseOption(EraseOption opts) var configuration = Configuration.Instance; configuration.Workspace = opts.Workspace; - configuration.IsDebug = opts.IsDebug; + configuration.IsDebug = opts.IsDebug??false; configuration.Platform = platform; configuration.ConnectionString = opts.ConnectionString; @@ -255,7 +255,7 @@ public int RunDestroyOption(DestroyOption opts) var configuration = Configuration.Instance; configuration.Workspace = opts.Workspace; - configuration.IsDebug = opts.IsDebug; + configuration.IsDebug = opts.IsDebug??false; configuration.Platform = platform; configuration.ConnectionString = connectionString; @@ -368,10 +368,65 @@ public int RunArchiveOption(ArchiveOption opts) return OnException(ex, "Failed to execute archive function", opts.IsDebug); } } + public int RunConfigOption(ConfigOption opts) + { + try + + { + IPrinter versionPrettyPrint; + if (opts.Output != null && opts.Output.Equals("json", + StringComparison.OrdinalIgnoreCase)) + versionPrettyPrint = new JsonPrinter(); + else + versionPrettyPrint = new TablePrinter("Property", "Value", "Source"); + + // platform + var platformValue = _configurationService.GetValueOrDefault(opts.Platform, ENVIRONMENT_VARIABLE.YUNIQL_PLATFORM, defaultValue: SUPPORTED_DATABASES.SQLSERVER); + var platformSource = opts.Platform != null ? Source.CMD_LINE_OPTIONS : + _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_PLATFORM) != null ? Source.ENVIRONMENT_VARIABLE + : Source.DEFAULT; + versionPrettyPrint.AddRow("Platform", platformValue, platformSource); + //workspace + var workspaceValue = _configurationService.GetValueOrDefault(opts.Workspace, ENVIRONMENT_VARIABLE.YUNIQL_WORKSPACE,"undefined"); + var workspaceSource = opts.Workspace != null ? Source.CMD_LINE_OPTIONS : + _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_WORKSPACE) != null ? Source.ENVIRONMENT_VARIABLE + : Source.DEFAULT; + versionPrettyPrint.AddRow("Workspace", workspaceValue, workspaceSource); + + //connection string + var connectionStringValue = _configurationService.GetValueOrDefault(opts.ConnectionString, ENVIRONMENT_VARIABLE.YUNIQL_CONNECTION_STRING,"undefined"); + var connectionStringSource = opts.ConnectionString != null ? Source.CMD_LINE_OPTIONS : + _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_CONNECTION_STRING) != null ? Source.ENVIRONMENT_VARIABLE + : Source.DEFAULT; + versionPrettyPrint.AddRow("ConnectionString", connectionStringValue, connectionStringSource); + + //connection string + var IsDebugValue = opts.IsDebug?? false; + var IsDebugSource = opts.IsDebug != null ? Source.CMD_LINE_OPTIONS : Source.DEFAULT; + versionPrettyPrint.AddRow("IsDebug", IsDebugValue, IsDebugSource); + + // Auto Create Database + var IsAutoCreateDatabaseValue = opts.IsAutoCreateDatabase ?? false; + var IsAutoCreateDatabaseSource = opts.IsAutoCreateDatabase != null ? Source.CMD_LINE_OPTIONS : Source.DEFAULT; + versionPrettyPrint.AddRow("IsAutoCreateDatabase", IsAutoCreateDatabaseValue, IsAutoCreateDatabaseSource); + + //print table + versionPrettyPrint.Print(); + _traceService.Success($"Listed all configuration variables successfully"); + + return 0; + } + catch (Exception ex) + { + return OnException(ex, "Failed to execute config function", opts.IsDebug); + } + } + - private int OnException(Exception exception, string headerMessage, bool debug) + private int OnException(Exception exception, string headerMessage, bool? debug) { - var stackTraceMessage = debug ? exception.ToString().Replace(exception.Message, string.Empty) + bool debugOption = debug ?? false; + var stackTraceMessage = debugOption? exception.ToString().Replace(exception.Message, string.Empty) : $"{exception.Message} {exception.InnerException?.Message}"; _traceService.Error($"{headerMessage}. {exception.Message}{Environment.NewLine}" + diff --git a/yuniql-cli/ConfigOption.cs b/yuniql-cli/ConfigOption.cs new file mode 100644 index 00000000..a33ab4e6 --- /dev/null +++ b/yuniql-cli/ConfigOption.cs @@ -0,0 +1,9 @@ +using CommandLine; + +namespace Yuniql.CLI +{ + [Verb("config", HelpText = "Print the environment setup in the command line")] + public class ConfigOption : BaseConfigOption + { + } +} diff --git a/yuniql-cli/JsonPrinter.cs b/yuniql-cli/JsonPrinter.cs new file mode 100644 index 00000000..d7104606 --- /dev/null +++ b/yuniql-cli/JsonPrinter.cs @@ -0,0 +1,43 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using Yuniql.Core; + +namespace Yuniql.CLI +{ + /// + /// This class is used to print configuration variable as json text + /// + public class JsonPrinter : IPrinter + { + private readonly string[] titles = new string[] { "property","value","source"}; + private readonly List rows = new List(); + + public void AddRow(params object[] row) + { + if (row.Length != titles.Length) + { + throw new Exception($"Added row length [{row.Length}] is not equal to title row length [{titles.Length}]"); + } + rows.Add(row.Select(o => o.ToString()).ToArray()); + } + + public void Print() + { + var jarray = new JArray("Properties"); + var jproperty = new JProperty("Properties", jarray); + var jobject = new JObject(jproperty); + foreach (var row in rows) + { + JObject obj = new JObject(); + for (int i = 0; i < row.Length; i++) + { + obj.Add(new JProperty(titles[i], row[i])); + } + jarray.Add(obj); + } + Console.WriteLine(jobject.ToString()); + } + } +} diff --git a/yuniql-cli/Program.cs b/yuniql-cli/Program.cs index 22dc8264..06cc3595 100644 --- a/yuniql-cli/Program.cs +++ b/yuniql-cli/Program.cs @@ -1,5 +1,6 @@ using CommandLine; using System; +using System.Collections; using System.Reflection; using Yuniql.Core; using Yuniql.Extensibility; @@ -30,7 +31,6 @@ public static int Main(string[] args) environmentService, traceService, configurationService); - var resultCode = Parser.Default .ParseArguments< CheckOption, @@ -44,7 +44,8 @@ public static int Main(string[] args) BaselineOption, RebaseOption, //ArchiveOption, - PlatformsOption + PlatformsOption, + ConfigOption >(args).MapResult( (CheckOption opts) => Dispatch(commandLineService.RunCheckOption, opts, traceService), (InitOption opts) => Dispatch(commandLineService.RunInitOption, opts, traceService), @@ -58,8 +59,8 @@ public static int Main(string[] args) (RebaseOption opts) => Dispatch(commandLineService.RunRebaseOption, opts, traceService), //(ArchiveOption opts) => Dispatch(commandLineService.RunArchiveOption, opts, traceService), (PlatformsOption opts) => Dispatch(commandLineService.RunPlatformsOption, opts, traceService), - - errs => 1); + (ConfigOption opts) => Dispatch(commandLineService.RunConfigOption, opts, traceService), + errs => 1 ); return resultCode; } @@ -78,7 +79,7 @@ private static int Dispatch(Func command, T opts, ITraceService trace Console.WriteLine($"Visit https://yuniql.io for documentation and working samples{Environment.NewLine}"); Console.ResetColor(); - traceService.IsDebugEnabled = opts.IsDebug; + traceService.IsDebugEnabled = opts.IsDebug?? false; traceService.IsTraceSensitiveData = opts.IsTraceSensitiveData; traceService.IsTraceToFile = opts.IsTraceToFile; traceService.TraceToDirectory = opts.TraceToDirectory; diff --git a/yuniql-cli/Properties/launchSettings.json b/yuniql-cli/Properties/launchSettings.json deleted file mode 100644 index 8afc538c..00000000 --- a/yuniql-cli/Properties/launchSettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "profiles": { - "Yuniql.CLI": { - "commandName": "Project", - "commandLineArgs": "destroy --force --debug -c \"Server=localhost;Port=3306;Database=helloyuniql;Uid=root;Pwd=P@ssw0rd!;\"" - } - } -} \ No newline at end of file diff --git a/yuniql-core/IPrinter.cs b/yuniql-core/IPrinter.cs new file mode 100644 index 00000000..9a07319f --- /dev/null +++ b/yuniql-core/IPrinter.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Yuniql.Core +{ + public interface IPrinter + { + void Print(); + void AddRow(params object[] row); + + } +} diff --git a/yuniql-core/Source.cs b/yuniql-core/Source.cs new file mode 100644 index 00000000..78c00c14 --- /dev/null +++ b/yuniql-core/Source.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Yuniql.Core +{ + /// + /// Information source for yuniql configuration values + /// + public enum Source + { + /// + /// Default value assigned to configuration parameter + /// + DEFAULT, + /// + /// Environment variable is the source of the configuration parameter + /// + ENVIRONMENT_VARIABLE, + /// + /// command line is the source of the configuration parameter + /// + CMD_LINE_OPTIONS + } +} diff --git a/yuniql-cli/TablePrinter.cs b/yuniql-core/TablePrinter.cs similarity index 90% rename from yuniql-cli/TablePrinter.cs rename to yuniql-core/TablePrinter.cs index 25936104..d86f33ba 100644 --- a/yuniql-cli/TablePrinter.cs +++ b/yuniql-core/TablePrinter.cs @@ -2,12 +2,9 @@ using System.Collections.Generic; using System.Linq; -namespace Yuniql.CLI +namespace Yuniql.Core { - //TODO: Move this into Yuniql.Core as TablePrintService - //thanks https://stackoverflow.com/users/1547699/sumudu - //https://stackoverflow.com/a/54943087/3449591 - public class TablePrinter + public class TablePrinter : IPrinter { private readonly string[] titles; private readonly List lengths; @@ -71,5 +68,6 @@ public void Print() Console.WriteLine("+"); Console.WriteLine(); } + } }