-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathNewGistNotebook.ps1
47 lines (47 loc) · 1.91 KB
/
NewGistNotebook.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
function New-GistNotebook {
param(
[Parameter(ParameterSetName='File',ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
$Path,
[Parameter(Mandatory,ParameterSetName='TwoStrings',Position=0)]
$Contents,
[Parameter(ParameterSetName='File',ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory,ParameterSetName='TwoStrings',Position=1)]
[alias('Name')]
$FileName,
$GistDescription = "PowerShell Notebook",
[switch]$Public,
[switch]$Show
)
begin {
if (Test-Path env:github_token) {$token = $env:github_token}
elseif ($PSVersionTable.Platform -like "win*") {
try {$token = & (Join-Path $PSScriptRoot 'Get-CredentialFromWindowsCredentialManager.ps1') -TargetName git:https://github.com -PlainTextPasswordOnly }
catch {throw "Could not read stored access token and env:github_token not set. You need to set it to a GitHub PAT"}
}
else { throw "env:github_token not set. You need to set it to a GitHub PAT"}
$params = @{
Method = 'Post'
Uri = 'https://api.github.com/gists'
Headers = @{"Authorization" = "token $token" }
}
}
process {
if ($PSBoundParameters.ContainsKey('Path')) {
$Contents = Get-content $Path -Encoding utf8
if (-not $FileName) {$FileName = Split-Path -Leaf $Path}
}
$gist = @{
'description' = $GistDescription
'public' = ($Public -as [bool])
'files' = @{
"$($FileName)" = @{
'content' = "$($Contents)"
}
}
}
$result = Invoke-RestMethod @params -Body ($gist | ConvertTo-Json -EscapeHandling EscapeNonAscii)
if ($Show) {Start-Process $result.html_url}
else {return $result.html_Url}
}
}