From 067ed86cf4eddf2279394da51ff4f7140c7b9e05 Mon Sep 17 00:00:00 2001 From: Brett Williams Date: Sun, 29 Dec 2019 11:23:20 -0400 Subject: [PATCH 1/2] Adds parsing of port for MySQL/MariaDB databases --- src/Tgstation.Server.Host/Core/SetupWizard.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Tgstation.Server.Host/Core/SetupWizard.cs b/src/Tgstation.Server.Host/Core/SetupWizard.cs index 44fc7eb3ff..317696056b 100644 --- a/src/Tgstation.Server.Host/Core/SetupWizard.cs +++ b/src/Tgstation.Server.Host/Core/SetupWizard.cs @@ -10,6 +10,7 @@ using System.Data.SqlClient; using System.Globalization; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Host.Configuration; @@ -166,8 +167,18 @@ namespace Tgstation.Server.Host.Core await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); await console.WriteAsync("Enter the server's address and port (blank for local): ", false, cancellationToken).ConfigureAwait(false); var serverAddress = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); + var serverPort = 3306U; if (String.IsNullOrWhiteSpace(serverAddress)) serverAddress = null; + else + { + var m = Regex.Match(serverAddress, @"^(?.+):(?[0-9]+)$"); + if (m.Success) + { + serverAddress = m.Groups["server"].Value; + serverPort = uint.Parse(m.Groups["port"].Value, CultureInfo.InvariantCulture); + } + } await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); await console.WriteAsync("Enter the database name (Can be from previous installation. Otherwise, should not exist): ", false, cancellationToken).ConfigureAwait(false); @@ -239,6 +250,7 @@ namespace Tgstation.Server.Host.Core var csb = new MySqlConnectionStringBuilder { Server = serverAddress ?? "127.0.0.1", + Port = serverPort, UserID = username, Password = password }; From 121e8e83309251109025162c1a953768c8c1e22b Mon Sep 17 00:00:00 2001 From: Brett Williams Date: Sun, 5 Jan 2020 14:33:54 -0400 Subject: [PATCH 2/2] Looping for server input, improved parsing of port --- src/Tgstation.Server.Host/Core/SetupWizard.cs | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Tgstation.Server.Host/Core/SetupWizard.cs b/src/Tgstation.Server.Host/Core/SetupWizard.cs index 317696056b..f38ea0b844 100644 --- a/src/Tgstation.Server.Host/Core/SetupWizard.cs +++ b/src/Tgstation.Server.Host/Core/SetupWizard.cs @@ -164,21 +164,39 @@ namespace Tgstation.Server.Host.Core } while (true); - await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); - await console.WriteAsync("Enter the server's address and port (blank for local): ", false, cancellationToken).ConfigureAwait(false); - var serverAddress = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); - var serverPort = 3306U; - if (String.IsNullOrWhiteSpace(serverAddress)) - serverAddress = null; - else + string serverAddress; + uint? mySQLServerPort = null; + do { - var m = Regex.Match(serverAddress, @"^(?.+):(?[0-9]+)$"); - if (m.Success) + await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); + await console.WriteAsync("Enter the server's address and port [: or ] (blank for local): ", false, cancellationToken).ConfigureAwait(false); + serverAddress = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); + if (String.IsNullOrWhiteSpace(serverAddress)) { - serverAddress = m.Groups["server"].Value; - serverPort = uint.Parse(m.Groups["port"].Value, CultureInfo.InvariantCulture); + serverAddress = null; + break; } + else if (databaseConfiguration.DatabaseType != DatabaseType.SqlServer) + { + var m = Regex.Match(serverAddress, @"^(?.+):(?.+)$"); + if (m.Success) + { + serverAddress = m.Groups["server"].Value; + if (uint.TryParse(m.Groups["port"].Value, out uint port)) + { + mySQLServerPort = port; + break; + } + else + { + await console.WriteAsync($@"Failed to parse port ""{m.Groups["port"].Value}"", please try again.", true, cancellationToken).ConfigureAwait(false); + } + } + else break; + } + else break; } + while (true); await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); await console.WriteAsync("Enter the database name (Can be from previous installation. Otherwise, should not exist): ", false, cancellationToken).ConfigureAwait(false); @@ -250,11 +268,13 @@ namespace Tgstation.Server.Host.Core var csb = new MySqlConnectionStringBuilder { Server = serverAddress ?? "127.0.0.1", - Port = serverPort, UserID = username, Password = password }; + if (mySQLServerPort.HasValue) + csb.Port = mySQLServerPort.Value; + CreateTestConnection(csb.ConnectionString); csb.Database = databaseName; databaseConfiguration.ConnectionString = csb.ConnectionString;