Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #448 from whoisj/refactor-program
Browse files Browse the repository at this point in the history
Refactor `Program`
  • Loading branch information
J Wyman authored Jun 15, 2017
2 parents 0f35098 + 2d6a6fa commit 21c9a39
Show file tree
Hide file tree
Showing 22 changed files with 1,840 additions and 1,412 deletions.
Binary file modified Cli-Askpass/GlobalSuppressions.cs
Binary file not shown.
82 changes: 47 additions & 35 deletions Cli-Askpass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ namespace Microsoft.Alm.Cli
{
internal partial class Program
{
public const string Title = "Askpass Utility for Windows";
public const string Description = "Secure askpass utility for Windows, by Microsoft";
public const string AssemblyTitle = "Askpass Utility for Windows";
public const string AssemblyDesciption = "Secure askpass utility for Windows, by Microsoft";
public const string DefinitionUrlPassphrase = "https://www.visualstudio.com/docs/git/gcm-ssh-passphrase";

private static readonly Regex AskCredentialRegex = new Regex(@"(\S+)\s+for\s+['""]([^'""]+)['""]:\s*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex AskPassphraseRegex = new Regex(@"Enter\s+passphrase\s*for\s*key\s*['""]([^'""]+)['""]\:\s*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex AskPasswordRegex = new Regex(@"(\S+)'s\s+password:\s*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
private static readonly Regex AskAuthenticityRegex = new Regex(@"^\s*The authenticity of host '([^']+)' can't be established.\s+RSA key fingerprint is ([^\s:]+:[^\.]+).", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

internal static bool TryParseUrlCredentials(string targetUrl, out string username, out string password)
internal Program()
{
Title = AssemblyTitle;
}

internal bool TryParseUrlCredentials(string targetUrl, out string username, out string password)
{
// config stored credentials come in the format of <username>[:<password>]@<url>
// with password being optional scheme terminator is actually "://" so we need
Expand Down Expand Up @@ -90,7 +95,7 @@ internal static bool TryParseUrlCredentials(string targetUrl, out string usernam
return false;
}

private static void Askpass(string[] args)
internal void Askpass(string[] args)
{
if (args == null || args.Length == 0)
throw new ArgumentException("Arguments cannot be empty.");
Expand Down Expand Up @@ -271,8 +276,46 @@ private static void Askpass(string[] args)
Die("failed to acquire credentials.");
}

internal void PrintHelpMessage()
{
const string HelpFileName = "git-askpass.html";

Console.Out.WriteLine("usage: git askpass '<user_prompt_text>'");

List<Git.GitInstallation> installations;
if (Git.Where.FindGitInstallations(out installations))
{
foreach (var installation in installations)
{
if (Directory.Exists(installation.Doc))
{
string doc = Path.Combine(installation.Doc, HelpFileName);

// if the help file exists, send it to the operating system to display to the user
if (File.Exists(doc))
{
Git.Trace.WriteLine($"opening help documentation '{doc}'.");

Process.Start(doc);

return;
}
}
}
}

Die("Unable to open help documentation.");
}

[STAThread]
private static void Main(string[] args)
{
Program program = new Program();

program.Run(args);
}

private void Run(string[] args)
{
EnableDebugTrace();

Expand Down Expand Up @@ -310,36 +353,5 @@ private static void Main(string[] args)

Trace.Flush();
}

private static void PrintHelpMessage()
{
const string HelpFileName = "git-askpass.html";

Console.Out.WriteLine("usage: git askpass '<user_prompt_text>'");

List<Git.GitInstallation> installations;
if (Git.Where.FindGitInstallations(out installations))
{
foreach (var installation in installations)
{
if (Directory.Exists(installation.Doc))
{
string doc = Path.Combine(installation.Doc, HelpFileName);

// if the help file exists, send it to the operating system to display to the user
if (File.Exists(doc))
{
Git.Trace.WriteLine($"opening help documentation '{doc}'.");

Process.Start(doc);

return;
}
}
}
}

Die("Unable to open help documentation.");
}
}
}
6 changes: 3 additions & 3 deletions Cli-Askpass/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle(Microsoft.Alm.Cli.Program.Title)]
[assembly: AssemblyDescription(Microsoft.Alm.Cli.Program.Title + ". " + Microsoft.Alm.Cli.Program.SourceUrl)]
[assembly: AssemblyTitle(Microsoft.Alm.Cli.Program.AssemblyTitle)]
[assembly: AssemblyDescription(Microsoft.Alm.Cli.Program.AssemblyDesciption + ". " + Microsoft.Alm.Cli.Program.SourceUrl)]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct(Microsoft.Alm.Cli.Program.Title + " command line interface")]
[assembly: AssemblyProduct(Microsoft.Alm.Cli.Program.AssemblyTitle + " command line interface.")]
[assembly: AssemblyCopyright("Copyright © Microsoft Corporation 2017. All rights reserved.")]
[assembly: AssemblyTrademark("Microsoft Corporation")]
[assembly: AssemblyCulture("")]
Expand Down
20 changes: 12 additions & 8 deletions Cli-CredentialHelper.Test/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public class ProgramTests
[TestMethod]
public void LoadOperationArgumentsTest()
{
Program._dieExceptionCallback = (Exception e) => Assert.Fail($"Error: {e.ToString()}");
Program._dieMessageCallback = (string m) => Assert.Fail($"Error: {m}");
Program._exitCallback = (int e, string m) => Assert.Fail($"Error: {e} {m}");
Program program = new Program();

program._dieException = (Program caller, Exception e, string path, int line, string name) => Assert.Fail($"Error: {e.ToString()}");
program._dieMessage = (Program caller, string m, string path, int line, string name) => Assert.Fail($"Error: {m}");
program._exit = (Program caller, int e, string m, string path, int line, string name) => Assert.Fail($"Error: {e} {m}");

var envvars = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
Expand All @@ -60,7 +62,7 @@ public void LoadOperationArgumentsTest()

var opargs = opargsMock.Object;

Program.LoadOperationArguments(opargs);
program.LoadOperationArguments(opargs);

Assert.IsNotNull(opargs);
}
Expand Down Expand Up @@ -89,10 +91,12 @@ public void TryReadBooleanTest()
opargsMock.Setup(r => r.QueryUri)
.Returns(targetUri);

Assert.IsFalse(Program.TryReadBoolean(opargsMock.Object, "notFound", "notFound", out yesno));
Program program = new Program();

Assert.IsFalse(CommonFunctions.TryReadBoolean(program, opargsMock.Object, "notFound", "notFound", out yesno));
Assert.IsFalse(yesno.HasValue);

Assert.IsTrue(Program.TryReadBoolean(opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(yesno.HasValue);
Assert.IsFalse(yesno.Value);

Expand All @@ -104,7 +108,7 @@ public void TryReadBooleanTest()
opargsMock.Setup(r => r.EnvironmentVariables)
.Returns(envvars);

Assert.IsTrue(Program.TryReadBoolean(opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsTrue(yesno.HasValue);
Assert.IsTrue(yesno.Value);

Expand All @@ -116,7 +120,7 @@ public void TryReadBooleanTest()
opargsMock.Setup(r => r.EnvironmentVariables)
.Returns(envvars);

Assert.IsFalse(Program.TryReadBoolean(opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsFalse(CommonFunctions.TryReadBoolean(program, opargsMock.Object, Program.ConfigPreserveCredentialsKey, Program.EnvironPreserveCredentialsKey, out yesno));
Assert.IsFalse(yesno.HasValue);
}
}
Expand Down
Binary file modified Cli-CredentialHelper/GlobalSuppressions.cs
Binary file not shown.
52 changes: 29 additions & 23 deletions Cli-CredentialHelper/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ internal class Installer
"git-credential-manager.html",
};

public Installer()
public Installer(Program program)
{
_program = program;

var args = Environment.GetCommandLineArgs();

// parse arguments
Expand Down Expand Up @@ -93,7 +95,24 @@ public Installer()
}
}

internal static string CygwinPath
private string _customPath = null;
private string _cygwinPath;
private bool _isPassive = false;
private bool _isForced = false;
private Program _program;
private TextWriter _stdout = null;
private TextWriter _stderr = null;
private string _userBinPath = null;

public int ExitCode
{
get { return (int)Result; }
set { Result = (ResultValue)value; }
}

public ResultValue Result { get; private set; }

internal string CygwinPath
{
get
{
Expand Down Expand Up @@ -130,9 +149,12 @@ internal static string CygwinPath
}
}

private static string _cygwinPath;
internal Program Program
{
get { return _program; }
}

internal static string UserBinPath
internal string UserBinPath
{
get
{
Expand Down Expand Up @@ -173,22 +195,6 @@ internal static string UserBinPath
}
}

private static string _userBinPath = null;

public int ExitCode
{
get { return (int)Result; }
set { Result = (ResultValue)value; }
}

public ResultValue Result { get; private set; }

private bool _isPassive = false;
private bool _isForced = false;
private string _customPath = null;
private TextWriter _stdout = null;
private TextWriter _stderr = null;

public void DeployConsole()
{
SetOutput(_isPassive, _isPassive && _isForced);
Expand Down Expand Up @@ -426,7 +432,7 @@ public void DeployConsole()
}
}

public static bool DetectNetFx(out Version version)
public bool DetectNetFx(out Version version)
{
const string NetFxKeyBase = @"HKEY_LOCAL_MACHINE\Software\Microsoft\Net Framework Setup\NDP\v4\";
const string NetFxKeyClient = NetFxKeyBase + @"\Client";
Expand All @@ -441,9 +447,9 @@ public static bool DetectNetFx(out Version version)
Version netfxVerson = null;

// query for existing installations of .NET
if ((netfxString = Registry.GetValue(NetFxKeyClient, ValueName, DefaultValue) as String) != null
if ((netfxString = Registry.GetValue(NetFxKeyClient, ValueName, DefaultValue) as string) != null
&& Version.TryParse(netfxString, out netfxVerson)
|| (netfxString = Registry.GetValue(NetFxKeyFull, ValueName, DefaultValue) as String) != null
|| (netfxString = Registry.GetValue(NetFxKeyFull, ValueName, DefaultValue) as string) != null
&& Version.TryParse(netfxString, out netfxVerson))
{
Program.LogEvent($"NetFx version {netfxVerson.ToString(3)} detected.", EventLogEntryType.Information);
Expand Down
Loading

0 comments on commit 21c9a39

Please sign in to comment.