Skip to content

Commit

Permalink
Merge pull request #6 from russellds/master
Browse files Browse the repository at this point in the history
Updated to work with new version of GitHub API as well as work on both Powershell 2.0 and 4.0.
  • Loading branch information
sgeto authored Oct 21, 2017
2 parents 42e03b9 + 4fb0269 commit fdc364f
Showing 1 changed file with 85 additions and 18 deletions.
103 changes: 85 additions & 18 deletions PsGist.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
. (join-path $PSScriptRoot "/json 1.7.ps1")
$psInfo = Get-Host

if( $psInfo.Version.Major -eq 2 ) {

. (join-path $PSScriptRoot "/json 1.7.ps1")
}

function Get-Github-Credential($username) {
$host.ui.PromptForCredential("Github Credential", "Please enter your Github user name and password.", $username, "")
Expand Down Expand Up @@ -64,13 +69,33 @@ function New-DiffGist {
$files.Add($Name, $content)
}
END {
$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

if($netAssembly)
{
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")

$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())

if($instance)
{
$bindingFlags = "NonPublic","Instance"
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)

if($useUnsafeHeaderParsingField)
{
$useUnsafeHeaderParsingField.SetValue($instance, $true)
}
}
}

$apiurl = "https://api.github.com/gists"

$request = [Net.WebRequest]::Create($apiurl)

$credential = $(Get-Github-Credential $Username)

if($credential -eq $null) {
write-host "Github credentials are required."
return
Expand All @@ -84,22 +109,24 @@ function New-DiffGist {

$basiccredential = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([String]::Format("{0}:{1}", $username, $insecurepassword)))
$request.Headers.Add("Authorization", "Basic " + $basiccredential)


$request.UserAgent = "PsGist"

$request.ContentType = "application/json"
$request.Method = "POST"

$files.GetEnumerator() | % {
$singlefilejson = """" + $_.Name + """: {
""content"": """ + $_.Value + """
},"

$filesjson += $singlefilejson
}

$filesjson = $filesjson.TrimEnd(',')

$ispublic = $Public.ToString().ToLower()

$body = "{
""description"": """ + $Description + """,
""public"": $ispublic,
Expand All @@ -117,10 +144,10 @@ function New-DiffGist {
}
catch [System.Net.WebException] {
$_.Exception.Message | write-error

return
}

$responseStream = $response.GetResponseStream()
$reader = New-Object system.io.streamreader -ArgumentList $responseStream
$content = $reader.ReadToEnd()
Expand All @@ -132,10 +159,19 @@ function New-DiffGist {
return
}

$result = convertfrom-json $content -Type PSObject
$psInfo = Get-Host

if( $psInfo.Version.Major -eq 2 ) {

$result = convertfrom-json $content -Type PSObject
}
else {

$result = convertfrom-json $content
}

$url = $result.html_url

write-output $url

if ($Launch) {
Expand Down Expand Up @@ -218,13 +254,33 @@ function New-Gist {
$files.Add($filename, $content)
}
END {
$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

if($netAssembly)
{
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")

$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())

if($instance)
{
$bindingFlags = "NonPublic","Instance"
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)

if($useUnsafeHeaderParsingField)
{
$useUnsafeHeaderParsingField.SetValue($instance, $true)
}
}
}

$apiurl = "https://api.github.com/gists"

$request = [Net.WebRequest]::Create($apiurl)

$credential = $(Get-Github-Credential $Username)

if($credential -eq $null) {
write-host "Github credentials are required."
return
Expand All @@ -238,22 +294,24 @@ function New-Gist {

$basiccredential = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([String]::Format("{0}:{1}", $username, $insecurepassword)))
$request.Headers.Add("Authorization", "Basic " + $basiccredential)


$request.UserAgent = "PsGist"

$request.ContentType = "application/json"
$request.Method = "POST"

$files.GetEnumerator() | % {
$singlefilejson = """" + $_.Name + """: {
""content"": """ + $_.Value + """
},"

$filesjson += $singlefilejson
}

$filesjson = $filesjson.TrimEnd(',')

$ispublic = $Public.ToString().ToLower()

$body = "{
""description"": """ + $Description + """,
""public"": $ispublic,
Expand All @@ -271,10 +329,10 @@ function New-Gist {
}
catch [System.Net.WebException] {
$_.Exception.Message | write-error

return
}

$responseStream = $response.GetResponseStream()
$reader = New-Object system.io.streamreader -ArgumentList $responseStream
$content = $reader.ReadToEnd()
Expand All @@ -285,11 +343,20 @@ function New-Gist {

return
}

$psInfo = Get-Host

$result = convertfrom-json $content -Type PSObject
if( $psInfo.Version.Major -eq 2 ) {

$result = convertfrom-json $content -Type PSObject
}
else {

$result = convertfrom-json $content
}

$url = $result.html_url

write-output $url
}
}
Expand All @@ -299,4 +366,4 @@ new-alias Create-Gist New-Gist # For those who saw my post when I used create...
new-alias diffgist New-DiffGist

export-modulemember -alias * -function New-Gist
export-modulemember -alias * -function New-DiffGist
export-modulemember -alias * -function New-DiffGist

0 comments on commit fdc364f

Please sign in to comment.