Functions/GenXdev.Data.KeyValueStore/Remove-KeyFromStore.cs

// ################################################################################
// Part of PowerShell module : GenXdev.Data.KeyValueStore
// Original cmdlet filename : Remove-KeyFromStore.cs
// Original author : René Vaessen / GenXdev
// Version : 3.28.2026
// ################################################################################
// Copyright (c) 2026 René Vaessen / GenXdev
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ################################################################################



using System.Management.Automation;

namespace GenXdev.Data.KeyValueStore
{
    [System.ComponentModel.Description(@"
.SYNOPSIS
Removes a key from a local key-value store.
.DESCRIPTION
This function marks a specified key as deleted in a named local key-value
store. The key's value is preserved with a deletion timestamp for audit
purposes.
.EXAMPLE
```powershell
Remove-KeyFromStore -StoreName ""MyStore"" -KeyName ""MyKey""
```

Remove the key ""MyKey"" from the store ""MyStore"".

.EXAMPLE
```powershell
removekey ""MyStore"" ""MyKey""
```

Remove a key using the alias.
")]
    [Cmdlet(VerbsCommon.Remove, "KeyFromStore", SupportsShouldProcess = true)]
    [Alias("removekey")]
    public class RemoveKeyFromStoreCommand : PSGenXdevCmdlet
    {
        /// <summary>
        /// Name of the store
        /// </summary>
        [Parameter(
            Mandatory = true,
            Position = 0,
            HelpMessage = "Name of the store")]
        public string StoreName { get; set; }

        /// <summary>
        /// Key to be deleted
        /// </summary>
        [Parameter(
            Mandatory = true,
            Position = 1,
            HelpMessage = "Key to be deleted")]
        public string KeyName { get; set; }

        /// <summary>
        /// Database path for key-value store data files
        /// </summary>
        [Parameter(
            Mandatory = false,
            HelpMessage = "Database path for key-value store data files")]
        public string DatabasePath { get; set; }

        // Private fields for processing
        private string lastModifiedBy;

        /// <summary>
        /// Begin processing - initialization logic
        /// </summary>
        protected override void BeginProcessing()
        {
            WriteVerbose($"Preparing to remove key '{KeyName}' from store '{StoreName}'");

            // Get current user info for audit trail
            string computerName = Environment.GetEnvironmentVariable("COMPUTERNAME");
            string userName = Environment.GetEnvironmentVariable("USERNAME");
            lastModifiedBy = $"{computerName}\\{userName}";
        }

        /// <summary>
        /// Process record - main cmdlet logic
        /// </summary>
        protected override void ProcessRecord()
        {
            // Check if user wants to proceed with deletion
            if (ShouldProcess(
                $"Key '{KeyName}' in store '{StoreName}'",
                "Mark as deleted"))
            {
                RemoveKeyFromStore(StoreName, KeyName, DatabasePath);
            }
        } /// <summary>
                 /// End processing - cleanup logic
                 /// </summary>
        protected override void EndProcessing()
        {
        }
    }
}