clean up some command line parsing

This commit is contained in:
2024-01-27 12:25:00 -06:00
parent ed03194471
commit 0861986563
4 changed files with 33 additions and 11 deletions

View File

@@ -1,11 +1,6 @@
using System.Diagnostics;
using GitAnchor.Lib;
namespace GitAnchor.Actions;
/** represent a function type matching either of the below class members */
// public delegate Task<ActivityStatusCode> Callable();
public abstract class BaseAction {
public virtual void Run()
{

View File

@@ -9,13 +9,18 @@ public class Create : BaseAction
{
string[] args = Environment.GetCommandLineArgs();
string? backupName = args.FirstOrDefault(arg => arg.StartsWith("--name") || arg.StartsWith("-n"));
string? token = args.FirstOrDefault(arg => arg.StartsWith("--token"));
string? backupName = args.FirstOrDefault(
arg => arg.StartsWith("--name") || arg.StartsWith("-n"))?
.Split("=")[1].Trim();
bool verbose = args.Any(arg => arg.StartsWith("--verbose")
|| arg.StartsWith("-v"));
// find or configure the default backup directory
DirectoryInfo backupDir = FileSystemTools.SetUpMainBackupFile() ?? throw new Exception("Encountered a problem creating main backup directory");
DirectoryInfo backupDir = FileSystemTools.SetUpMainBackupFile()
?? throw new Exception("Encountered a problem creating main backup directory");
// create backup file
DirectoryInfo? anchorDir = FileSystemTools.SetUpAnchor(backupDir, backupName);
@@ -46,6 +51,16 @@ public class Create : BaseAction
Console.WriteLine($"Found {privateRepos.Count()} private repos and {publicRepos.Count()} public repos.");
}
// get user confirmation to proceed
Console.WriteLine($"\n\nProceed to clone {repos.Count()} repositories? (y/n)");
string? response = Console.ReadLine();
if (response?.ToLower() != "y")
{
Console.WriteLine("Aborting...");
return ActivityStatusCode.Ok;
}
// set up, update directories for hosting projects
HashSet<Repository> newProjects = Volumes.PopulateBackupDirectories(uniqueRepos, anchorDir);
@@ -68,7 +83,7 @@ public class Create : BaseAction
Console.WriteLine($"Preparing to clone {taskPool.Count} repositories. Starting...");
// start a timer to track progress
ProgressReporter reporter = new();
using var reporter = new ProgressReporter();
reporter.Start();
Parallel.ForEach(taskPool, task => task.Start());

View File

@@ -18,9 +18,11 @@ namespace GitAnchor.Actions
// act on provided arguments
string? token = args.FirstOrDefault(arg => arg.StartsWith("--token"));
bool verbose = args.Any(arg => arg.StartsWith("--verbose") || arg.StartsWith("-v"));
string? anchorName = args.FirstOrDefault(arg => arg.StartsWith("--name") || arg.StartsWith("-n"))
string anchorName = args.FirstOrDefault(arg => arg.StartsWith("--name") || arg.StartsWith("-n"))?.Split("=")[1].Trim()
?? throw new Exception("No anchor name provided");
anchorName = anchorName.Split("=")[1].Trim();
// find (or create) the anchor in question
var anchor = Volumes.GetAnchor(mainBackupDir, anchorName) ?? FileSystemTools.SetUpAnchor(mainBackupDir, anchorName);
var projectDirectories = anchor?.GetDirectories();
@@ -53,7 +55,7 @@ namespace GitAnchor.Actions
}
// start progress tracker
ProgressReporter reporter = new();
using var reporter = new ProgressReporter();
reporter.Start();
Parallel.ForEach(taskPool, task => task.Start());

View File

@@ -2,14 +2,24 @@ using System.Diagnostics;
namespace GitAnchor.Lib;
public class ProgressReporter : Stopwatch
public class ProgressReporter : Stopwatch, IDisposable
{
public ProgressReporter()
{
// progress = new();
}
public void Dispose()
{
this.Stop();
// progress = null!;
GC.SuppressFinalize(this);
}
public override string ToString()
{
// progress.ProgressChanged += (sender, message) => Console.WriteLine(message);
var elapsed = this.ElapsedMilliseconds < 1000
? this.ElapsedMilliseconds
: this.ElapsedMilliseconds / 1000;