-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathClassCommandLine.cs
148 lines (136 loc) · 6.67 KB
/
ClassCommandLine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Process command line arguments
/// </summary>
static class ClassCommandLine
{
public static int ReturnCode = -1;
public static string initRepo = null;
private static bool runGitForce = true;
/// <summary>
/// The main execution function for command line arguments.
/// Returns 'false' if the main program should not continue with the execution, in which
/// case ReturnCode contains the return code to return to the OS.
/// </summary>
public static bool Execute(Arguments commandLine)
{
// WAR: On Windows, re-attach the console so we can print. Mono does not need that to use Console class.
if (!ClassUtils.IsMono())
{
NativeMethods.AttachConsole();
Console.WriteLine(Environment.NewLine);
}
if (commandLine["help"] == "true" || commandLine["?"] == "true")
{
Console.WriteLine(Environment.NewLine +
"GitForce optional arguments:" + Environment.NewLine +
" --version Show the application version number." + Environment.NewLine +
" --reset-windows Reset stored locations of windows and dialogs." + Environment.NewLine +
" --reset-config Reset program configuration (repos etc.)." + Environment.NewLine +
" --repo=\"<path>\" Open a specified existing git repo upon start." + Environment.NewLine +
" --log Logs debug output to file." + Environment.NewLine);
ReturnCode = 0;
runGitForce = false;
}
// --version Show the application version number and quit
if (commandLine["version"] == "true")
{
Console.WriteLine("GitForce version " + ClassVersion.GetVersion());
ReturnCode = 0;
runGitForce = false;
}
// --reset-windows Reset stored locations and sizes of all windows and dialogs
if (commandLine["reset-windows"] == "true")
{
Properties.Settings.Default.WindowsGeometries = new StringCollection();
Properties.Settings.Default.Save();
Console.WriteLine("GitForce windows and dialogs geometries have been reset.");
ReturnCode = 0;
runGitForce = true;
}
// --reset-config Reset stored configuration items (repos, settings)
if (commandLine["reset-config"] == "true")
{
List<string> toWhack = new List<string>();
// This is very much dependent on the platform, load a list of directories to whack appropriately
if (ClassUtils.IsMono())
{
string home = Environment.GetEnvironmentVariable("HOME");
if (!string.IsNullOrEmpty(home))
{
toWhack.Add(Path.Combine(home, ".config/GitForce"));
toWhack.Add(Path.Combine(home, ".local/share/GitForce"));
}
}
else
{
toWhack.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GitForce"));
toWhack.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "GitForce"));
}
// Now that we have a list of directories to remove, delete them
foreach (var dir in toWhack)
{
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (ClassUtils.DeleteFolder(dirInfo, false, false) == false)
Console.Write("Unable to delete some files!");
}
Console.WriteLine("Configuration has been reset.");
ReturnCode = 0;
runGitForce = false;
}
// --log Create a log file and append all debug log messages to it
if (commandLine["log"] == "true")
{
App.AppLog = Path.Combine(App.AppHome, "gitforce.log");
File.WriteAllText(App.AppLog, "Log created on " + DateTime.Now.ToShortDateString() + Environment.NewLine);
Console.WriteLine("Logging: " + App.AppLog);
}
// --passwd This is not a user option. It is used when the app is called to provide password echoed on a command line.
if (commandLine["passwd"] == "true")
{
ReturnCode = -1;
FormHttpsAuth httpsAuth = new FormHttpsAuth(false);
if (httpsAuth.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(httpsAuth.Password);
ReturnCode = 0;
}
runGitForce = false;
}
// --repo="<path>" Open an existing git repo at the specified path
if (commandLine["repo"] == "true") // --repo requires an additional argument
{
Console.WriteLine("Malformed arguments");
ReturnCode = -1;
runGitForce = false;
}
else if (commandLine["repo"] != null)
{
initRepo = commandLine["repo"];
// Substitute home folder for the token ~/
if (initRepo.StartsWith("~/"))
initRepo = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), initRepo.Substring(2));
initRepo = Path.GetFullPath(initRepo.TrimEnd(new[] { '/', '\\' })); // Further correct the path formatting if needed
string initGitRepo = Path.Combine(initRepo, ".git");
if (Directory.Exists(initGitRepo))
Console.WriteLine("Open git repo " + initRepo);
else
{
Console.WriteLine("The specified folder does not contain a valid git repo: " + initRepo);
ReturnCode = -1;
runGitForce = false;
}
}
// WAR: On Windows, detach the console when we are done. Mono does not need that to use Console class.
if (!ClassUtils.IsMono())
NativeMethods.FreeConsole();
return runGitForce;
}
}
}