static int Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue); var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue); bool certExists() { return(File.Exists(cert.Value())); } bool coreExists() { return(File.Exists(coreDll.Value())); } bool verifyTopOptions() { return(!string.IsNullOrWhiteSpace(cert.Value()) && !string.IsNullOrWhiteSpace(coreDll.Value()) && certExists() && coreExists()); } app.Command("user", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var userIdArg = config.Argument("User ID", "your user id"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.ShowHelp("user"); return(1); } if (string.IsNullOrWhiteSpace(userIdArg.Value) || !Guid.TryParse(userIdArg.Value, out Guid userId)) { config.Error.WriteLine($"User ID not provided"); config.ShowHelp("user"); return(1); } GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, userId, key.Value); return(0); }); }); app.Command("org", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var installId = config.Argument("InstallId", "your installation id (GUID)"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value) || string.IsNullOrWhiteSpace(installId.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.ShowHelp("org"); return(1); } if (!Guid.TryParse(installId.Value, out Guid installationId)) { config.Error.WriteLine("Unable to parse your installation id as a GUID"); config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}"); config.ShowHelp("org"); return(1); } GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, key.Value); return(0); }); }); app.OnExecute(() => { app.ShowHelp(); return(10); }); app.HelpOption("-? | -h | --help"); try { return(app.Execute(args)); } catch (Exception e) { Console.Error.WriteLine("Oops: {0}", e); return(100); } }
static int Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue); var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue); bool certExists() { return(File.Exists(cert.Value())); } bool coreExists() { return(File.Exists(coreDll.Value())); } bool verifyTopOptions() { return(!string.IsNullOrWhiteSpace(cert.Value()) && !string.IsNullOrWhiteSpace(coreDll.Value()) && certExists() && coreExists()); } app.Command("interactive", config => { string buff = "", licensetype = "", name = "", email = "", businessname = ""; bool valid_guid = false, valid_installid = false; Guid guid = new Guid(), installid = new Guid(); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } WriteLine("Interactive license mode..."); while (licensetype == "") { WriteLine("What would you like to generate, a [u]ser license or an [o]rg license?"); buff = Console.ReadLine(); if (buff == "u") { licensetype = "user"; WriteLineOver("Okay, we will generate a user license."); while (valid_guid == false) { WriteLine("Please provide the user's guid — refer to the Readme for details on how to retrieve this. [GUID]:"); buff = Console.ReadLine(); if (Guid.TryParse(buff, out guid)) { valid_guid = true; } else { WriteLineOver("The user-guid provided does not appear to be valid."); } } } else if (buff == "o") { licensetype = "org"; WriteLineOver("Okay, we will generate an organization license."); while (valid_installid == false) { WriteLine("Please provide your Bitwarden Install-ID — refer to the Readme for details on how to retrieve this. [Install-ID]:"); buff = Console.ReadLine(); if (Guid.TryParse(buff, out installid)) { valid_installid = true; } else { WriteLineOver("The install-id provided does not appear to be valid."); } } while (businessname == "") { WriteLineOver("Please enter a business name, default is BitBetter. [Business Name]:"); buff = Console.ReadLine(); if (buff == "") { businessname = "BitBetter"; } else if (checkBusinessName(buff)) { businessname = buff; } } } else { WriteLineOver("Unrecognized option \'" + buff + "\'. "); } } while (name == "") { WriteLineOver("Please provide the username this license will be registered to. [username]:"); buff = Console.ReadLine(); if (checkUsername(buff)) { name = buff; } } while (email == "") { WriteLineOver("Please provide the email address for the user " + name + ". [email]"); buff = Console.ReadLine(); if (checkEmail(buff)) { email = buff; } } if (licensetype == "user") { WriteLineOver("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", User-GUID: \"" + guid + "\"? Y/n"); buff = Console.ReadLine(); if (buff == "" || buff == "y" || buff == "Y") { GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, guid, null); } else { WriteLineOver("Exiting..."); return(0); } } else if (licensetype == "org") { WriteLineOver("Confirm creation of \"organization\" license for business name: \"" + businessname + "\", username: \"" + name + "\", email: \"" + email + "\", Install-ID: \"" + installid + "\"? Y/n"); buff = Console.ReadLine(); if (buff == "" || buff == "y" || buff == "Y") { GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, installid, businessname, null); } else { WriteLineOver("Exiting..."); return(0); } } return(0); }); }); app.Command("user", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var userIdArg = config.Argument("User ID", "your user id"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.ShowHelp("user"); return(1); } if (string.IsNullOrWhiteSpace(userIdArg.Value) || !Guid.TryParse(userIdArg.Value, out Guid userId)) { config.Error.WriteLine($"User ID not provided"); config.ShowHelp("user"); return(1); } GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, userId, key.Value); return(0); }); }); app.Command("org", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var installId = config.Argument("InstallId", "your installation id (GUID)"); var businessName = config.Argument("BusinessName", "name For the organization (optional)"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value) || string.IsNullOrWhiteSpace(installId.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.ShowHelp("org"); return(1); } if (!Guid.TryParse(installId.Value, out Guid installationId)) { config.Error.WriteLine("Unable to parse your installation id as a GUID"); config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}"); config.ShowHelp("org"); return(1); } GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, businessName.Value, key.Value); return(0); }); }); app.OnExecute(() => { app.ShowHelp(); return(10); }); app.HelpOption("-? | -h | --help"); try { return(app.Execute(args)); } catch (Exception e) { Console.Error.WriteLine("Oops: {0}", e); return(100); } }
private static async Task MainAsync(string[] args) { var result = 0; Console.WriteLine("Message Sender Data Generator: usage: dotnet DataGenNetCore.dll sendmessages -t <default|telco> <slow|fast|faster|insane>"); // docker background cannot handle the ESC //Console.WriteLine("Press ESC to stop"); var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(throwOnUnexpectedArg: false); app.Name = "Event Hub Data Generator"; app.HelpOption("-? | -h | --help"); var sendmessages = app.Command("sendmessages", s => { s.OnExecute(() => { s.ShowHelp(); //show help for sendmessages return(1); //return error since we didn't do anything }); s.HelpOption("-? | -h | --help"); //show help on --help }); var optionMessageType = sendmessages.Option("-t | --message-type", "-t --message-type simple|telco", CommandOptionType.SingleValue); var optionVerbose = sendmessages.Option("-v | --verbose", "-v --verbose", CommandOptionType.NoValue); sendmessages.Command("help", help => { help.Description = "get help!"; help.OnExecute(() => { sendmessages.ShowHelp("sendmessages"); return(1); }); }); sendmessages.Command("slow", slow => { slow.Description = "run message sending with 1 second thread sleep plus time of day delay (slow)"; slow.HelpOption("-? | -h | --help"); slow.OnExecute(async() => { Console.WriteLine("using slow mode (1 message every ~five seconds, plus time of day delay, using thread.sleep)"); await sendMessages("slow", optionMessageType.Value(), optionVerbose.HasValue()); return(0); }); }); sendmessages.Command("fast", fast => { fast.Description = "run message sending with time of day delay only (fast)"; fast.HelpOption("-? | -h | --help"); fast.OnExecute(async() => { Console.WriteLine("using fast mode (single thread, time of day delay)"); await sendMessages("fast", optionMessageType.Value(), optionVerbose.HasValue()); return(0); }); }); sendmessages.Command("faster", faster => { faster.Description = "run parallel for loop message sending (fastest)"; faster.HelpOption("-? | -h | --help"); faster.OnExecute(async() => { Console.WriteLine("using faster mode (10 threads via parallel.for, time of day delay)"); await sendMessages("faster", optionMessageType.Value(), optionVerbose.HasValue()); return(0); }); }); sendmessages.Command("insane", insane => { insane.Description = "run parallel for loop message sending (insane)"; insane.HelpOption("-? | -h | --help"); insane.OnExecute(async() => { Console.WriteLine("using insane mode (20 threads via parallel.for, no delay)"); await sendMessages("insane", optionMessageType.Value(), optionVerbose.HasValue()); return(0); }); }); try { var connectionStringBuilder = new EventHubsConnectionStringBuilder(secrets.EventHubConnectionString()) { EntityPath = secrets.EventHubPath() }; hubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); app.Execute(args); } catch (Exception e) { app.HelpOption("-? | -h | --help"); Console.WriteLine("Error occurred: {0}", e.Message); } Console.WriteLine("press any key to exit"); Console.ReadKey(); Environment.Exit(result); }