Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Done #32
-> Done #36
-> Done #40
  • Loading branch information
NaysKutzu committed Aug 22, 2023
1 parent 4ff8787 commit 2f2a948
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 367 deletions.
10 changes: 5 additions & 5 deletions cli/MythicalDash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Costura.Fody" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Costura.Fody" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>compile; runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MySqlConnector" Version="2.2.7" />
<PackageReference Include="YamlDotNet" Version="13.2.0" />
</ItemGroup>

</Project>
28 changes: 27 additions & 1 deletion cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ __ __ _ _ _ _ _____ _
public static Debug dbg = new Debug();
public static Encryption encryption = new Encryption();
public static IConsole iconsole = new IConsole();
public static Database db = new Database();

public static void Main(string[] args)
{
Console.Clear();
Expand Down Expand Up @@ -151,7 +153,7 @@ public static void Main(string[] args)
logger.Log(LogType.Error, "Failed to delete config: " + ex.Message);
Environment.Exit(0x0);
}
}
}
else if (args.Contains("-disable-debug"))
{
try
Expand Down Expand Up @@ -196,6 +198,30 @@ public static void Main(string[] args)
logger.Log(LogType.Info, "You are running version: " + version);
Environment.Exit(0x0);
}
else if (args.Contains("-config-database")) {
db.Configurator();
Environment.Exit(0x0);
}
else if (args.Contains("-help")) {
Console.Clear();
Console.WriteLine("--------------------------------------------MythicalDash CLI-------------------------------------------------");
Console.WriteLine("| |");
Console.WriteLine("| -help | Opens a help menu with the available commands. |");
Console.WriteLine("| -generate-config | Generate a new config file for MythicalDash. |");
Console.WriteLine("| -delete-config | Delete the config file for MythicalDash. |");
Console.WriteLine("| -key-generate | Generate a new encryption key for MythicalDash. |");
Console.WriteLine("| -enable-debug | Enables the debug mode to display error messages for MythicalDash. |");
Console.WriteLine("| -disable-console | Disables the browser's inspect element or console from being used on MythicalDash. |");
Console.WriteLine("| -enable-console | Enables the browser's inspect element or console on MythicalDash. |");
Console.WriteLine("| -disable-debug | Disables the debug mode to hide error messages for MythicalDash. |");
Console.WriteLine("| -enable-silent-debug | Hides the debug mode online status messages from being disabled. |");
Console.WriteLine("| -disable-silent-debug | Shows the debug mode online status messages from being enabled. |");
Console.WriteLine("| -config-database | Add the database connection to your config file. |");
Console.WriteLine("| -version | See the version / build version of the CLI. |");
Console.WriteLine("| |");
Console.WriteLine("-------------------------------------------------------------------------------------------------------------");
Environment.Exit(0x0);
}
else if (args.Length > 0)
{
logger.Log(LogType.Error, "This is an invalid startup argument. Please use '-help' to get more information");
Expand Down
151 changes: 151 additions & 0 deletions cli/scripts/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using MythicalDash;
using MySqlConnector;
using YamlDotNet.RepresentationModel;

namespace MythicalDash
{
public class Database
{
FileManager fm = new FileManager();
private static string ReadPasswordInput()
{
string password = "";
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password = password.Remove(password.Length - 1);
Console.Write("\b \b");
}
}
else
{
password += key.KeyChar;
Console.Write("*");
}
}
return password;
}
public void Configurator()
{
#pragma warning disable
Program.logger.Log(LogType.Info, "Hi, please fill in your database configuration for MythicalDash.");
string defaultHost = "localhost";
string defaultPort = "3306";
string defaultUsername = "mythicaldash";
string deafultdbName = "mythicaldash";

Console.Write("Host [");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"{defaultHost}");
Console.ResetColor();
Console.Write("]: ");

string host = Console.ReadLine();
if (string.IsNullOrWhiteSpace(host))
{
host = defaultHost;
}

Console.Write("Port [");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"{defaultPort}");
Console.ResetColor();
Console.Write("]: ");

string port = Console.ReadLine();
if (string.IsNullOrWhiteSpace(port))
{
port = defaultPort;
}

Console.Write("Username [");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"{defaultUsername}");
Console.ResetColor();
Console.Write("]: ");

string username = Console.ReadLine();
if (string.IsNullOrWhiteSpace(username))
{
username = defaultUsername;
}

Console.Write("Password: ");
string password = ReadPasswordInput();

Console.Write("Database Name [");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"{deafultdbName}");
Console.ResetColor();
Console.Write("]: ");

string dbName = Console.ReadLine();
if (string.IsNullOrWhiteSpace(dbName))
{
dbName = deafultdbName;
}
#pragma warning restore
if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(dbName))
{
Program.logger.Log(LogType.Error, "Invalid input. Please provide all the required values.");
Environment.Exit(0x0);
}
try
{
using var connection = new MySqlConnection($"Server={host};Port={port};User ID={username};Password={password};Database={dbName}");
connection.Open();
Program.logger.Log(LogType.Info, "Connected to MySQL, saving database configuration to config.");
connection.Close();
UpdateConfig(host, port, username, password, dbName);
Program.logger.Log(LogType.Info, "Done we saved your MySQL connection to your config file");
Environment.Exit(0x0);
}
catch (Exception ex)
{
Program.logger.Log(LogType.Error, $"Failed to connect to MySQL: {ex.Message}");
Environment.Exit(0x0);
}
}
public void UpdateConfig(string host, string port, string username, string password, string dbname)
{
if (fm.Exist() == true)
{
string filePath = "config.yml";
var yaml = new YamlStream();

using (var reader = new StreamReader(filePath))
{
yaml.Load(reader);
}

var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
var appSection = (YamlMappingNode)mapping["database"];

appSection.Children[new YamlScalarNode("host")] = new YamlScalarNode(host);
appSection.Children[new YamlScalarNode("port")] = new YamlScalarNode(port);
appSection.Children[new YamlScalarNode("username")] = new YamlScalarNode(username);
appSection.Children[new YamlScalarNode("password")] = new YamlScalarNode(password);
appSection.Children[new YamlScalarNode("database")] = new YamlScalarNode(dbname);

using (var writer = new StreamWriter(filePath))
{
yaml.Save(writer, false);
}
Program.logger.Log(LogType.Info, "We updated the settings");
}
else
{
Program.logger.Log(LogType.Error, "It looks like the config file does not exist!");
}
}
}
}
Loading

0 comments on commit 2f2a948

Please sign in to comment.