-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpromote.ps1
86 lines (68 loc) · 2.68 KB
/
promote.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
### promotes a given stream to a different stream while modifying the published_at date to 'now'
param (
[string]$FromPath,
[string]$ToPath
)
# thanks to https://jonathancrozier.com/blog/formatting-json-with-proper-indentation-using-powershell
function Format-Json
{
<#
.SYNOPSIS
Applies proper formatting to a JSON string with the specified indentation.
.DESCRIPTION
The `Format-Json` function takes a JSON string as input and formats it with the specified level of indentation.
The function processes each line of the JSON string, adjusting the indentation level based on the structure of the JSON.
.PARAMETER Json
The JSON string to be formatted.
This parameter is mandatory and accepts input from the pipeline.
.PARAMETER Indentation
Specifies the number of spaces to use for each indentation level.
The value must be between 1 and 1024.
The default value is 2.
.EXAMPLE
$formattedJson = Get-Content -Path 'config.json' | Format-Json -Indentation 4
This example reads the JSON content from a file named 'config.json', formats it with an
indentation level of 4 spaces, and stores the result in the `$formattedJson` variable.
.EXAMPLE
@'
{
"EnableSSL": true,
"MaxThreads": 8,
"ConnectionStrings": {
"DefaultConnection": "Server=SERVER_NAME;Database=DATABASE_NAME;Trusted_Connection=True;"
}
}
'@ | Format-Json
This example formats an inline JSON string with the default indentation level of 2 spaces.
.NOTES
This function assumes that the input string is valid JSON.
#>
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[String]$Json,
[ValidateRange(1, 1024)]
[Int]$Indentation = 2
)
$lines = $Json -split '\n'
$indentLevel = 0
$result = $lines | ForEach-Object `
{
if ($_ -match "[\}\]]")
{
$indentLevel--
}
$line = (' ' * $indentLevel * $Indentation) + $_.TrimStart().Replace(": ", ": ")
if ($_ -match "[\{\[]")
{
$indentLevel++
}
return $line
}
return $result -join "`n"
}
# Load the JSON from the source file
$json = Get-Content -Path $FromPath | ConvertFrom-Json
# Change the 'published_at' field to the current date and time
$json.published_at = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
$json | ConvertTo-Json | Format-Json | Set-Content -Path $ToPath