Example #1
0
        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)}"));
                }
            }
        }
Example #2
0
        public void TestRead()
        {
            var hostsfile = new HostsFile(MyFile);
            List <HostsFileLine> hosts;

            using (new Watch("GetHosts")) {
                hosts = hostsfile.GetHosts().ToList();
            }

            // print
            Watch.PrintTasks();
            var table = new Table();

            table.Create(1, 1, "Hosts", hosts);

            // validate
            Assert.IsNotEmpty(hosts);
            Assert.AreEqual(11 * REPEAT, hosts.Count, "Count all");
            Assert.AreEqual(0 * REPEAT, hosts.Count(x => x.IsCommentarLine), "CommentarLine count");
            Assert.AreEqual(5 * REPEAT, hosts.Count(x => x.IsDisabled), "Disabled count");
            Assert.AreEqual(3 * REPEAT, hosts.Count(x => !string.IsNullOrEmpty(x.Ipv6)), "Ipv6 count");
            Assert.AreEqual(2 * REPEAT, hosts.Count(x => !string.IsNullOrEmpty(x.Commentar)), "Commentar count");
        }