Skip to content

Commit

Permalink
Changes to allow import and export of yaml files in #4
Browse files Browse the repository at this point in the history
  • Loading branch information
gitfvb committed Nov 2, 2023
1 parent 423e1ba commit bf5b4f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions AptecoPSFramework/AptecoPSFramework.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ PrivateData = @{

# 'ReleaseNotes' des Moduls
ReleaseNotes = '
0.2.0 Added yaml as a new functionality to save and load settings - please make sure to install your dependencies again
0.1.6 Added a change for Hubspot to load associations into the properties
0.1.5 Fixed a problem in CleverReach when reading and parsing the headers from the csv
Improved the CleverReach logging when something aborts
Expand Down
1 change: 1 addition & 0 deletions AptecoPSFramework/bin/dependencies.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $psModules = [Array]@(
"EncryptCredential"
"ExtendFunction"
"ConvertUnixTimestamp"
"powershell-yaml"
#"Microsoft.PowerShell.Utility"
#"PSOAUth" # is defined in the local plugins where it is needed
)
Expand Down
18 changes: 15 additions & 3 deletions AptecoPSFramework/public/settings/export-settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ Function Export-Settings {
# Resolve path first
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)

# Work out the extension - the file does not need to exist for that
$pathExtension = [System.IO.Path]::GetExtension($Path)

If ( ( Test-Path -Path $absolutePath -IsValid ) -eq $true ) {

# TODO [x] Handle overwriting the file, currently it will be overwritten
If ( Test-Path -Path $absolutePath ) {
$backupPath = "$( $absolutePath ).$( [Datetime]::Now.ToString("yyyyMMddHHmmss") )"
Write-Verbose -message "Moving previous settings file '$( $absolutePath )' to $( $backupPath )" -Verbose
Write-Verbose -message "Moving previous settings file '$( $absolutePath )' to $( $backupPath )"
Move-Item -Path $absolutePath -Destination $backupPath -Verbose
}

# Now save the settings file
ConvertTo-Json -InputObject $script:settings -Depth 99 | Set-Content -Path $absolutePath -Encoding utf8 -Verbose
Switch ( $pathExtension ) {

{ $PSItem -in @( ".yml", ".yaml" ) } {
ConvertTo-Yaml $script:settings -OutFile $absolutePath #-KeepArray
}

default {
ConvertTo-Json -InputObject $script:settings -Depth 99 | Set-Content -Path $absolutePath -Encoding utf8
}

}

# Resolve the path now to an absolute path
$resolvedPath = Resolve-Path -Path $absolutePath


} else {

Write-Error -Message "The path '$( $Path )' is invalid."
Expand Down
18 changes: 17 additions & 1 deletion AptecoPSFramework/public/settings/import-settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Function Import-Settings {
# Try to resolve the path
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)

# Work out the extension - the file does not need to exist for that
$pathExtension = [System.IO.Path]::GetExtension($Path)

try {

If ( ( Test-Path -Path $absolutePath -IsValid ) -eq $true ) {
Expand All @@ -17,7 +20,20 @@ Function Import-Settings {

# Load the new settings file
try {
$settings = Get-Content -Path $absolutePath -Encoding utf8 -Raw | ConvertFrom-Json

# Now save the settings file
Switch ( $pathExtension ) {

{ $PSItem -in @( ".yml", ".yaml" ) } {
$settings = [PSCustomObject]( Get-Content -Path $absolutePath -Encoding utf8 -Raw | ConvertFrom-Yaml )
}

default {
$settings = Get-Content -Path $absolutePath -Encoding utf8 -Raw | ConvertFrom-Json
}

}

} catch {
Write-Error "There is a problem loading the settings file"
}
Expand Down

0 comments on commit bf5b4f3

Please sign in to comment.