From 08619865639479523f6579e51d78d8165208135f Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Sat, 27 Jan 2024 12:25:00 -0600 Subject: [PATCH] clean up some command line parsing --- git-anchor/Actions/Base.cs | 5 ----- git-anchor/Actions/Create.cs | 21 ++++++++++++++++++--- git-anchor/Actions/Pull.cs | 6 ++++-- git-anchor/Lib/ProgressReporter.cs | 12 +++++++++++- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/git-anchor/Actions/Base.cs b/git-anchor/Actions/Base.cs index d59b87a..f07e3c4 100644 --- a/git-anchor/Actions/Base.cs +++ b/git-anchor/Actions/Base.cs @@ -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 Callable(); - public abstract class BaseAction { public virtual void Run() { diff --git a/git-anchor/Actions/Create.cs b/git-anchor/Actions/Create.cs index 99a0104..1ce6de9 100644 --- a/git-anchor/Actions/Create.cs +++ b/git-anchor/Actions/Create.cs @@ -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 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()); diff --git a/git-anchor/Actions/Pull.cs b/git-anchor/Actions/Pull.cs index 1813ad9..6dfb7bf 100644 --- a/git-anchor/Actions/Pull.cs +++ b/git-anchor/Actions/Pull.cs @@ -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()); diff --git a/git-anchor/Lib/ProgressReporter.cs b/git-anchor/Lib/ProgressReporter.cs index 17558a4..8552667 100644 --- a/git-anchor/Lib/ProgressReporter.cs +++ b/git-anchor/Lib/ProgressReporter.cs @@ -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;