Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add feature print configuration issue 172 #203

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions yuniql-cli/BaseConfigOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CommandLine;

namespace Yuniql.CLI
{
public class BaseConfigOption : BasePlatformOption
{
//yuniql <command> -o json
[Option('o', "output", Required = false, HelpText = "The choosen Output format")]
public string Output { get; set; }

//yuniql <command> -a true | --auto-create-db true
[Option('a', "auto-create-db", Required = false, HelpText = "Create database automatically.")]
public bool? IsAutoCreateDatabase { get; set; }
}
}


2 changes: 1 addition & 1 deletion yuniql-cli/BaseOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class BaseOption

//yuniql <command> -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; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why you think this has to be changed. I'm afraid it may introduce bugs not directly related to feature we're implementing. :)

Copy link
Author

@zwdOr20 zwdOr20 Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raison why I made it nullable is I want to differentiate between two states :

  1. IsDebug is false && the value wasn't specified by the user (default value for boolean is False doc)
  2. When the user specify --debug false

In both cases IsDebug set to false but the source of this false is different


//yuniql <command> --trace-sensitive-data
[Option("trace-sensitive-data", Required = false, HelpText = "Include sensitive data like connection string in the log messages.", Default = false)]
Expand Down
4 changes: 2 additions & 2 deletions yuniql-cli/BasePlatformOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Yuniql.CLI
{
public class BasePlatformOption : BaseOption
{
//yuniql <command> -d | --debug
{
//yuniql <command> -p | --platform
rdagumampan marked this conversation as resolved.
Show resolved Hide resolved
[Option(longName: "platform", Required = false, HelpText = "Target database platform. Default is sqlserver.")]
public string Platform { get; set; }

Expand Down
69 changes: 62 additions & 7 deletions yuniql-cli/CommandLineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private Configuration SetupRunConfiguration(BaseRunPlatformOption opts, bool isV
var tokens = opts.Tokens.Select(t => new KeyValuePair<string, string>(t.Split("=")[0], t.Split("=")[1])).ToList();

configuration.Workspace = opts.Workspace;
configuration.IsDebug = opts.IsDebug;
configuration.IsDebug = opts.IsDebug?? false;
rdagumampan marked this conversation as resolved.
Show resolved Hide resolved

configuration.Platform = platform;
configuration.ConnectionString = opts.ConnectionString;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -368,10 +368,65 @@ public int RunArchiveOption(ArchiveOption opts)
return OnException(ex, "Failed to execute archive function", opts.IsDebug);
}
}
public int RunConfigOption(ConfigOption opts)
rdagumampan marked this conversation as resolved.
Show resolved Hide resolved
{
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}" +
Expand Down
9 changes: 9 additions & 0 deletions yuniql-cli/ConfigOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CommandLine;

namespace Yuniql.CLI
{
[Verb("config", HelpText = "Print the environment setup in the command line")]
public class ConfigOption : BaseConfigOption
{
}
}
43 changes: 43 additions & 0 deletions yuniql-cli/JsonPrinter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Yuniql.Core;

namespace Yuniql.CLI
{
/// <summary>
/// This class is used to print configuration variable as json text
/// </summary>
public class JsonPrinter : IPrinter
{
private readonly string[] titles = new string[] { "property","value","source"};
private readonly List<string[]> rows = new List<string[]>();

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()
rdagumampan marked this conversation as resolved.
Show resolved Hide resolved
{
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());
}
}
}
11 changes: 6 additions & 5 deletions yuniql-cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommandLine;
using System;
using System.Collections;
using System.Reflection;
using Yuniql.Core;
using Yuniql.Extensibility;
Expand Down Expand Up @@ -30,7 +31,6 @@ public static int Main(string[] args)
environmentService,
traceService,
configurationService);

var resultCode = Parser.Default
.ParseArguments<
CheckOption,
Expand All @@ -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),
Expand All @@ -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;
}
Expand All @@ -78,7 +79,7 @@ private static int Dispatch<T>(Func<T, int> 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;
Expand Down
8 changes: 0 additions & 8 deletions yuniql-cli/Properties/launchSettings.json

This file was deleted.

13 changes: 13 additions & 0 deletions yuniql-core/IPrinter.cs
Original file line number Diff line number Diff line change
@@ -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);

}
}
25 changes: 25 additions & 0 deletions yuniql-core/Source.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Yuniql.Core
{
/// <summary>
/// Information source for yuniql configuration values
/// </summary>
public enum Source
{
/// <summary>
/// Default value assigned to configuration parameter
/// </summary>
DEFAULT,
/// <summary>
/// Environment variable is the source of the configuration parameter
/// </summary>
ENVIRONMENT_VARIABLE,
/// <summary>
/// command line is the source of the configuration parameter
/// </summary>
CMD_LINE_OPTIONS
}
}
8 changes: 3 additions & 5 deletions yuniql-cli/TablePrinter.cs → yuniql-core/TablePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> lengths;
Expand Down Expand Up @@ -71,5 +68,6 @@ public void Print()
Console.WriteLine("+");
Console.WriteLine();
}

}
}