Public/Initialize-PCSecretStore.ps1
|
function Initialize-PCSecretStore { <# .SYNOPSIS Creates the PowerCraft secrets store if it doesn't exist. .DESCRIPTION Creates ~/.powercraft/ directory and secrets.json with restrictive permissions. Safe to call multiple times — does nothing if already initialized. .PARAMETER Force Recreate secrets.json even if it already exists (resets to empty). .OUTPUTS [string] Path to the secrets file. .EXAMPLE Initialize-PCSecretStore # Returns: C:\Users\you\.powercraft\secrets.json #> [CmdletBinding(SupportsShouldProcess)] [OutputType([string])] param( [Parameter()] [switch]$Force ) $paths = Get-PCSecretsPath if (-not (Test-Path $paths.Home)) { if ($PSCmdlet.ShouldProcess($paths.Home, 'Create directory')) { New-Item -ItemType Directory -Path $paths.Home -Force | Out-Null Write-Verbose "Created PowerCraft home: $($paths.Home)" } } if ($Force -or -not (Test-Path $paths.File)) { if ($PSCmdlet.ShouldProcess($paths.File, 'Create secrets file')) { @{} | ConvertTo-Json | Set-Content $paths.File -Encoding UTF8 Set-PCFilePermissions -Path $paths.File Write-Verbose "Created secrets file: $($paths.File)" } } return $paths.File } |