Skip to content

Instantly share code, notes, and snippets.

@ryansobol
Last active May 5, 2019 04:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryansobol/7214372 to your computer and use it in GitHub Desktop.
Save ryansobol/7214372 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Shorten a GitHub URL to a git.io URL
# v1.2.0 - Copyright (c) 2016 Ryan Sobol - The MIT License
#
# Usage: gitio URL [code]
#
# Examples:
# $ gitio https://github.com/ryansobol ryansobol
# https://git.io/ryansobol
#
# $ gitio github.com/ryansobol ryansobol
# https://git.io/ryansobol
#
# $ gitio https://gist.github.com/ryansobol/7214372
# https://git.io/EgwCBg
#
# $ gitio gist.github.com/ryansobol/7214372
# https://git.io/EgwCBg
#
# $ gitio https://gist.githubusercontent.com/ryansobol/7214372/
# https://git.io/beeP
#
# $ gitio gist.githubusercontent.com/ryansobol/7214372/
# https://git.io/beeP
url = ARGV[0]
code = ARGV[1]
if url !~ /^(https?:\/\/)?(gist\.)?(github|githubusercontent).com/
abort <<-MSG
Shorten a GitHub URL to a git.io URL
v1.2.0 - Copyright (c) 2016 Ryan Sobol - The MIT License
Usage: gitio URL [code]
Examples:
$ gitio https://github.com/ryansobol ryansobol
https://git.io/ryansobol
$ gitio github.com/ryansobol ryansobol
https://git.io/ryansobol
$ gitio https://gist.github.com/ryansobol/7214372
https://git.io/EgwCBg
$ gitio gist.github.com/ryansobol/7214372
https://git.io/EgwCBg
$ gitio https://gist.githubusercontent.com/ryansobol/7214372/
https://git.io/beeP
$ gitio gist.githubusercontent.com/ryansobol/7214372/
https://git.io/beeP
MSG
end
if url !~ /^http/
url = "https://#{url}"
end
if code
code = "-F 'code=#{code}'"
end
output = `curl -i https://git.io -F 'url=#{url}' #{code} 2> /dev/null`
if output =~ /Location: (.+)\n?/
puts $1
else
puts output
end
# The MIT License (MIT)
#
# Copyright (c) 2016 Ryan Sobol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment