public static bool DownloadAccessFile(ConsoleX consoleX)
		{
			bool fileDownloaded = false;
			do
			{
				var url = consoleX.WriteClipboardQuery("CORRECT URL");
				url = url.Trim(); // Remove any whitespace that may have been copied by mistake.
				
				if(!AccessFileDownloader.IsUrlCorrect(url))
				{
					consoleX.WriteLine("Sorry, incorrect URL. Please try again.");
				}
				else
				{
					try
					{
						consoleX.WriteLine("Thanks! Downloading file now. Please wait...");
						AccessFileDownloader.DownloadFile(url);
						consoleX.WriteLine("OK, Done.");
						fileDownloaded = true;
					}
					catch(Exception ex)
					{
						ExceptionHelper.HandleException(ex, consoleX);
					}
				}
			}
			while(!fileDownloaded);
			
			return fileDownloaded;
		}
		public static void TestDatabaseState(ConsoleX consoleX, bool showTestFeedback = false)
		{
			if(showTestFeedback)
				consoleX.WriteLine("Ok, let's check the database for synchronisation issues...");
			
			if(!DatabaseState.IsDataSynchronised())
			{
				
				consoleX.WriteWarning("Warning: Database records have not been synchronised!", false);
				consoleX.WriteWarning("Attempting to synchronise now...");
				if(DatabaseState.TrySync())
				{
					consoleX.WriteWarning("Success!", true);
				}
				else
				{
					consoleX.WriteWarning("Failure! Could not synchronise at this time.", false);
				}
				consoleX.WriteWarning("If this message persists, please contact IT Support.");
			}
			else if(showTestFeedback)
			{
				consoleX.WriteLine("Great, no synchronisation issues found!");
			}
		}
Example #3
0
        /// <summary>
        /// Destroys an existing Vultr script.
        /// </summary>
        /// <param name="script">The configuration to reference to destroy the
        /// script.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Destroy(Script script, bool dryrun = false)
        {
            Console.WriteLine("Destroying script");
            ConsoleX.WriteLine("name", script.Name);

            var existingScripts = Client.StartupScript.GetStartupScripts();

            bool Predicate(KeyValuePair <string, StartupScript> existingScript) =>
            existingScript.Value.name == script.Name &&
            existingScript.Value.type == script.Type.ToString().ToLower();

            if (existingScripts.StartupScripts != null &&
                existingScripts.StartupScripts.Exists(Predicate))
            {
                var(id, _) =
                    existingScripts.StartupScripts.Single(Predicate);
                Console.WriteLine("Script {0} with ID {1} exists",
                                  script.Name, id);

                if (!dryrun)
                {
                    Client.StartupScript.DeleteStartupScript(id);
                    Console.WriteLine("Deleted script {0}", script.Name);
                }
            }
            else
            {
                Console.WriteLine("Script {0} does not exist", script.Name);
            }

            Console.WriteLine("---");
        }
Example #4
0
        /// <summary>
        /// Destroys an existing Vultr firewall.
        /// </summary>
        /// <param name="firewall">The configuration to reference to destroy the
        /// firewall.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Destroy(Firewall firewall, bool dryrun = false)
        {
            Console.WriteLine("Destroying firewall");
            ConsoleX.WriteLine("name", firewall.Name);

            var firewallGroupId = Client.Firewall.GetExistingFirewall(firewall.Name);

            if (!string.IsNullOrEmpty(firewallGroupId))
            {
                Console.WriteLine("Firewall {0} with ID {1} exists",
                                  firewall.Name, firewallGroupId);

                var existingRules =
                    Client.Firewall.GetExistingRules(firewallGroupId);
                foreach (var firewallRule in existingRules)
                {
                    Client.Firewall.DeleteRule(
                        firewall.Name, firewallGroupId, firewallRule.Value, dryrun);
                }

                if (!dryrun)
                {
                    Client.Firewall.DeleteFirewallGroup(firewallGroupId);
                    Console.WriteLine("Deleted firewall {0}", firewall.Name);
                }
            }
            else
            {
                Console.WriteLine("Firewall {0} doesn't exist", firewall.Name);
            }

            Console.WriteLine("---");
        }
Example #5
0
        private void CreateRule(
            string firewallName,
            string firewallGroupId,
            Configuration.FirewallRule rule,
            bool dryrun)
        {
            var ipType   = rule.IpType.ToString().ToLower();
            var protocol = rule.Protocol.ToString().ToLower();

            Console.WriteLine("Creating firewall rule for {0}", firewallName);
            ConsoleX.WriteLine("ip_type", ipType);
            ConsoleX.WriteLine("port", rule.Ports);
            ConsoleX.WriteLine("protocol", protocol);
            ConsoleX.WriteLine("source", rule.Source);
            ConsoleX.WriteLine("subnet", rule.Subnet);
            ConsoleX.WriteLine("subnet_size", rule.SubnetSize);

            if (!dryrun)
            {
                Client.Firewall.CreateFirewallRule(
                    firewallGroupId,
                    ip_type: ipType,
                    protocol: protocol,
                    subnet: rule.Subnet,
                    subnet_size: rule.SubnetSize,
                    port: rule.Ports,
                    source: rule.Source);
            }

            Console.WriteLine("--");
        }
		public static void CheckForAccessFile(ConsoleX consoleX)
		{
			if(!AccessFileDownloader.AccessFileExists)
			{
				consoleX.WriteIntro("Database requirements");
				
				consoleX.WriteWarning("It looks like you don't have the Access database file yet.");
				consoleX.WriteLine("Don't worry, I can download it for you and save it to the correct place.");
				
				var fileDownloaded = AccessFileHelper.DownloadAccessFile(consoleX);
				
				if(fileDownloaded)
					consoleX.WriteLine("You can now continue with your previous task.");
				
				consoleX.WriteHorizontalRule();
			}
		}
		public static void HandleException(Exception ex, ConsoleX consoleX)
		{
			consoleX.WriteException(ex);
			if(consoleX.WriteBooleanQuery("Would you like to report this error?"))
			{
				consoleX.WriteLine("Ok. I'll compose an email for you.");
				consoleX.WriteLine("This should open in your default email client. Please send the email.");
				string mailto = "mailto:[email protected]?subject=Error report: {0}&body={1}";
				string body = "Error: " + ex.Message + Environment.NewLine;
				body += "Type: " + ex.GetType().ToString() + Environment.NewLine;
				body += "Stack Trace: " + Environment.NewLine + ex.StackTrace;
				mailto = string.Format(mailto, ex.Message, body);
				mailto = Uri.EscapeUriString(mailto);
				Process.Start(mailto);
			}
			consoleX.WriteHorizontalRule();
		}
Example #8
0
        static void Main()
        {
            ConsoleX.WriteTitle("MS SQL CLR Assembly Extractor");

            Handler.AddCommands(new ICommandContainer[] { new ProgramCommands() });

            ConsoleX.WriteLine("Use the commands to build a connection string", ConsoleX.MessageStatus.Info);

            ConsoleX.WriteLine("Once complete use -c to return to the exporter.", ConsoleX.MessageStatus.Info);

            Handler.Execute("-bc");

            Handler.Listen();
        }
        public static void ExecuteAll(IEnumerable <IExecutableCommand> exeCommands)
        {
            foreach (var exeCommand in exeCommands)
            {
                var matchedCommands = GetAllCommandsProperties().Where(c => c.IsCommand(exeCommand.Command));
                if (!matchedCommands.Any())
                {
                    ConsoleX.WriteWarningLine($"The command { exeCommand.Command.ShortName ?? exeCommand.Command.LongName } cannot be found", ConsoleX.LogLevel.basic);
                    continue;
                }
                foreach (var cmd in matchedCommands)
                {
                    var result = cmd.Invoke(exeCommand.Arguments?.ToArray());
                    switch (result)
                    {
                    case null:
                        continue;

                    case string str:
                    {
                        ConsoleX.WriteLine(str);
                        break;
                    }

                    case IEnumerable enumerable:
                    {
                        foreach (var s in enumerable.Cast <object>().Select(i => i.ToString()))
                        {
                            ConsoleX.WriteLine(s);
                        }

                        break;
                    }

                    default:
                        ConsoleX.WriteLine(result.ToString());
                        break;
                    }
                }
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            var solution = new Solution();

            while (true)
            {
                //int input = int.Parse(Console.ReadLine());
                //int input2 = int.Parse(Console.ReadLine());
                //int input3 = int.Parse(Console.ReadLine());
                //string input = Console.ReadLine();
                //string input2 = Console.ReadLine();
                //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
                //int input2 = int.Parse(Console.ReadLine());
                //var builder = new DataStructureBuilder();
                //int?[] data = new int?[] { 1, 2, 3, 4, 5, null, 6, null, null, 7, 8 };
                //var tree = builder.BuildTree(data);
                //var listNode = builder.BuildListNode(new int[] { 1, 4, 5 });
                //int[][] arr = new int[3][] { new int[] { 1, 3, 1 }, new int[] { 1, 5, 1 }, new int[] { 4, 2, 1 } };
                //string input = "abcbefga";
                //string input2 = "dbefga";
                //int[] nums1 = new int[] { 1, 2, 3 };
                //int[] nums2 = new int[] { 1, 1 };
                //IList<IList<int>> data = new List<IList<int>>()
                //{
                //    new List<int>() { 1, 3 },
                //    new List<int>() { 3, 0, 1 },
                //    new List<int>() { 2 },
                //    new List<int>() { 0 }

                //    //new List<int>() { 1 },
                //    //new List<int>() { 2 },
                //    //new List<int>() { 3 },
                //    //new List<int>() {  }
                //};

                string str = "ABAB";
                var    res = solution.CharacterReplacement(str, 2);
                ConsoleX.WriteLine(res);
            }
        }
Example #11
0
        /// <summary>
        /// Deletes an existing firewall rule.
        /// </summary>
        /// <param name="client">The FirewallClient to use to delete the firewall
        /// rule.</param>
        /// <param name="firewallName">The name of the firewall the rule belongs
        /// to.</param>
        /// <param name="firewallGroupId">The ID of the firewall the rule belongs
        /// to.</param>
        /// <param name="rule">The rule to delete.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public static void DeleteRule(
            this FirewallClient client,
            string firewallName,
            string firewallGroupId,
            FirewallRule rule,
            bool dryrun)
        {
            Console.WriteLine("Deleting existing firewall rule for {0}", firewallName);
            ConsoleX.WriteLine("port", rule.port);
            ConsoleX.WriteLine("port", rule.rulenumber);
            ConsoleX.WriteLine("port", rule.subnet);
            ConsoleX.WriteLine("port", rule.subnet_size);
            ConsoleX.WriteLine("protocol", rule.protocol);
            ConsoleX.WriteLine("source", rule.source);

            if (!dryrun)
            {
                client.DeleteFirewallRule(firewallGroupId, rule.rulenumber);
            }

            Console.WriteLine("--");
        }
Example #12
0
        public static void WriteCommandHelp(string arg)
        {
            var commandProps = Handler.GetAllCommandsProperties().Where(c => c.IsCommand(arg));

            if (!commandProps.Any())
            {
                ConsoleX.WriteError(new Exception($"command does not exist"), ConsoleX.LogLevel.basic);
                return;
            }
            foreach (var commandProp in commandProps)
            {
                ConsoleX.WriteLine($"short name : { commandProp.Command.ShortName }");
                ConsoleX.WriteLine($"long name : { commandProp.Command.LongName }");
                ConsoleX.WriteLine($"description : { commandProp.Command.Description } ");
                if (!commandProp.HasArguments())
                {
                    ConsoleX.WriteLine($"no arguments");
                    continue;
                }
                ConsoleX.WriteLines(commandProp.GetArgDescriptions());
            }
        }
Example #13
0
        /// <summary>
        /// Destroys an existing Vultr server.
        /// </summary>
        /// <param name="server">The configuration to reference to destroy the
        /// server.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Destroy(Server server, bool dryrun = false)
        {
            var dcId  = Client.Region.GetRegionId(server.Region);
            var label = server.Label;

            Console.WriteLine("Destroying server");
            ConsoleX.WriteLine("DCID", dcId);
            ConsoleX.WriteLine("label", label);

            var existingServers = Client.Server.GetServers();

            bool Predicate(KeyValuePair <
                               string, global::Vultr.API.Models.Server> existingServer) =>
            existingServer.Value.DCID == dcId.ToString() &&
            existingServer.Value.label == label;

            if (existingServers.Servers != null &&
                existingServers.Servers.Exists(Predicate))
            {
                var(id, _) = existingServers.Servers.Single(Predicate);
                Console.WriteLine("Server with label {0} in DCID {1} exists",
                                  label, dcId);

                var subId = int.Parse(id);
                if (!dryrun)
                {
                    Client.Server.DestroyServer(subId);
                    Console.WriteLine("Deleted server {0}", subId);
                }
            }
            else
            {
                Console.WriteLine("Server with label {0} in DCID {1} does not exist",
                                  label, dcId);
            }

            Console.WriteLine("---");
        }
Example #14
0
        /// <summary>
        /// Provisions a new Vultr firewall.
        /// </summary>
        /// <param name="firewall">The configuration to reference to provision the
        /// firewall.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Provision(Firewall firewall, bool dryrun = false)
        {
            Console.WriteLine("Creating firewall");
            ConsoleX.WriteLine("name", firewall.Name);

            var firewallGroupId = Client.Firewall.GetExistingFirewall(firewall.Name);

            if (!string.IsNullOrEmpty(firewallGroupId))
            {
                Console.WriteLine("Firewall {0} with ID {1} already exists",
                                  firewall.Name, firewallGroupId);

                var existingRules =
                    Client.Firewall.GetExistingRules(firewallGroupId);
                CreateRules(firewall, firewallGroupId, dryrun, existingRules);
                DeleteOldRules(firewall, firewallGroupId, existingRules, dryrun);
            }
            else
            {
                CreateRules(firewall, CreateNewFirewallGroup(firewall, dryrun), dryrun);
            }

            Console.WriteLine("---");
        }
		public static bool TrySearchForNames(Volunteer volunteer, ConsoleX consoleX)
		{
			// TODO Use Gender also to help with match.
			bool matchesFound = true;
			
			consoleX.WriteLine("Searching for matches on both First and Last name...");
			
			var table = Volunteers.GetByBothNames(volunteer.FirstName, volunteer.LastName, volunteer.Gender);
			
			if(table.Rows.Count == 0)
			{
				consoleX.WriteLine("No matched found on both First and Last name! Trying a wider search:");
				if(volunteer.Gender == GenderKind.Male)
				{
					consoleX.WriteLine("Searching for other brothers with same Last name...");
					table = Volunteers.GetByLastName(volunteer.LastName, volunteer.Gender);
				}
				else
				{
					consoleX.WriteLine("Searching for other sisters with same First OR Last name...");
					table = Volunteers.GetByEitherName(volunteer.FirstName, volunteer.LastName, volunteer.Gender);
				}
			}
			
			if(table.Rows.Count > 0)
			{
				consoleX.WriteLine(table.Rows.Count > 1 ? "The following matches where found:" : "The following match was found");
				consoleX.WriteDataTable(table, 15);
			}
			else
			{
				matchesFound = false;
				consoleX.WriteLine("No matches found!");
			}
			return matchesFound;
		}
Example #16
0
        /// <summary>
        /// Provisions a new Vultr startup script.
        /// </summary>
        /// <param name="script">The configuration to reference to provision the
        /// script.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Provision(Script script, bool dryrun = false)
        {
            var type = script.Type switch
            {
                ScriptType.Boot => global::Vultr.API.Models.ScriptType.boot,
                ScriptType.PXE => global::Vultr.API.Models.ScriptType.pxe,
                _ => throw new ArgumentException(
                          $"Unknown script type {script.Type}", nameof(script)),
            };

            Console.WriteLine("Creating script");
            ConsoleX.WriteLine("name", script.Name);
            ConsoleX.WriteLine("type", type);
            ConsoleX.WriteLine("script", script.Content);

            var existingScripts = Client.StartupScript.GetStartupScripts();

            bool Predicate(KeyValuePair <string, StartupScript> existingScript) =>
            existingScript.Value.name == script.Name;

            if (existingScripts.StartupScripts != null &&
                existingScripts.StartupScripts.Exists(Predicate))
            {
                var(id, existingScript) =
                    existingScripts.StartupScripts.Single(Predicate);
                Console.WriteLine("Script {0} with ID {1} already exists",
                                  script.Name, id);

                if (existingScript.type != script.Type.ToString().ToLower())
                {
                    Console.WriteLine("Script {0} type is different", script.Name);
                    Console.WriteLine("Deleting script {0}", script.Name);

                    if (!dryrun)
                    {
                        Client.StartupScript.DeleteStartupScript(id);
                        Console.WriteLine("Deleted script {0}", script.Name);
                    }

                    Console.WriteLine("Creating new script called {0}", script.Name);
                    if (!dryrun)
                    {
                        var result = Client.StartupScript.CreateStartupScript(
                            script.Name, script.Content, type);
                        Console.WriteLine("Created script with ID {0}",
                                          result.StartupScript.SCRIPTID);
                    }
                }
                else if (existingScript.script != script.Content)
                {
                    Console.WriteLine("Script {0} content is different", script.Name);
                    Console.WriteLine("Updating script {0} content", script.Name);

                    if (!dryrun)
                    {
                        Client.StartupScript.UpdateStartupScript(
                            id, script: script.Content);
                        Console.WriteLine("Updated script {0} content", script.Name);
                    }
                }
                else
                {
                    Console.WriteLine("Script {0} is the same. Moving on.", script.Name);
                }
            }
            else
            {
                if (!dryrun)
                {
                    var result = Client.StartupScript.CreateStartupScript(
                        script.Name, script.Content, type);
                    Console.WriteLine(
                        "Created script with ID {0}", result.StartupScript.SCRIPTID);
                }
            }

            Console.WriteLine("---");
        }
Example #17
0
		public static string[] GetFiles(ConsoleX consoleX)
		{
			consoleX.WriteLine("Press any key to select the S82 PDF files...");
			Console.ReadKey();
			
			string[] files = null;
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Multiselect = true;
			dlg.Title = "Choose S82 PDF files";
			dlg.Filter = "PDF Files|*.pdf";
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				files = dlg.FileNames;
			}
			return files;
		}
Example #18
0
		public static void ShowStartUpMessage(ConsoleX consoleX)
		{
			// Display application title
			consoleX.WriteTitle("RBC Console, application for interfacing with RBC South Wales and Gloucestershire database");
			consoleX.WriteLine("Enter a command to start (e.g. 'help')");
		}
Example #19
0
        /// <summary>
        /// Provisions a new Vultr server.
        /// </summary>
        /// <param name="server">The configuration to reference to provision the
        /// server.</param>
        /// <param name="dryrun">Whether or not this is a dryrun. If set to true then
        /// provision commands will not be sent to the platform and instead messaging
        /// will be outputted describing what would be done.</param>
        public override void Provision(Server server, bool dryrun = false)
        {
            var        os                   = GetOs(server);
            const bool notifyActivate       = false;
            var        appId                = os.Appid;
            var        dcId                 = Client.Region.GetRegionId(server.Region);
            var        enablePrivateNetwork = server.PrivateNetworking;
            var        firewallId           = GetFirewallId(server.Firewall);
            var        isoId                = os.IsoId?.ToString();
            var        label                = server.Label;
            var        osId                 = os.OsId;
            var        scriptId             = os.ScriptId;
            var        snapshotId           = os.SnapshotId;
            var        sshKeys              = GetSshKeys(server.SshKeys);
            var        tag                  = server.Tag;
            var        userdata             = server.UserData.Base64Encode();
            var        vpsPlanId            = GetPlanId(server.Plan);

            Console.WriteLine("Provisioning server");
            ConsoleX.WriteLine("APPID", appId);
            ConsoleX.WriteLine("DCID", dcId);
            ConsoleX.WriteLine("enable_private_network", enablePrivateNetwork);
            ConsoleX.WriteLine("FIREWALLGROUPID", firewallId);
            ConsoleX.WriteLine("ISOID", isoId);
            ConsoleX.WriteLine("label", label);
            ConsoleX.WriteLine("notify_activate", notifyActivate);
            ConsoleX.WriteLine("OSID", osId);
            ConsoleX.WriteLine("SCRIPTID", scriptId);
            ConsoleX.WriteLine("SNAPSHOTID", snapshotId);
            ConsoleX.WriteLine("SSHKEYID", sshKeys);
            ConsoleX.WriteLine("tag", tag);
            ConsoleX.WriteLine("userdata", server.UserData);
            ConsoleX.WriteLine("VPSPLANID", vpsPlanId);

            var existingServers = Client.Server.GetServers();

            bool Predicate(KeyValuePair <
                               string, global::Vultr.API.Models.Server> existingServer) =>
            existingServer.Value.DCID == dcId.ToString() &&
            existingServer.Value.label == label;

            if (existingServers.Servers != null &&
                existingServers.Servers.Exists(Predicate))
            {
                var(id, existingServer) = existingServers.Servers.Single(Predicate);
                Console.WriteLine("Server with label {0} in DCID {1} already exists",
                                  label, dcId);

                var vultrServer = new global::Vultr.API.Models.Server
                {
                    OSID            = osId.ToString(),
                    tag             = tag,
                    label           = label,
                    APPID           = appId?.ToString(),
                    DCID            = dcId.ToString(),
                    FIREWALLGROUPID = firewallId,
                    VPSPLANID       = vpsPlanId.ToString()
                };

                // TODO: Check if userdata has changed

                var subId = int.Parse(id);
                if (!vultrServer.IsEquivalent(existingServer))
                {
                    Console.WriteLine("Server is different. Deleting server {0}", subId);

                    if (!dryrun)
                    {
                        Client.Server.DestroyServer(subId);
                        Console.WriteLine("Deleted server {0}", subId);
                    }
                }
                else
                {
                    Console.WriteLine("Server {0} is the same. Moving on.", subId);
                    return;
                }
            }

            if (!dryrun)
            {
                var result = Client.Server.CreateServer(
                    dcId,
                    vpsPlanId,
                    osId,
                    APPID: appId,
                    enable_private_network: enablePrivateNetwork,
                    FIREWALLGROUPID: firewallId,
                    ISOID: isoId,
                    label: label,
                    notify_activate: notifyActivate,
                    SCRIPTID: scriptId,
                    SNAPSHOTID: snapshotId,
                    SSHKEYID: sshKeys,
                    tag: tag,
                    userdata: userdata
                    );

                Console.WriteLine("Provisioned server with ID {0}", result.Server.SUBID);
            }

            Console.WriteLine("---");
        }