Skip to content

Commit

Permalink
Release 6.0.4: Add options to run in quiet mode: -q|--quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxiangyao committed May 6, 2022
1 parent 64852eb commit 98ec253
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
73 changes: 40 additions & 33 deletions src/IpFix/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using System.Text.RegularExpressions;

namespace Myvas.Tools.IpFix;

Expand Down Expand Up @@ -53,56 +50,66 @@ static void Main(string[] args)
return;
}

var quietMode = args.Contains("-q")
|| args.Contains("--quiet");
if (quietMode)
{
args = args.Except(new[] { "-q", "--quiet" }).ToArray();
}

foreach (var dns in args)
{
if (!dns.StartsWith('-') && !dns.StartsWith('/')) Fix(dns);
}

if (!quietMode)
{
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
finally
{
if ((args?.Contains("-q") ?? false)
|| (args?.Contains("--quiet") ?? false))
{ }
else
{
try
{
var isInConsole = Console.CursorLeft >= int.MinValue;
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
catch { }
}
}
}

public static void Fix(string dns)
{
var ipAddress = IpAddressFetcher.RetrieveIpAddress(dns);
if (string.IsNullOrWhiteSpace(ipAddress))
try
{
Console.WriteLine($"No record of '{dns}' be found on {IpAddressFetcher.Name}.");
return;
}
Console.WriteLine($"The IP address of {dns} is {ipAddress}");
var ipAddress = IpAddressFetcher.RetrieveIpAddress(dns);
if (string.IsNullOrWhiteSpace(ipAddress))
{
Console.WriteLine($"No record of '{dns}' be found on {IpAddressFetcher.Name}.");
return;
}
Console.WriteLine($"The IP address of {dns} is {ipAddress}");

var hostsFile = new HostsFile();
var changed = hostsFile.UpdateHostsRecord(dns, ipAddress, IpAddressFetcher.Name);
if (!changed)
{
Console.WriteLine($"The record does not need to update.");
return;
}
var hostsFile = new HostsFile();
var changed = hostsFile.UpdateHostsRecord(dns, ipAddress, IpAddressFetcher.Name);
if (!changed)
{
Console.WriteLine($"The record does not need to update.");
return;
}

hostsFile.Write();
Console.WriteLine($"{dns} updated to {ipAddress}");
hostsFile.Write();
Console.WriteLine($"{dns} updated to {ipAddress}");

IpConfigHelper.Flushdns();
IpConfigHelper.Flushdns();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

7 changes: 5 additions & 2 deletions src/IpFix/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"profiles": {
"Myvas.Tools.IpFix": {
"ipfix github.com": {
"commandName": "Project",
"commandLineArgs": "gmail.com"
"commandLineArgs": "github.com"
},
"ipfix": {
"commandName": "Project"
}
}
}

0 comments on commit 98ec253

Please sign in to comment.