From 918427398c210d8e1e8bc3df6c842b66a3a7ab4b Mon Sep 17 00:00:00 2001 From: Carsten Lohmann Date: Sun, 3 Dec 2023 15:11:04 +0000 Subject: [PATCH] Added the base for persisting folders --- classes/configuration/EnvConfiguration.cs | 4 + commands/environment/AddEnvCommand.cs | 134 +++++++++++++++++- helper/env/EnvRestoreHelper.cs | 2 +- .../config/sections/EnvironmentConfig.cs | 13 ++ 4 files changed, 151 insertions(+), 2 deletions(-) diff --git a/classes/configuration/EnvConfiguration.cs b/classes/configuration/EnvConfiguration.cs index 122f2c0..c4f458c 100644 --- a/classes/configuration/EnvConfiguration.cs +++ b/classes/configuration/EnvConfiguration.cs @@ -12,5 +12,9 @@ class EnvConfiguration private Dictionary> files = new(); public Dictionary> Files { get { return files; } set { files = value; }} + + private Dictionary>> folders = new(); + + public Dictionary>> Folders { get { return folders; } set { folders = value; }} } } \ No newline at end of file diff --git a/commands/environment/AddEnvCommand.cs b/commands/environment/AddEnvCommand.cs index 0525bf8..6550acb 100644 --- a/commands/environment/AddEnvCommand.cs +++ b/commands/environment/AddEnvCommand.cs @@ -20,7 +20,7 @@ public class Settings : CommandSettings public override int Execute(CommandContext context, Settings settings) { - var envTypes = new string[] {"Variable", "File"}; + var envTypes = new string[] {"Variable", "File", "Folder"}; var envType = AnsiConsole.Prompt( new SelectionPrompt() @@ -33,6 +33,8 @@ public override int Execute(CommandContext context, Settings settings) AddVariable(); } else if (envType == "File") { AddFile(); + } else if (envType == "Folder") { + AddFolder(); } return 0; @@ -41,6 +43,136 @@ public override int Execute(CommandContext context, Settings settings) [GeneratedRegex(@"^([a-z0-9-_]+)$")] private static partial Regex EnvNameMatchRegex(); + private void AddFolder() + { + bool finished = false; + + do { + var envFileShortname = string.Empty; + bool inputInvalid = false; + + do { + inputInvalid = false; + + envFileShortname = AnsiConsole.Ask("Shortname for the folder? (Will only be used as entry name in .gpt.yml.): "); + + if (!EnvNameMatchRegex().Match(envFileShortname).Success) { + AnsiConsole.MarkupLine("[red]The name should only consist of the following characters: a-z 0-9 - _[/]"); + inputInvalid = true; + } + + if (EnvironmentConfig.Folders.Keys.Contains(envFileShortname, StringComparer.OrdinalIgnoreCase)) { + if (!AnsiConsole.Confirm("An entry with that name already exists. Do you want to replace it?", false)) { + inputInvalid = true; + } + } + } while (inputInvalid); + + var envFolderPath = string.Empty; + string[] files = null; + + do { + inputInvalid = false; + + envFolderPath = AnsiConsole.Ask("Enter the full path to the folder:"); + + if (!Directory.Exists(envFolderPath)) { + AnsiConsole.MarkupLine("[red]The folder could not be found![/]"); + inputInvalid = true; + } + + files = Directory.GetFiles(envFolderPath, "*", SearchOption.AllDirectories); + + if (files.Length == 0) { + AnsiConsole.MarkupLine("[red]No files found in the folder.[/]"); + inputInvalid = true; + } + } while (inputInvalid); + + var saveLocations = new string[] {".gpt.yml", "gitpod"}; + + var saveLocation = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Where should the content of the file being saved?") + .PageSize(10) + .AddChoices(saveLocations) + ); + + var gpVariable = string.Empty; + + if (saveLocation == "gitpod") { + do { + inputInvalid = false; + + gpVariable = AnsiConsole.Ask("What is the name of the variable?"); + + if (!EnvNameMatchRegex().Match(gpVariable).Success) { + AnsiConsole.MarkupLine("[red]The variable name should only consist of the following characters: a-z 0-9 - _[/]"); + inputInvalid = true; + } + } while (inputInvalid); + } + + EnvironmentConfig.Folders.TryGetValue(envFileShortname, out Dictionary> existingEntry); + + foreach (string file in files) { + string encodedFileContent = string.Empty; + + try { + var fileContent = File.ReadAllText(file); + encodedFileContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContent)); + } catch (Exception e) { + AnsiConsole.WriteException(e); + } + + /*if (existingEntry != null) { + if (existingEntry.ContainsKey("file")) { + existingEntry["file"] = envFilePath; + } else { + existingEntry.Add("file", envFilePath); + } + + if (existingEntry.ContainsKey("content") && saveLocation == ".gpt.yml") { + existingEntry["content"] = encodedFileContent; + } else if (!existingEntry.ContainsKey("content") && saveLocation == ".gpt.yml") { + existingEntry.Add("content", encodedFileContent); + } else if (existingEntry.ContainsKey("content") && saveLocation != ".gpt.yml") { + existingEntry.Remove("content"); + } + + if (existingEntry.ContainsKey("var") && saveLocation != ".gpt.yml") { + existingEntry.Remove("var"); + } + } else { + var newEntry = new Dictionary() { + {"file", envFilePath} + }; + + if (saveLocation == ".gpt.yml") { + newEntry.Add("content", encodedFileContent); + } else { + newEntry.Add("var", gpVariable); + } + + EnvironmentConfig.Files.Add(envFileShortname, newEntry); + } + + if (saveLocation != ".gpt.yml") { + ExecCommand.Exec("gp env " + gpVariable + "=" + encodedFileContent, 10); + }*/ + } + + // Mark the config as updated, so it will be saved on exiting the application + EnvironmentConfig.ConfigUpdated = true; + + if (!AnsiConsole.Confirm("Do you want to add more files?", false)) { + finished = true; + } + } while (!finished); + + AnsiConsole.MarkupLine("[green]The changes have been saved.[/]"); + } + private void AddFile() { bool finished = false; diff --git a/helper/env/EnvRestoreHelper.cs b/helper/env/EnvRestoreHelper.cs index a55c1ea..fbf9540 100644 --- a/helper/env/EnvRestoreHelper.cs +++ b/helper/env/EnvRestoreHelper.cs @@ -48,7 +48,7 @@ public static void RestoreEnvironmentFiles(bool debug = false) entry.Value.TryGetValue("gpVariable", out string gpVariable); if (encodedFileContent == null && gpVariable != null) { - + encodedFileContent = Environment.GetEnvironmentVariable(gpVariable); } if (encodedFileContent != null && encodedFileContent != string.Empty) { diff --git a/helper/internal/config/sections/EnvironmentConfig.cs b/helper/internal/config/sections/EnvironmentConfig.cs index a62131f..04cfa82 100644 --- a/helper/internal/config/sections/EnvironmentConfig.cs +++ b/helper/internal/config/sections/EnvironmentConfig.cs @@ -29,5 +29,18 @@ public static Dictionary> Files appConfig.Env.Files = value; } } + + public static Dictionary>> Folders + { + get { + return appConfig.Env.Folders; + } + + set { + ConfigUpdated = true; + + appConfig.Env.Folders = value; + } + } } } \ No newline at end of file