-
Notifications
You must be signed in to change notification settings - Fork 8
/
Send-Gist.ps1
72 lines (55 loc) · 1.75 KB
/
Send-Gist.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
function Send-Gist {
param([string]$Path, [string]$Description)
if($Path) {
if (Test-Path -Path $Path) {
$fileName = Split-Path -Path $Path -Leaf
$contents = Get-Content -Path $Path -Raw
}
else {
Write-Warning "$($Path) not found"
break
}
}
else {
if($psISE) {
$fileName = Split-Path -Leaf $psISE.CurrentFile.FullPath
if($psISE.CurrentFile.Editor.SelectedText) {
$contents = $psISE.CurrentFile.Editor.SelectedText
}
else {
$contents = $psISE.CurrentFile.Editor.Text
}
}
else {
Write-Warning 'Using this function without the Path parameter only works in PowerShell ISE'
break
}
}
if($Description) {
$gistDescription = $Description
}
else {
$gistDescription = "Description for $($fileName)"
}
$gist = @{
'description' = $gistDescription
'public' = $true
'files' = @{
"$($fileName)" = @{
'content' = "$($contents)"
}
}
}
$Header = Get-GistAuthHeader
$BaseUri = $Uri = 'https://api.github.com/gists'
$Method = 'POST'
$targetGist = Get-Gist $Global:GitHubCred.UserName $fileName
if($targetGist) {
$r = [System.Windows.MessageBox]::Show('Gist already exists. Do you want to overwrite?', 'Confirmation', 'YesNo', 'Question')
if($r -eq 'no') {return}
$Uri = $BaseUri + "/$($targetGist.GistID)"
$Method = 'Patch'
}
$resp = Invoke-RestMethod -Uri $Uri -Method $Method -Headers $Header -Body ($gist | ConvertTo-Json)
Start-Process $resp.'html_url'
}