LoginSignup
18
17

More than 1 year has passed since last update.

GitHub の URL 短縮サービス git.io 用のシェルスクリプト

Last updated at Posted at 2016-02-03

GitHub の URL も長くなったりするので、何度も参照するものは短縮 URL にすると都合が良いのですが、実は GitHub 自身が git.io という短縮 URL サービスを提供しています。

上記ページのサンプルと読むと、使い方はいたってシンプル。

ブラウザからだと短縮 URL の後ろに指定の文字を入れることができないのですが、API を叩くと可能なようです。

$ curl -i http://git.io -F "url=https://github.com/xtetsuji/p5-ModPerl-PSGI" -F "code=modperl-psgi"

これで https://github.com/xtetsuji/p5-ModPerl-PSGIhttps://git.io/modperl-psgi に短縮されます。

code= のパラメータが無ければ、URL はランダムに払い出されます。

せっかくなので、簡単に使うことができるシェルスクリプトを書いてみました。

#!/bin/bash

GITIO_URL=https://git.io

url="$1"
code="$2"

if [ -z "$url" ] ; then
    echo Usage:
    echo "  $0 URL [CODE]"
fi

if [ -n "$code" ] && grep -E '[^a-zA-Z0-9_-]' <<<"$code" >/dev/null 2>&1 ; then
    echo "code is only alpha-numeric and _ and -."
    exit
fi

echo "url = $url"
if [ -n "$code" ] ; then
    echo "code = $code"
fi

if [ -z "$code" ] ; then
    curl -i $GITIO_URL -F "url=$url"
else
    curl -i $GITIO_URL -F "url=$url" -F "code=$code"
fi

echo ""

文字制限などは、なんとなくアルファベットと数字とハイフンとアンダースコアのみだろうと勝手に入れています。

コマンドと出力の例は以下のような感じになります。

$ mkgitiourl https://gist.github.com/xtetsuji/1446584 maillog-hashnize 
url = https://gist.github.com/xtetsuji/1446584
code = maillog-hashnize
HTTP/1.1 100 Continue

HTTP/1.1 201 Created
Server: Cowboy
Connection: keep-alive
Date: Wed, 03 Feb 2016 11:10:48 GMT
Status: 201 Created
Content-Type: text/html;charset=utf-8
Location: https://git.io/vgtEH
Content-Length: 40
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.187053
X-Node: 982f2daf-c02c-439c-8e49-52eaa3e2cc1c
X-Revision: 4fdc60de6311e6a3aa31e19bc7b3aad7e85d33a6
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur

https://gist.github.com/xtetsuji/1446584
18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17