Skip to content

Commit 78bb0c6

Browse files
committed
Add -PassThru to Get-BloggerPost #26
1 parent 86e889a commit 78bb0c6

3 files changed

Lines changed: 80 additions & 17 deletions

File tree

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg
5555
1. Fetch an individual post from your blog
5656

5757
```
58-
Get-BloggerPost -PostId <postid>
58+
$post = Get-BloggerPost -PostId <postid>
5959
```
60-
61-
You can also persist the post to disk as HTML or markdown in the current directory.
62-
60+
61+
You can also persist the post to disk as HTML or markdown in the current directory.
62+
6363
When using `HTML` format, files are saved as `<postid>.html`
6464

6565
When using `Markdown` format, files are saved as `<title>.md`
@@ -84,6 +84,12 @@ A PowerShell library for publishing markdown files authored in markdown to Blogg
8484
Get-BloggerPost -PostId <postId> -OutDirectory ".\Blog" -FolderDateFormat "YYYY\\MM" -Format Markdown
8585
```
8686

87+
When persisting to disk, the post object is not returned unless `-PassThru` is specified.
88+
89+
```
90+
$post = Get-BloggerPost -PostId <postid> -Format Markdown -PassThru
91+
```
92+
8793
1. Publish a markdown file to your blog as draft
8894

8995
```

src/public/Get-BloggerPost.ps1

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@
1818
.PARAMETER OutDirectory
1919
The directory where the HTML file will be saved. If not specified, uses the current directory.
2020
21+
.PARAMETER PassThru
22+
If specified, the function will return the post object instead of just saving it to a file
23+
2124
.EXAMPLE
22-
Get-BloggerPost -PostId "1234567890123456789"
25+
# obtain a post from the blog defined in the user preferences
26+
$post = Get-BloggerPost -PostId "1234567890123456789"
2327
2428
.EXAMPLE
29+
# obtain a post from a specified blog and save it as HTML in a specific directory
2530
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format HTML -OutDirectory "C:\temp"
2631
2732
.EXAMPLE
33+
# obtain a post from a specified blog and save it as Markdown in a specific directory with a date-based folder structure
2834
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -DateFormat "YYYY\\MM" -OutDirectory "C:\blogposts"
35+
36+
.EXAMPLE
37+
# obtain a post from a specified blog and save it as JSON in the current directory
38+
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format JSON
39+
40+
.EXAMPLE
41+
# obtain a post from a specified blog, write it to disk and return the post object
42+
$post = Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -PassThru
2943
#>
3044
Function Get-BloggerPost {
3145
[CmdletBinding()]
@@ -46,7 +60,10 @@ Function Get-BloggerPost {
4660
[string]$FolderDateFormat,
4761

4862
[Parameter(ParameterSetName = "Persist")]
49-
[string]$OutDirectory = (Get-Location).Path
63+
[string]$OutDirectory = (Get-Location).Path,
64+
65+
[Parameter(ParameterSetName = "Persist")]
66+
[switch]$PassThru
5067
)
5168

5269
if (!$PSBoundParameters.ContainsKey("BlogId")) {
@@ -56,10 +73,6 @@ Function Get-BloggerPost {
5673
}
5774
}
5875

59-
if ([string]::IsNullOrEmpty($PostId)) {
60-
throw "PostId is required."
61-
}
62-
6376
try {
6477
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts/$PostId"
6578

@@ -144,7 +157,10 @@ Function Get-BloggerPost {
144157
}
145158

146159
# Return the post object for further processing if needed
147-
return $result
160+
if (!($PSCmdlet.ParameterSetName -eq "Persist") -or ($PassThru.IsPresent -and $PassThru)) {
161+
Write-Verbose "Returning blog post object"
162+
return $result
163+
}
148164
}
149165
catch {
150166
throw "Failed to save post content: $($_.Exception.Message)"

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ Describe "Get-BloggerPost" {
4040

4141
# setup blog post retrieval
4242
Mock Invoke-GAPi {
43-
return [pscustomobject]@{ content = "<html>Test content</html>" }
43+
return [pscustomobject]@{ content = "<p>Test content</p>" }
4444
}
4545
}
4646

4747
# act
48-
$result = Get-BloggerPost -PostId "123" -Format HTML -OutDirectory $OutDirectory
48+
Get-BloggerPost -PostId "123" -Format HTML -OutDirectory $OutDirectory
4949

5050
# assert
51-
$result | Should -Not -BeNullOrEmpty
52-
Test-Path -Path (Join-Path -Path $OutDirectory -ChildPath "123.html") | Should -BeTrue
51+
$outputFile = Join-Path -Path $OutDirectory -ChildPath "123.html"
52+
Test-Path -Path $outputFile | Should -BeTrue
5353
}
5454
}
5555

@@ -64,7 +64,7 @@ Describe "Get-BloggerPost" {
6464
It "Should call correct API endpoint" {
6565
InModuleScope PSBlogger {
6666
Mock Invoke-GApi {
67-
return [pscustomobject]@{ content = "<html>Test content</html>" }
67+
return [pscustomobject]@{ content = "<p>Test content</p>" }
6868
} -ParameterFilter {
6969
$uri -eq "https://www.googleapis.com/blogger/v3/blogs/test-blog-id/posts/123"
7070
} -Verifiable
@@ -131,7 +131,7 @@ Describe "Get-BloggerPost" {
131131

132132
# mock post retrieval
133133
Mock Invoke-GApi {
134-
return @{ content = "<html>Test content</html>" }
134+
return @{ content = "<p>Test content</p>" }
135135
}
136136
}
137137
}
@@ -367,4 +367,45 @@ Describe "Get-BloggerPost" {
367367
Test-Path "TestDrive:\123.html" | Should -BeTrue
368368
}
369369
}
370+
371+
Context "PassThru" {
372+
373+
BeforeEach {
374+
InModuleScope PSBlogger {
375+
# Mock the session to return a test blog ID
376+
$BloggerSession.BlogId = "test-blog-id"
377+
378+
# mock post retrieval
379+
Mock Invoke-GApi {
380+
return @{ content = "<p>Test content</p>" }
381+
}
382+
}
383+
}
384+
385+
It "Should return post object when not persisting output to disk" {
386+
# act
387+
$result = Get-BloggerPost -PostId "123"
388+
389+
# assert
390+
$result | Should -Not -BeNullOrEmpty
391+
}
392+
393+
It "Should not return a post object when persisting to disk" {
394+
# act
395+
$result = Get-BloggerPost -PostId "123" -Format HTML -OutDirectory TestDrive:\
396+
397+
# assert
398+
$result | Should -BeNullOrEmpty
399+
}
400+
401+
It "Should return post object when persisting to disk with PassThru specified" {
402+
# act
403+
$result = Get-BloggerPost -PostId "123" -Format HTML -OutDirectory TestDrive:\ -PassThru
404+
405+
# assert
406+
$result | Should -Not -BeNullOrEmpty
407+
}
408+
409+
410+
}
370411
}

0 commit comments

Comments
 (0)