Skip to content

Commit

Permalink
Updated to work on Powershell 2 and 4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Slater committed Jul 29, 2014
1 parent 89729f0 commit 4fb0269
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 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 @@ -90,7 +95,7 @@ function New-DiffGist {
$request = [Net.WebRequest]::Create($apiurl)

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

if($credential -eq $null) {
write-host "Github credentials are required."
return
Expand All @@ -105,23 +110,23 @@ 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.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 @@ -139,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 @@ -154,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 @@ -240,7 +254,7 @@ function New-Gist {
$files.Add($filename, $content)
}
END {
$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])
$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

if($netAssembly)
{
Expand All @@ -260,13 +274,13 @@ function New-Gist {
}
}
}

$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 @@ -280,47 +294,45 @@ 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.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,
""files"": {" + $filesjson + "}
}"

$bytes = [text.encoding]::Default.getbytes($body)
$request.ContentLength = $bytes.Length

$stream = $request.GetRequestStream()
$stream.Write($bytes, 0, $bytes.Length)
$stream = [io.stream]$request.GetRequestStream()
$stream.Write($bytes,0,$bytes.Length)

try {
$response = $request.GetResponse()
}
catch [System.Net.WebException] {
$_.Exception.Message | write-error

return
}

$response

<#
$responseStream = $response.GetResponseStream()
$reader = New-Object system.io.streamreader -ArgumentList $responseStream
$content = $reader.ReadToEnd()
Expand All @@ -331,13 +343,21 @@ 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 @@ -346,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 4fb0269

Please sign in to comment.