From f9bd792f381c36214e4a40383f6f75ec064ac01c Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 31 Jan 2024 15:56:02 +0200 Subject: [PATCH] - added a delete files using search pattern addin to the file plugin --- .../Nox.Cli.Plugin.File/FileDeleteFiles_v1.cs | 81 +++++++++++++++++++ src/Nox.Cli/Properties/launchSettings.json | 2 +- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileDeleteFiles_v1.cs diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileDeleteFiles_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileDeleteFiles_v1.cs new file mode 100644 index 0000000..e7967a5 --- /dev/null +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileDeleteFiles_v1.cs @@ -0,0 +1,81 @@ +using Nox.Cli.Abstractions; +using Nox.Cli.Abstractions.Extensions; + +namespace Nox.Cli.Plugin.File; + +public class FileDeleteFiles_v1 : INoxCliAddin +{ + public NoxActionMetaData Discover() + { + return new NoxActionMetaData + { + Name = "file/delete-files@v1", + Author = "Jan Schutte", + Description = "Delete files in a folder using a search pattern.", + + Inputs = + { + ["folder"] = new NoxActionInput { + Id = "folder", + Description = "The folder in which to delete the files", + Default = string.Empty, + IsRequired = true + }, + + ["search-pattern"] = new NoxActionInput { + Id = "search-pattern", + Description = "The search-pattern to use to determine which files to delete", + Default = string.Empty, + IsRequired = true + } + + } + }; + } + + private string? _folder; + private string? _searchPattern; + + public Task BeginAsync(IDictionary inputs) + { + _folder = inputs.Value("folder"); + _searchPattern = inputs.Value("search-pattern"); + return Task.CompletedTask; + } + + public Task> ProcessAsync(INoxWorkflowContext ctx) + { + var outputs = new Dictionary(); + + ctx.SetState(ActionState.Error); + + if (string.IsNullOrWhiteSpace(_folder) || + string.IsNullOrWhiteSpace(_searchPattern)) + { + ctx.SetErrorMessage("The File delete-files action was not initialized"); + } + else + { + try + { + var dir = new DirectoryInfo(_folder); + foreach (var file in dir.EnumerateFiles(_searchPattern)) + { + file.Delete(); + } + ctx.SetState(ActionState.Success); + } + catch (Exception ex) + { + ctx.SetErrorMessage(ex.Message); + } + } + + return Task.FromResult>(outputs); + } + + public Task EndAsync() + { + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/src/Nox.Cli/Properties/launchSettings.json b/src/Nox.Cli/Properties/launchSettings.json index 91b04f5..c969d51 100755 --- a/src/Nox.Cli/Properties/launchSettings.json +++ b/src/Nox.Cli/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "Nox.Cli": { "commandName": "Project", - "commandLineArgs": "test cmd", + "commandLineArgs": "sync helm-chart", "workingDirectory": "/home/jan/demo/CliDemo", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development"