Skip to content

Commit

Permalink
Adds JSON support (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhunt authored Jan 5, 2024
1 parent 73b50c7 commit 533dc75
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 184 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,7 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

*.sln

testResults.xml
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.2

- Adds `json` support

## v0.1

- [6c12e8d](https://github.com/cdhunt/potel/commit/6c12e8d6dcce2c44b1c70232268217794dd89696) Adds test for [httpunitPS](https://github.com/cdhunt/httpunitPS) configs and fixes related issues
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Import-ConfigData

Load configuration data from multiple file types.
The returned _Hashtable_ should have the same structure regardless of the source format.

Currently supported types:

- [PSD1](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_files?view=powershell-7.4)
- [YAML](https://yaml.org/)
- [JSON](https://www.json.org/)
- [TOML](https://toml.io/)

## CI Status

Expand Down
27 changes: 14 additions & 13 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,25 @@ function Publish {
function Docs {
param ()

Import-Module C:\source\Build-Docs\publish\Build-Docs\
Import-Module $publish -Force

$commands = Get-Command -Module $module
$HelpToMd = [System.IO.Path]::Combine($src, 'internal', 'Export-HelpToMd.ps1')
. $HelpToMd
$help = Get-HelpModuleData $module

@('# Import-ConfigData', [System.Environment]::NewLine) | Set-Content -Path "$docs/README.md"
$($manifest.Description) | Add-Content -Path "$docs/README.md"
@('## Cmdlets', [System.Environment]::NewLine) | Add-Content -Path "$docs/README.md"
# docs/README.md
$help | New-HelpDoc |
Add-ModuleProperty Name -H1 |
Add-ModuleProperty Description |
Add-HelpDocText "Commands" -H2 |
Add-ModuleCommands -AsLinks |
Out-HelpDoc -Path 'docs/README.md'

foreach ($command in $Commands | Sort-Object -Property Verb) {
# Individual Commands
foreach ($command in $help.Commands) {
$name = $command.Name
$docPath = Join-Path -Path $docs -ChildPath "$name.md"
$help = Get-Help -Name $name

Export-HelpToMd $help | Set-Content -Path $docPath

"- [$name]($name.md) $($help.Synopsis)" | Add-Content -Path "$docs/README.md"
$doc = New-HelpDoc -HelpModuleData $help
$doc.Text = $command.ToMD()
$doc | Out-HelpDoc -Path "docs/$name.md"
}

ChangeLog | Set-Content -Path "$parent/Changelog.md"
Expand Down
20 changes: 2 additions & 18 deletions docs/Import-ConfigData.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,45 @@
# Import-ConfigData


Load configuration data from multiple file types. The returned object should look the same regardless of the source format.
## Parameters

## Parameters

### Parameter Set 1


- `[String]` **Path** _Specifies a path to a configuration file with an extention of psd1, toml, yaml, or yml._ Mandatory, ValueFromPipeline

- `[String]` **Path** _Specifies a path to a configuration file with an extention of psd1, toml, json, yaml, or yml._ Mandatory, ValueFromPipeline

## Examples


### Example 1


Return an object representing the contents of a PowerShell Data File.


```powershell
$config = Import-ConfigData -Path config.psd1
$config.DriveName
data
```


### Example 2


Return an object representing the contents of a TOML File.


```powershell
$config = Import-ConfigData -Path config.toml
$config.DriveName
data
```


### Example 3


Return an object representing the contents of a YAML File.


```powershell
$config = Import-ConfigData -Path config.yaml
$config.DriveName
data
```


## Links


- [https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_files?view=powershell-7.4](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_files?view=powershell-7.4)
- [https://toml.io/](https://toml.io/)
- [https://yaml.org/](https://yaml.org/)
Expand Down
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Import-ConfigData


Load configuration data from multiple file types.
## Cmdlets

## Commands

- [Import-ConfigData](Import-ConfigData.md) _Load configuration data from multiple file types._

- [Import-ConfigData](Import-ConfigData.md) Load configuration data from multiple file types.
148 changes: 0 additions & 148 deletions src/internal/Export-HelpToMd.ps1

This file was deleted.

23 changes: 23 additions & 0 deletions src/private/ConvertFrom-PSCustomObject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function ConvertFrom-PSCustomObject ($Object) {

foreach ($item in $Object) {

$itemType = $item.GetType()

if ($itemType.Name -eq 'PSCustomObject') {
$keys = $item | Get-Member -MemberType NoteProperty

$hash = @{}
foreach ($key in $keys) {
$name = $key.Name
$value = @(ConvertFrom-PSCustomObject $item.$name)
$hash.Add($name, $value)
}
$hash | Write-Output
}

if ($itemType.IsValueType -or $itemType.Name -eq 'String') {
$item | Write-Output
}
}
}
27 changes: 27 additions & 0 deletions src/private/Import-JsonConfigData.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Import-JsonConfigData {
[CmdletBinding()]
param (
[Parameter(Mandatory,
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$Path
)

$content = Get-Content -Path $Path -Raw

switch ($PSVersionTable.PSVersion.Major) {
5 {
$psObject = $content | ConvertFrom-Json
ConvertFrom-PSCustomObject $psObject
break
}
Default {
$content | ConvertFrom-Json -AsHashtable
}
}

}
3 changes: 2 additions & 1 deletion src/public/Import-ConfigData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Import-ConfigData {
.DESCRIPTION
Load configuration data from multiple file types. The returned object should look the same regardless of the source format.
.PARAMETER Path
Specifies a path to a configuration file with an extention of psd1, toml, yaml, or yml.
Specifies a path to a configuration file with an extention of psd1, toml, json, yaml, or yml.
.LINK
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_data_files?view=powershell-7.4
.LINK
Expand Down Expand Up @@ -57,6 +57,7 @@ function Import-ConfigData {
'.toml' { Import-TomlConfigData -Path $Path; break }
'.yaml' { Import-YamlConfigData -Path $Path; break }
'.yml' { Import-YamlConfigData -Path $Path; break }
'.json' { Import-JsonConfigData -Path $Path; break }
}
}

Expand Down
Loading

0 comments on commit 533dc75

Please sign in to comment.