コード例 #1
0
ファイル: HostController.cs プロジェクト: r-Larch/Host
        public void Remove(Filter filter, FilterProp what, bool force)
        {
            List <HostsFileLine> hosts;

            using (new Watch("read file")) {
                hosts = _hostsFile.GetHosts().ToList();
            }

            var filterd = Filter(hosts, filter, what);

            if (!force)
            {
                Console.WriteLine($"found {filterd.Count} to remove\r\n");
                filterd = ConsoleEx.AskYesOrNo(filterd, x => new ConsoleWriter()
                                               .FormatLine("remove '{line} {disabled} {ip}    {domain} {comment}'?", parms => parms
                                                           .Add("line", x.LineNumber, what == FilterProp.Line)
                                                           .Add("disabled", x.IsDisabled ? "#" : "")
                                                           .Add("ip", x.Ip, what == FilterProp.Domain)
                                                           .Add("domain", x.Domain, what == FilterProp.Domain)
                                                           .Add("comment", x.Commentar, what == FilterProp.Commentar)
                                                           )
                                               );
            }

            if (filterd.Count == 0)
            {
                Console.WriteLine($"-- nothing to remove");
                return;
            }

            using (new Watch("delete")) {
                _hostsFile.Remove(filterd.Select(x => x.Model));
            }
            filterd.ForEach(x => Console.WriteLine($"removed: {HostsFile.CreateTextLine(x.Model)}"));
        }
コード例 #2
0
ファイル: CreateTextLineTester.cs プロジェクト: r-Larch/Host
        public void CreateTextLineTest()
        {
            var line = HostsFile.CreateTextLine(new HostsFileLine(0)
            {
                Ipv4   = "127.0.0.1",
                Domain = "lokalhost",
            });

            Assert.AreEqual("127.0.0.1 lokalhost", line);
            Console.WriteLine(line);

            line = HostsFile.CreateTextLine(new FileLine {
                IsDisabled = true,
                Ip         = "127.0.0.1",
                Domain     = "lokalhost",
            });
            Assert.AreEqual("# 127.0.0.1 lokalhost", line);
            Console.WriteLine(line);

            line = HostsFile.CreateTextLine(new FileLine {
                Ip        = "127.0.0.1",
                Domain    = "lokalhost",
                Commentar = "Hallo "
            });
            Assert.AreEqual("127.0.0.1 lokalhost # Hallo", line);
            Console.WriteLine(line);

            line = HostsFile.CreateTextLine(new FileLine {
                IsDisabled = true,
                Ip         = "127.0.0.1",
                Domain     = "lokalhost",
                Commentar  = "Hallo "
            });
            Assert.AreEqual("# 127.0.0.1 lokalhost # Hallo", line);
            Console.WriteLine(line);

            line = HostsFile.CreateTextLine(new FileLine {
                IsCommentarLine = true,
                IsDisabled      = true,
                Ip        = "127.0.0.1",
                Domain    = "lokalhost",
                Commentar = "Hallo "
            });
            Assert.AreEqual("# Hallo", line);
            Console.WriteLine(line);
        }
コード例 #3
0
ファイル: HostController.cs プロジェクト: r-Larch/Host
        public void Duplicates(Filter filter, FilterProp what)
        {
            List <HostsFileLine> hosts;

            using (new Watch("read file")) {
                hosts = _hostsFile.GetHosts().ToList();
            }

            var filterd = Filter(hosts, filter, what);

            var duplicates = (from f in filterd
                              group f by $"{f.Ip.Value} {f.Domain.Value}"
                              into g
                              where g.Count() > 1
                              select g).ToList();

            ConsoleEx.PrintWithPaging(
                list: duplicates,
                countAll: hosts.Count,
                line: (models, i) => new ConsoleWriter()
                .FormatLine("{count,5} {domain}", parms => parms
                            .Add("count", models.Count())
                            .Add("domain", models.Key)
                            )
                .FormatLines("          {ip} {domain}", models.ToList(), (model, parms) => parms
                             .Add("ip", model.Ip)
                             .Add("domain", model.Domain)
                             )
                );

            if (duplicates.Any())
            {
                var toremove = duplicates.SelectMany(x => x.Skip(1).Select(_ => _.Model)).ToList();
                if (ConsoleEx.AskForYes($"remove {toremove.Count} duplicates?"))
                {
                    _hostsFile.Remove(toremove);
                    toremove.ForEach(x => Console.WriteLine($"removed: {HostsFile.CreateTextLine(x)}"));
                }
            }
        }