From b33adad7600de0589c07f5549026661875846216 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Sun, 21 Jan 2024 16:20:12 -0600 Subject: [PATCH] add some logic for file system interactions --- repo-backup-util/Program.cs | 16 ++++- repo-backup-util/lib/Volumes.cs | 123 +++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/repo-backup-util/Program.cs b/repo-backup-util/Program.cs index 466aa16..39ae356 100644 --- a/repo-backup-util/Program.cs +++ b/repo-backup-util/Program.cs @@ -27,4 +27,18 @@ static async Task Main() var cloneUrls = uniqueRepos.Select(r => r.CloneUrl); } -await Main(); \ No newline at end of file +static void DriveStuff() +{ + IEnumerable volumes = Volumes.GetVolumes(); + DriveInfo? selection = Volumes.SelectFromList(volumes); + if (selection == null) + { + Console.WriteLine("No selection found"); + return; + } + + var backupDir = Volumes.CreateBackupDirectory(selection); +} + +DriveStuff(); +//await Main(); \ No newline at end of file diff --git a/repo-backup-util/lib/Volumes.cs b/repo-backup-util/lib/Volumes.cs index 6f59b67..5a015ab 100644 --- a/repo-backup-util/lib/Volumes.cs +++ b/repo-backup-util/lib/Volumes.cs @@ -1,10 +1,129 @@ -namespace RepoBackupUtil.Lib +using System.Collections; + +namespace RepoBackupUtil.Lib { public static class Volumes { public static IEnumerable GetVolumes() { - return DriveInfo.GetDrives(); + try + { + return DriveInfo.GetDrives(); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return []; + } + } + + private static int ReadInput() + { + string? input = Console.ReadLine(); + + while (input == null) + { + Console.WriteLine("Please enter a number: "); + input = Console.ReadLine(); + } + + try + { + return int.Parse(input); + } + catch + { + Console.WriteLine("Invaid input. Please enter a number: "); + return ReadInput(); + } + } + + private static DriveInfo? GetSelection(Hashtable options) + { + int inputAsInt = ReadInput(); + + if (!options.ContainsKey(inputAsInt)) + { + return null; + } + + foreach (DictionaryEntry entry in options) + { + if (entry.Key.Equals(inputAsInt)) + { + Console.WriteLine($"value: {entry.Value}"); + Console.WriteLine($"type: {entry.Value?.GetType()}"); + return entry.Value != null ? (DriveInfo)entry.Value : null; + } + } + + return null; + } + + public static DriveInfo? SelectFromList(IEnumerable volumes) + { + int i = 0; + Hashtable options = []; + + // prepare table and present options to user + Console.WriteLine("Select the drive you want to backup to (enter a number): \n"); + foreach (DriveInfo volume in volumes) + { + ++i; + string option = $"{i}: {volume.Name} ({GetSizeInGB(volume)}GB available)"; + Console.WriteLine(option); + options.Add(i, volume); + } + + try + { + // parse user input and return appropiate drive info + return GetSelection(options); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return null; + } + } + + public static double GetSizeInGB(DriveInfo volume) => volume.TotalSize / 1024 / 1024 / 1024; + + public static DirectoryInfo? CreateBackupDirectory(DriveInfo volume) + { + bool isSystemDrive = volume.DriveType == DriveType.Fixed; + + try + { + + if (isSystemDrive) + { + Console.WriteLine("Using system drive. Directing you to user folder..."); + + string? userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + DirectoryInfo userDir = new(userPath); + + Console.WriteLine($"Checking for folder `.ghbackups` at `{userDir.FullName}`..."); + DirectoryInfo? backupDir = userDir.CreateSubdirectory(".ghbackups"); + + return backupDir; + } + else + { + Console.WriteLine($"Using {volume.Name}. Directing you to root folder..."); + + DirectoryInfo? rootDir = volume.RootDirectory; + Console.WriteLine($"Checking for folder `.ghbackups` at `{rootDir.FullName}`..."); + DirectoryInfo? backupDir = rootDir.CreateSubdirectory(".ghbackups"); + + return backupDir; + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return null; + } } } }