Last active
February 24, 2020 21:02
-
-
Save JustinGrote/0e0351f8d202b3af64bba0204476ec2c to your computer and use it in GitHub Desktop.
Github Git.IO Link Shortener Client
This file contains 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
function New-GitIoLink { | |
<# | |
.SYNOPSIS | |
Creates a git.io shortened link to a git file | |
.DESCRIPTION | |
Git.IO link shortener, useful for making short links to repositories or files for use in weblinks, twitter, or tools such as iex (iwr -useb $uri) for running snippets of code | |
*WARNING* Once you create a link, it becomes immutable, so be very careful and practice on non-essential links first! | |
.EXAMPLE | |
New-GitIoLink 'https://www.github.com/notarealsitehopefully' | |
Create a link with a random shortname | |
==SAMPLE OUTPUT== | |
Link Target | |
---- ------ | |
https://git.io/fjMxf https://www.github.com/notarealsitehopefully | |
.EXAMPLE | |
New-GitIoLink https://github.com/powershell/powershell/tree/master powershell | |
Create a named Git.io link | |
==SAMPLE OUTPUT== | |
Link Target | |
---- ------ | |
https://git.io/powershell https://github.com/powershell/powershell/tree/master | |
#> | |
[CmdletBinding()] | |
param ( | |
#The Github or Gist URI that you wish to link to. | |
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)][URI]$URI, | |
#An optional custom name for your link. https://git.io/<shortname> | |
[String]$ShortName | |
) | |
$Body = "url=$URI" | |
if ($ShortName) { $body += "&code=$ShortName" } | |
$result = Invoke-WebRequest -useb -erroraction stop -body $Body -method POST -uri 'https://git.io' | |
if ($Result -and $result.statuscode -eq 201) { | |
$Link = [uri]($result.headers.location | Select-Object -First 1) | |
if ($ShortName) { | |
$LinkShortName = $Link.segments[1] | |
if ($LinkShortName -ne $ShortName) { Write-Warning "You requested shortname '$shortname' but URI has already been shortened to '$LinkShortName'" } | |
} | |
[PSCustomObject]@{ | |
Link = $Link | |
Target = $result.Content | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment