Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to uninstall by passing "-u" #10

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 93 additions & 16 deletions git-credential-winstore/Program.cs
Expand Up @@ -22,20 +22,33 @@ class Program
{ "erase", EraseCommand }
};

private static bool _isSilent;

static void Main(string[] args)
{
TryLaunchDebugger(ref args);
if (TrySilentInstall(ref args)) { return; }

SetIfSilent(ref args);

if (IsAlreadyInstalled())
{
if (TrySilentUninstall(ref args)) { return; }
}
else
{
if (TrySilentInstall(ref args)) { return; }
}


// Parse command
Func<IDictionary<string, string>, IEnumerable<Tuple<string, string>>> command = null;
string cmd;
if (args.Length == 0)
{
InstallTheApp(silent: false);
InstallTheApp();
return;
}

cmd = args[0];

IDictionary<string, string> parameters = ReadGitParameters();
Expand All @@ -58,11 +71,34 @@ static void Main(string[] args)
WriteGitParameters(response);
}

private static bool IsAlreadyInstalled()
{
return new DirectoryInfo(Environment.ExpandEnvironmentVariables(@"%AppData%\GitCredStore")).Exists;
}

private static void SetIfSilent(ref string[] args)
{
if (IsArgumentPresent(ref args, "-s"))
{
args = args.Skip(1).ToArray();
_isSilent = true;
}
else
{
_isSilent = false;
}
}

private static bool IsArgumentPresent(ref string[] args, string dashArgumentValue)
{
return args.Length > 0 && args[0] == dashArgumentValue;
}

// Conditional methods can return anything, so we use a ref arg... :S
[Conditional("DEBUG")]
private static void TryLaunchDebugger(ref string[] args)
{
if (args.Length > 0 && args[0] == "-d")
if (IsArgumentPresent(ref args, "-d"))
{
Console.Error.WriteLine("Launching debugger...");
Debugger.Launch();
Expand All @@ -72,10 +108,20 @@ private static void TryLaunchDebugger(ref string[] args)

private static bool TrySilentInstall(ref string[] args)
{
if (args.Length > 0 && args[0] == "-s")
if (_isSilent)
{
Console.Out.WriteLine("Silently Installing...");
InstallTheApp(silent: true);
InstallTheApp();
return true;
}

return false;
}

private static bool TrySilentUninstall(ref string[] args)
{
if (IsArgumentPresent(ref args, "-u"))
{
UninstallTheApp();
args = args.Skip(1).ToArray();
return true;
}
Expand Down Expand Up @@ -110,16 +156,20 @@ private static void WriteUsage()
Console.Error.WriteLine("See the following link for more info: http://www.manpagez.com/man/1/git-credential-cache/");
}

private static void InstallTheApp(bool silent)
private static void InstallTheApp()
{
if(!silent)
if (!_isSilent)
{
if (MessageBox.Show("Do you want to install git-credential-winstore to prompt for passwords?",
"Installing git-credential-winstore", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
}
else
{
Console.Out.WriteLine("Silently Installing...");
}

var target = new DirectoryInfo(Environment.ExpandEnvironmentVariables(@"%AppData%\GitCredStore"));
if (!target.Exists)
Expand All @@ -133,6 +183,31 @@ private static void InstallTheApp(bool silent)
Process.Start("git", string.Format("config --global credential.helper \"!'{0}'\"", dest.FullName));
}

private static void UninstallTheApp()
{
var target = new DirectoryInfo(Environment.ExpandEnvironmentVariables(@"%AppData%\GitCredStore"));

if (!_isSilent)
{
if (MessageBox.Show("Do you want to uninstall git-credential-winstore?",
"Uninstalling git-credential-winstore", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
}
else
{
Console.Out.WriteLine("Silently Uninstalling...");
}

if (target.Exists)
{
target.Delete(true);
}

Process.Start("git", "config --global --remove-section credential");
}

static IEnumerable<Tuple<string, string>> GetCommand(IDictionary<string, string> args)
{
// Build the URL
Expand All @@ -144,7 +219,7 @@ private static void InstallTheApp(bool silent)

string userName = args.GetOrDefault("username", null);
string password = null;

IntPtr credPtr = IntPtr.Zero;
try
{
Expand All @@ -161,7 +236,7 @@ private static void InstallTheApp(bool silent)
credPtr = IntPtr.Zero;

// If we have a username, pack an input authentication buffer
Tuple<int, IntPtr> inputBuffer = null;;
Tuple<int, IntPtr> inputBuffer = null; ;
IntPtr outputBuffer = IntPtr.Zero;
int outputBufferSize = 0;
try
Expand Down Expand Up @@ -227,7 +302,7 @@ private static void InstallTheApp(bool silent)
userName = cred.userName;
password = Marshal.PtrToStringBSTR(cred.credentialBlob);
}

yield return Tuple.Create("username", userName);
yield return Tuple.Create("password", password);
}
Expand Down Expand Up @@ -285,7 +360,7 @@ private static bool UnPackAuthBuffer(IntPtr buffer, int size, out string userNam
}
IntPtr buf = IntPtr.Zero;
int size = 0;

// First, calculate size. (buf == IntPtr.Zero)
var result = NativeMethods.CredPackAuthenticationBuffer(
dwFlags: 4, // CRED_PACK_GENERIC_CREDENTIALS
Expand Down Expand Up @@ -322,10 +397,12 @@ private static bool UnPackAuthBuffer(IntPtr buffer, int size, out string userNam
string password = args.GetOrDefault("password", String.Empty);

bool abort = false;
if(abort |= String.IsNullOrEmpty(userName)) {
if (abort |= String.IsNullOrEmpty(userName))
{
Console.Error.WriteLine("username parameter must be provided");
}
if(abort |= String.IsNullOrEmpty(password)) {
if (abort |= String.IsNullOrEmpty(password))
{
Console.Error.WriteLine("password parameter must be provided");
}
if (!abort)
Expand All @@ -347,7 +424,7 @@ private static bool UnPackAuthBuffer(IntPtr buffer, int size, out string userNam
Console.Error.WriteLine(
"Failed to write credential: " +
GetLastErrorMessage());
}
}
}
return Enumerable.Empty<Tuple<string, string>>();
}
Expand Down