rcmdnk's blog
Last update

GW Github

GitHubのAPI を使ってコマンドラインからGitHubのレポジトリを作るスクリプト を書いてみました。

Sponsored Links

github-createrepo.sh

github-createrepo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash

# Parameters
user=""
pass=""
repo=""
description=""
twofac=""

# Help
HELP="Usage: $(basename $0) [-u <User>] [-p <Password>]
               [-t <Two-Factor code>]
               [-r <Repository>] [-d <Description>]

Make new repository in your GitHub account.
"

# Check arguments
OPTNUM=0
while getopts u:p:r:d:t:h OPT;do
  OPTNUM=`expr $OPTNUM + 1`
  case $OPT in
    "u" ) user="$OPTARG" ;;
    "p" ) pass="$OPTARG" ;;
    "r" ) repo="$OPTARG" ;;
    "d" ) description="$OPTARG" ;;
    "t" ) twofac="$OPTARG" ;;
    "h" ) echo "$HELP"; exit 0;;
    * )
      echo "$(basename $0) $OPT: unknown argument
Check \"$(basename $0) -h\" for further information" >&2; exit 1;
      ;;
  esac
done
shift $(($OPTIND - 1))

# Check user name
if [ "$user" = "" ];then
  echo -n "GitHub user name: "
  read user
fi

# Check repository name
if [ "$repo" = "" ];then
  echo -n "Repository name: "
  read repo
fi

## Check if the repository already exists or not.
et=$(curl --head https://github.com/$user/$repo 2>/dev/null|head -n1)
if echo $ret| grep -q "OK";then
  echo
  echo Repository: https://github.com/$user/$repo already exists.
  exit
fi

# Check description
if [ "$description" = "" ];then
  echo -n "Repository description: "
  read description
fi

# Check password
if [ "$pass" = "" ];then
  echo -n "GitHub Password: "
  read -s pass
  echo
fi

# Try to create w/o two-factor code
while [ 1 ];do
  ret=$(curl -u ${user}:${pass} https://api.github.com/user/repos -d "{\"name\":\"$repo\",\"description\":\"$description\"}" 2>/dev/null)
  if ! echo $ret| grep -q "Bad credentials";then
    break
  fi
  printf "\e[31;1mWrong Password!\e[0m GitHub Password: "
  read -s pass
  echo
done

# If it failed, try with two-factor code
if echo $ret| grep -q "Must specify two-factor authentication OTP code";then
  echo -n "GitHub Two-Factor Code: "
  read twofac
  while [ 1 ];do
    ret=$(curl -u ${user}:${pass} -H "X-GitHub-OTP: $twofac" https://api.github.com/user/repos -d "{\"name\":\"$repo\",\"description\":\"$description\"}" 2>/dev/null)
    if ! echo $ret| grep -q "Must specify two-factor authentication OTP code";then
      break
    fi
    printf "\e[31;1mWrong code!\e[0m GitHub Two-Factor Code: "
    read twofac
  done
fi

# OK!
echo
echo Repository: https://github.com/$user/$repo was created.

追記: 2014/08/27

パスワードや二段階認証を間違えた時に繰り返して聞く機能を追加。

追記ここまで

こんな感じ。

そのまま

$ ./github-createrepo.sh

とすると、GitHubのユーザー名と作りたいレポジトリ名を聞かれます。

まず、レポジトリがあるかどうかチェックして、 ない場合にレポジトリの説明を入力して、 その後にパスワードが聞かれます。

一度パスワードだけで試しますが、二段階認証の設定をしてると失敗するので、 その場合には二段階認証のコードを聞かれます。

hubコマンド なんか使うともっと簡単に作れますが、 取り敢えずやってみたかったので。

Sponsored Links
Sponsored Links

« brew-file: MacのHomebrewでBrewfileを扱うbrewallを改名した Homebrew-fileにレポジトリを作る機能を追加した »