Reduce complexity

This commit is contained in:
Michiel Hazelhof 2023-01-04 01:15:03 +01:00
parent 8a69076dd1
commit 65bda70c34
No known key found for this signature in database
GPG Key ID: EECB9B96355B5EBF

View File

@ -52,72 +52,81 @@ internal class Program
return 1; return 1;
} }
WriteLine("Interactive license mode..."); Console.WriteLine("Interactive license mode...");
while (licensetype == "") while (licensetype == "")
{ {
WriteLine("What would you like to generate, a [u]ser license or an [o]rg license?"); Console.WriteLine("What would you like to generate, a [u]ser license or an [o]rg license: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if(buff == "u") if(buff == "u")
{ {
licensetype = "user"; licensetype = "user";
WriteLineOver("Okay, we will generate a user license."); Console.WriteLine("Okay, we will generate a user license.");
while (validGuid == false) while (validGuid == false)
{ {
WriteLine("Please provide the user's guid — refer to the Readme for details on how to retrieve this. [GUID]:"); Console.WriteLine("Please provide the user's guid — refer to the Readme for details on how to retrieve this. [GUID]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if (Guid.TryParse(buff, out guid))validGuid = true; if (Guid.TryParse(buff, out guid))validGuid = true;
else WriteLineOver("The user-guid provided does not appear to be valid."); else Console.WriteLine("The user-guid provided does not appear to be valid!");
} }
} }
else if (buff == "o") else if (buff == "o")
{ {
licensetype = "org"; licensetype = "org";
WriteLineOver("Okay, we will generate an organization license."); Console.WriteLine("Okay, we will generate an organization license.");
while (validInstallid == false) while (validInstallid == false)
{ {
WriteLine("Please provide your Bitwarden Install-ID — refer to the Readme for details on how to retrieve this. [Install-ID]:"); Console.WriteLine("Please provide your Bitwarden Install-ID — refer to the Readme for details on how to retrieve this. [Install-ID]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if (Guid.TryParse(buff, out installid)) validInstallid = true; if (Guid.TryParse(buff, out installid)) validInstallid = true;
else WriteLineOver("The install-id provided does not appear to be valid."); else Console.WriteLine("The install-id provided does not appear to be valid.");
} }
while (businessname == "") while (businessname == "")
{ {
WriteLineOver("Please enter a business name, default is BitBetter. [Business Name]:"); Console.WriteLine("Please enter a business name, default is BitBetter. [Business Name]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if (buff == "") businessname = "BitBetter"; if (buff == "")
else if (CheckBusinessName(buff)) businessname = buff; {
businessname = "BitBetter";
}
else if (CheckBusinessName(buff))
{
businessname = buff;
}
} }
} }
else else
{ {
WriteLineOver("Unrecognized option \'" + buff + "\'. "); Console.WriteLine("Unrecognized option \'" + buff + "\'.");
} }
} }
while (name == "") while (name == "")
{ {
WriteLineOver("Please provide the username this license will be registered to. [username]:"); Console.WriteLine("Please provide the username this license will be registered to. [username]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( CheckUsername(buff) ) name = buff; if ( CheckUsername(buff) ) name = buff;
} }
while (email == "") while (email == "")
{ {
WriteLineOver("Please provide the email address for the user " + name + ". [email]"); Console.WriteLine("Please provide the email address for the user " + name + ". [email]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( CheckEmail(buff) ) email = buff; if (CheckEmail(buff))
{
email = buff;
}
} }
while (storage == 0) while (storage == 0)
{ {
WriteLineOver("Extra storage space for the user " + name + ". (max.: " + Int16.MaxValue + "). Defaults to maximum value. [storage]"); Console.WriteLine("Extra storage space for the user " + name + ". (max.: " + Int16.MaxValue + "). Defaults to maximum value. [storage]");
buff = Console.ReadLine(); buff = Console.ReadLine();
if (String.IsNullOrWhiteSpace(buff)) if (String.IsNullOrWhiteSpace(buff))
{ {
@ -125,35 +134,38 @@ internal class Program
} }
else else
{ {
if (CheckStorage(buff)) storage = Int16.Parse(buff); if (CheckStorage(buff))
{
storage = Int16.Parse(buff);
}
} }
} }
if (licensetype == "user") if (licensetype == "user")
{ {
WriteLineOver("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", User-GUID: \"" + guid + "\"? Y/n"); Console.WriteLine("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", User-GUID: \"" + guid + "\"? Y/n");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" ) if ( buff is "" or "y" or "Y" )
{ {
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, guid, null); GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, guid, null);
} }
else else
{ {
WriteLineOver("Exiting..."); Console.WriteLine("Exiting...");
return 0; return 0;
} }
} }
else if (licensetype == "org") else if (licensetype == "org")
{ {
WriteLineOver("Confirm creation of \"organization\" license for business name: \"" + businessname + "\", username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", Install-ID: \"" + installid + "\"? Y/n"); Console.WriteLine("Confirm creation of \"organization\" license for business name: \"" + businessname + "\", username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", Install-ID: \"" + installid + "\"? Y/n");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( buff == "" || buff == "y" || buff == "Y" ) if ( buff is "" or "y" or "Y" )
{ {
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, installid, businessname, null); GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, installid, businessname, null);
} }
else else
{ {
WriteLineOver("Exiting..."); Console.WriteLine("Exiting...");
return 0; return 0;
} }
} }
@ -186,7 +198,8 @@ internal class Program
config.ShowHelp(); config.ShowHelp();
return 1; return 1;
} }
else if (String.IsNullOrWhiteSpace(name.Value) || String.IsNullOrWhiteSpace(email.Value))
if (String.IsNullOrWhiteSpace(name.Value) || String.IsNullOrWhiteSpace(email.Value))
{ {
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'");
config.ShowHelp("user"); config.ShowHelp("user");
@ -204,7 +217,7 @@ internal class Program
if (!String.IsNullOrWhiteSpace(storage.Value)) if (!String.IsNullOrWhiteSpace(storage.Value))
{ {
Double parsedStorage = Double.Parse(storage.Value); Double parsedStorage = Double.Parse(storage.Value);
if (parsedStorage > Int16.MaxValue || parsedStorage < 0) if (parsedStorage is > Int16.MaxValue or < 0)
{ {
config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "]"); config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "]");
config.ShowHelp("org"); config.ShowHelp("org");
@ -243,7 +256,8 @@ internal class Program
config.ShowHelp(); config.ShowHelp();
return 1; return 1;
} }
else if (String.IsNullOrWhiteSpace(name.Value) ||
if (String.IsNullOrWhiteSpace(name.Value) ||
String.IsNullOrWhiteSpace(email.Value) || String.IsNullOrWhiteSpace(email.Value) ||
String.IsNullOrWhiteSpace(installId.Value)) String.IsNullOrWhiteSpace(installId.Value))
{ {
@ -264,7 +278,7 @@ internal class Program
if (!String.IsNullOrWhiteSpace(storage.Value)) if (!String.IsNullOrWhiteSpace(storage.Value))
{ {
Double parsedStorage = Double.Parse(storage.Value); Double parsedStorage = Double.Parse(storage.Value);
if (parsedStorage > Int16.MaxValue || parsedStorage < 0) if (parsedStorage is > Int16.MaxValue or < 0)
{ {
config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "]"); config.Error.WriteLine("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "]");
config.ShowHelp("org"); config.ShowHelp("org");
@ -301,61 +315,47 @@ internal class Program
// checkUsername Checks that the username is a valid username // checkUsername Checks that the username is a valid username
private static Boolean CheckUsername(String s) private static Boolean CheckUsername(String s)
{ {
if ( String.IsNullOrWhiteSpace(s) ) { // TODO: Actually validate
WriteLineOver("The username provided doesn't appear to be valid.\n"); if (!String.IsNullOrWhiteSpace(s)) return true;
Console.WriteLine("The username provided doesn't appear to be valid!");
return false; return false;
} }
return true; // TODO: Actually validate
}
// checkBusinessName Checks that the Business Name is a valid username // checkBusinessName Checks that the Business Name is a valid username
private static Boolean CheckBusinessName(String s) private static Boolean CheckBusinessName(String s)
{ {
if ( String.IsNullOrWhiteSpace(s) ) { // TODO: Actually validate
WriteLineOver("The Business Name provided doesn't appear to be valid.\n"); if (!String.IsNullOrWhiteSpace(s)) return true;
Console.WriteLine("The Business Name provided doesn't appear to be valid!");
return false; return false;
} }
return true; // TODO: Actually validate
}
// checkEmail Checks that the email address is a valid email address // checkEmail Checks that the email address is a valid email address
private static Boolean CheckEmail(String s) private static Boolean CheckEmail(String s)
{ {
if ( String.IsNullOrWhiteSpace(s) ) { // TODO: Actually validate
WriteLineOver("The email provided doesn't appear to be valid.\n"); if (!String.IsNullOrWhiteSpace(s)) return true;
Console.WriteLine("The email provided doesn't appear to be valid!");
return false; return false;
} }
return true; // TODO: Actually validate
}
// checkStorage Checks that the storage is in a valid range // checkStorage Checks that the storage is in a valid range
private static Boolean CheckStorage(String s) private static Boolean CheckStorage(String s)
{ {
if (String.IsNullOrWhiteSpace(s)) if (String.IsNullOrWhiteSpace(s))
{ {
WriteLineOver("The storage provided doesn't appear to be valid.\n"); Console.WriteLine("The storage provided doesn't appear to be valid!");
return false; return false;
} }
if (Double.Parse(s) > Int16.MaxValue || Double.Parse(s) < 0)
{ if (!(Double.Parse(s) > Int16.MaxValue) && !(Double.Parse(s) < 0)) return true;
WriteLineOver("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "].\n");
Console.WriteLine("The storage value provided is outside the accepted range of [0-" + Int16.MaxValue + "]!");
return false; return false;
} }
return true;
}
// WriteLineOver Writes a new line to console over last line.
private static void WriteLineOver(String s)
{
Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine(s);
}
// WriteLine This wrapper is just here so that console writes all look similar.
private static void WriteLine(String s)
{
Console.WriteLine(s);
}
private static void GenerateUserLicense(X509Certificate2 cert, String corePath, String userName, String email, Int16 storage, Guid userId, String key) private static void GenerateUserLicense(X509Certificate2 cert, String corePath, String userName, String email, Int16 storage, Guid userId, String key)
{ {