-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_create
executable file
·98 lines (84 loc) · 2.13 KB
/
repo_create
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
98
#!/usr/bin/env bash
declare name
declare -A colors
colors[red]=$(tput setaf 1)
colors[green]=$(tput setaf 2)
colors[blue]=$(tput setaf 4)
colors[reset]=$(tput sgr0)
color() {
local c
c="$1"
shift
printf '%s' "${colors[$c]}"
printf '%s\n' "$@"
printf '%s' "${colors[reset]}"
}
err() {
color red "$@" >&2
}
die() {
[[ -n "$1" ]] && err "$@"
exit 1
}
has() {
local verbose
if [[ $1 = '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
ask() {
read -r -n1 -p "$* " ans
printf '\n'
[[ ${ans^} = Y* ]]
}
repo_init_github() {
# https://developer.github.com/v3/repos/#create
# if res=$(curl https://api.github.com/user/repos -H "Authorization: token $GITHUB_API_TOKEN" --json "{\"name\":\"$1\"}"); then
if res=$(api github post user/repos --json ".name=\"$1\""); then
if [[ $(jq -r '.errors' <<< "$res") != null ]]; then
die "$(jq -r '.errors[0].message' <<< "$res")"
fi
if [[ -d '.git' ]] && ask 'push current repo?'; then
git remote add origin "$(jq -r '.ssh_url' <<< "$res")"
git push -u origin "$(git branch --show-current)"
fi
jq -r '.html_url' <<< "$res"
else
jq <<< "$res"
fi
}
repo_init_gitlab() {
if res=$(api gitlab post projects -d "name=$1"); then
[[ "$(jq -r '.message' <<< "$res")" != 'null' ]] &&
die "$(jq '.message' <<< "$res")"
if [[ -d '.git' ]] && ask 'push current repo?'; then
git remote add origin "$(jq -r '.ssh_url_to_repo' <<< "$res")"
git push -u origin "$(git branch --show-current)"
fi
jq -r '.http_url_to_repo' <<< "$res"
else
jq <<< "$res"
fi
}
has -v api jq || die
service="$1"
shift
has "repo_init_$service" || {
mapfile -t configs_avail < <(compgen -A function | perl -lne 'print $1 if /repo_init_(.*)/')
die "can't create repo at $service" "the folowing are allowed: ${configs_avail[*]}"
}
name="${1// }"
if [[ -z $name ]]; then
dir="${PWD##*/}"
read -r -e -p 'name: ' -i "${dir// }" name
[[ -z $name ]] && die
name="${name// }"
fi
"repo_init_$service" "$name"