Created
October 4, 2018 23:18
-
-
Save devhawk/f9a108dea723958b33d20223f50002d3 to your computer and use it in GitHub Desktop.
create-gist.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding()] | |
| Param( | |
| [Parameter(ValueFromPipelineByPropertyName)] $FullName, | |
| [string] $description, | |
| [switch] $public, | |
| [switch] $launchBrowser | |
| ) | |
| begin | |
| { | |
| if ( -not (test-path env:GITHUB_TOKEN)) | |
| { | |
| throw "Missing GITHUB_TOKEN" | |
| } | |
| $files = @{} | |
| } | |
| process | |
| { | |
| $filename = split-path $FullName -Leaf | |
| $content = get-content -path $FullName -Raw | |
| $files.Add($filename, @{"content" = "$content"}) | |
| } | |
| end | |
| { | |
| $body = ConvertTo-Json (@{"description" = $description; "public" = if ($public) {"true"} else {"false"} ; "files" = $files }) | |
| $headers = @{"Authorization" = "token $env:GITHUB_TOKEN"} | |
| $resp = Invoke-RestMethod -Uri "https://api.github.com/gists" -Method Post -Body $body -ContentType 'application/json' -Headers $headers | |
| $url = $resp.html_url | |
| Write-Output "New Gist URL: $url" | |
| if ($launchBrowser) | |
| { | |
| Start-Process $url | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment