-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgist-unpack.sh
executable file
·179 lines (168 loc) · 4.9 KB
/
gist-unpack.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
echo Usage: "$0" [ GIST_ID | URL ] [ -u USER ] [ -t TOKEN ] >&2
exit 1
fi
FORCE=
GIST_ID=
USER=
TOKEN=
URL=
while [ $# -gt 0 ]; do
case "$1" in
-f|--force)
echo Forcing install... >&2
FORCE=1
shift
;;
-u|--user)
if [ -n "$USER" ]; then
echo Multiple users passed! >&2
exit 1
fi
shift
USER="$1"
shift
;;
-t|--token)
if [ -n "$TOKEN" ]; then
echo Multiple tokens passed! >&2
exit 1
fi
shift
TOKEN="$1"
shift
;;
-g|--gist-id)
if [ -n "$GIST_ID" ] || [ -n "$URL" ]; then
echo Multiple URLs or Gist ids passed! >&2
exit 1
fi
shift
GIST_ID="$1"
shift
;;
-u|--url)
if [ -n "$GIST_ID" ] || [ -n "$URL" ]; then
echo Multiple URLs or Gist ids passed! >&2
exit 1
fi
shift
URL="$1"
shift
;;
https://*|ssh://*|git://*)
if [ -n "$GIST_ID" ] || [ -n "$URL" ]; then
echo Multiple URLs or Gist ids passed! >&2
exit 1
fi
URL="$1"
shift
;;
*)
if [ -n "$GIST_ID" ] || [ -n "$URL" ]; then
echo Multiple URLs or Gist ids passed! >&2
exit 1
fi
GIST_ID="$1"
echo Using gist "$GIST_ID" >&2
shift
;;
esac
done
if [ -n "$GIST_ID" ]; then
URL=ssh://[email protected]/"$GIST_ID".git
fi
if [ -z "$URL" ]; then
echo No gist id or url provided. >&2
exit 1
fi
mkdir -p $HOME/.ssh
echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" >> $HOME/.ssh/known_hosts
ssh -T [email protected] 2> /dev/null
if [ $? -eq 255 ]; then
if ! [ -f "$HOME/.ssh/id_rsa" ]; then
echo No ssh key is available. Generating it and adding it to GitHub to continue.
ssh-keygen -t rsa -N "" -f "$HOME/.ssh/id_rsa"
fi
if [ -z "$USER" ] && [ -t 1 ]; then
echo -n "GitHub Username: "; read USER
fi
if [ -z "$USER" ]; then
echo Username cannot be empty. Pass -u with your username to continue.
exit 1
fi
auth="$USER"
if [ -n "$TOKEN" ]; then
auth="$auth:$TOKEN"
fi
curl -u "$auth" -d "$(printf '{"title": "%s", "key": "%s"}' "${HOST-$(hostname)}" "$(cat $HOME/.ssh/id_rsa.pub)")" https://api.github.com/user/keys > /dev/null
fi
gistdir="$(mktemp -d)"
setup() {
if ! git clone "$URL" "$gistdir"; then
echo Failed to clone Gist. Verify "$URL" exists >&2
echo and you have permission to access it. >&2
exit 1
fi
pushd "$gistdir" >/dev/null
}
cleanup() {
popd >/dev/null
rm -rf "$gistdir"
}
setup
trap cleanup EXIT
if [ -n "${BASH_VERSION-}" ]; then
shopt -s dotglob
fi
shopt -s nullglob
for f in *; do
if [ "$f" = ".git" ]; then
continue
fi
if ! [ -f "$f" ]; then
echo Skipping "$f", not a file >&2
continue
fi
DEST=
case "$f" in
settings.el) DEST="$HOME/.config/emacs/settings.el" ;;
.sshconfig) DEST="$HOME/.ssh/config" ;;
.ssh_authorized_keys) DEST="$HOME/.ssh/authorized_keys" ;;
nix.conf) DEST="$HOME/.config/nix/nix.conf" ;;
machines) DEST="$HOME/.config/nix/machines" ;;
.gitignore) DEST="$HOME/.config/git/ignore" ;;
.gitconfig) DEST="$HOME/.config/git/config" ;;
.gitattributes) DEST="$HOME/.config/git/attributes" ;;
credentials|.aws_credentials) DEST="$HOME/.aws/credentials" ;;
*) DEST="$HOME/$f" ;;
esac
CONCAT=
case "$f" in
.authinfo) CONCAT=1 ;;
.sshconfig) CONCAT=1 ;;
.ssh_authorized_keys) CONCAT=1 ;;
.gitignore) CONCAT=1 ;;
nix.conf) CONCAT=1 ;;
machines) CONCAT=1 ;;
esac
if [ -z "$DEST" ]; then
echo Skipping "$f", no destination found >&2
continue
fi
if [ -f "$DEST" ] && [ -z "$FORCE" ] && [ -z "$CONCAT" ]; then
echo Skipping "$f", destination already exists >&2
continue
fi
mkdir -p "$(dirname "$DEST")"
if [ -n "$CONCAT" ]; then
cat "$f" >> "$DEST"
else
cp "$f" "$DEST"
fi
case "$f" of
.authinfo) chmod 600 "$DEST" ;;
"Library/LaunchAgents/"*) launchctl load -w "$HOME/$f" ;;
esac
done