Exemplo n.º 1
0
        private void Run(Options options)
        {
            var hostfile = new HostsFile();
            var host     = new HostController(hostfile);

            // edit
            if (options.Edit)
            {
                host.Edit();
                return;
            }


            // setup filter
            var filter = new Filter(options.Value,
                                    options.Regex
                    ? CampareType.Regex
                    : CampareType.WildCard,
                                    CompareMode.CaseIgnore
                                    );
            var filterProp = FilterProp.Domain;

            if (options.Ip)
            {
                filterProp = FilterProp.Ip;
            }
            if (options.Line)
            {
                filterProp = FilterProp.Line;
            }
            if (options.Commentar)
            {
                filterProp = FilterProp.Commentar;
            }
            if (options.IsDisabled)
            {
                filterProp = FilterProp.IsDisabled;
            }

            // list
            if (options.Duplicates)
            {
                filter.OnEmptyMatchAll = true;
                host.Duplicates(filter, filterProp);
                return;
            }

            // list
            if (options.List)
            {
                filter.OnEmptyMatchAll = true;
                host.List(filter, filterProp);
                return;
            }

            // handle empty value
            if (string.IsNullOrEmpty(options.Value))
            {
                Console.WriteLine(options.GetUsage());
                return;
            }

            // remove value
            if (options.Remove)
            {
                host.Remove(filter, filterProp, options.Force);
                return;
            }

            // add value
            if (options.Add || !string.IsNullOrEmpty(options.Value))
            {
                var ip  = string.IsNullOrEmpty(options.Value2) ? "127.0.0.1" : options.Value;
                var url = string.IsNullOrEmpty(options.Value2) ? options.Value : options.Value2;
                host.Add(ip, url);
                return;
            }
        }