Public/Settings/New-EdenSettings.ps1

function New-EdenSettings
{
    <#
    .SYNOPSIS
        Creates a new local settings file from a global settings file.
    .DESCRIPTION
        Use this command to create a new local json settings file based
        on the global settings file that matches the name you provide.
        The local settings file is created in the './Eden/Settings/.local/'
        folder. The global settings file is searched for in the
        './Eden/Settings/' folder.

        If no name is provided for the global settings, the Eden framework
        will use the same name as the local settings file.

        If the Eden framework can not find the global settings file, it
        will use the Default global settings file 'Default.json'.

        If no name is provided for the local settings, the Eden framework
        will use 'Default' as the value of the name.

        Use -Force to overwrite an existing local settings file.
    .EXAMPLE
        PS> New-EdenSettings -LocalSettingsName MySettings -GlobalSettingsName MyGlobalSettings
        or
        PS> e-stn -lsn MySettings -gsn MyGlobalSettings
        Creates a new local settings file: './Eden/Settings/.local/MySettings.json'
        by using the global settings file: './Eden/Settings/MyGlobalSettings.json'.
    .EXAMPLE
        PS> New-EdenSettings -LocalSettingsName MySettings
        or
        PS> e-stn -lsn MySettings
        Creates a new local settings file: './Eden/Settings/.local/MySettings.json'
        by using the global settings file: './Eden/Settings/MySettings.json'.
        If the global settings file does not exist it uses the default file:
        './Eden/Settings/Default.json'.
    .EXAMPLE
        PS> New-EdenSettings
        or
        PS> e-stn
        Creates a new local settings file: './Eden/Settings/.local/Default.json'
        by using the global settings file: './Eden/Settings/Default.json'.
        If the global settings file does not exist it generates one with two settings:
        '{
            "SolutionName": <Name of folder above current parent folder>
            "ServiceName": <Name of parent folder>
        }'
    #>

    [CmdletBinding()]
    param(
        # The name of the local settings file to create.
        [Alias("lsn")]
        [String] $LocalSettingsName = "Default",
        # The name of the global settings file to use. If left blank the system
        # will use the value provided for the local settings name. If a file for
        # that name does not exist, it will use the default settings file 'Default.json'
        [Alias("gsn")]
        [String] $GlobalSettingsName,
        # Forces the system to overwrite an existing local settings file.
        [Alias("f")]
        [Switch] $Force
    )

    try {

        if (!$SolutionName) {
            $SolutionName = Get-SolutionName
        }
        if (!$ServiceName) {
            $ServiceName = Get-ServiceName
        }

        $loggingPrefix = "$SolutionName $ServiceName Settings $LocalSettingsName"

        if ([String]::IsNullOrEmpty($LocalSettingsName)) {
            $LocalSettingsName = "Default"
        }

        if ([String]::IsNullOrEmpty($GlobalSettingsName)) {
            $GlobalSettingsName = $LocalSettingsName
        }

        $globalSettingsPath = "./Eden/Settings/$GlobalSettingsName.json"
        $localSettingsPath = "./Eden/Settings/.local/$LocalSettingsName.json"

        if (!(Test-Path $globalSettingsPath) -and (Test-Path "./Eden/Settings/Default.json")) {
            Write-EdenInfo "Using the Default global settings: './Eden/Settings/Default.json'" $loggingPrefix
            Write-EdenInfo "A global settings file for the name specified does not exist:" $loggingPrefix
            Write-EdenInfo "'$globalSettingsPath'" $loggingPrefix
            $globalSettingsPath = "./Eden/Settings/Default.json"
        }

        if(!(Test-Path $globalSettingsPath)) {
            Write-EdenError "The global settings file that can be used to create the settings is missing." $loggingPrefix
            Write-EdenError "Please do the following:" $loggingPrefix
            Write-EdenError "Create a single level json file to define all settings for the service: " $loggingPrefix
            Write-EdenError "'$globalSettingsPath'" $loggingPrefix
            Write-EdenError "Include 'Prompt' as the value for the setting if the setting needs to " $loggingPrefix
            Write-EdenError "be provided specifically for the local environment." $loggingPrefix
            Write-EdenError "Include 'SecurePrompt' for settings that need to be provided for the " $LoggingPrefix
            Write-EdenError "local environment and should be encrypted." $loggingPrefix
            throw "Missing global settings file '$globalSettingsPath'."
        } else {
            if ((Test-Path $localSettingsPath) -and !$Force) {
                Write-EdenError "The settings file already exists: '$localSettingsPath'" $loggingPrefix
                Write-EdenError "Use the -Force (-f) flag to overwrite this file." $loggingPrefix
                throw "Local settings file '$localSettingsPath' already exists."
                return
            }
        }

        [String]$json = Get-Content $globalSettingsPath
        $jsonObject = ConvertFrom-Json $json

        Write-EdenInfo "Loaded global settings file: $globalSettingsPath" $loggingPrefix

        $jsonObject.PSObject.Properties | ForEach-Object {
            Write-Host "$($_.Name): $($_.Value)"
        }

        Write-EdenInfo "Prompting for local values." $loggingPrefix

        $jsonObject.PSObject.Properties | ForEach-Object {
            if ($_.Value.StartsWith("Prompt")) {
                if ($_.Value.Length -gt 7) {
                    Write-Host $_.Value.Substring(7, $_.Value.Length - 7)
                }
                $value = Read-Host -Prompt "Please provide the $($_.Name) setting"
                $jsonObject.$($_.Name) = $value
            }
            if ($_.Value.StartsWith("SecurePrompt")) {
                if ($_.Value.Length -gt 13) {
                    Write-Host $_.Value.Substring(7, $_.Value.Length - 13)
                }
                $value = Read-Host -AsSecureString -Prompt "Please provide the $($_.Name) setting (this value will be stored as an encrypted secure string)"
                $jsonObject.$($_.Name) = (ConvertFrom-SecureString -SecureString $value)
            }
        }

        Write-EdenInfo "Final settings values." $loggingPrefix

        $jsonObject.PSObject.Properties | ForEach-Object {
            Write-Host "$($_.Name): $($_.Value)"
        }

        if (!(Test-Path "./Eden/Settings/.local")) {
            New-Item -ItemType Directory "./Eden/Settings/.local"
        }

        Write-EdenInfo "Saving settings values to: '$localSettingsPath'" $loggingPrefix
        $json = ConvertTo-Json $jsonObject
        $json | Out-File -FilePath $localSettingsPath -Force
    }
    catch {
        Write-EdenError "There was an error saving the Eden service settings: " $loggingPrefix
        Write-EdenError "'$($_.Exception.Message)'" $loggingPrefix
    }
}
New-Alias `
    -Name e-stn `
    -Value New-EdenSettings `
    -Force