-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·162 lines (139 loc) · 5.83 KB
/
install.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
#!/usr/bin/env bash
printf "🏀 let's ball...\n"
################################## CLONE REPO ##################################
(
cd $HOME
# only clone if folder doesn't already exist
if [ ! -d ".ballin-scripts" ]; then
echo ''
git clone https://github.com/JBallin/ballin-scripts.git
mv ballin-scripts .ballin-scripts
fi
)
############################## CHECK INITIAL SETUP #############################
# Check that /usr/local/bin is in $PATH
if [[ ! $PATH = *"/usr/local/bin:"* ]]; then
l1="usr/local/bin doesn't seem to be in your path."
l2='Run: echo \"export PATH=/usr/local/bin:$PATH\" > $HOME/.bash_profile'
l3="and open a new terminal window and run this installation again."
PATH_ERROR="\n⚠️ ERROR: $l1\n$l2\n$l3\n"
printf "$PATH_ERROR"
unset l1 l2 l3 PATH_ERROR
# Check that either gist or brew is installed
elif [ ! -x "$(command -v gist)" ] && [ ! -x "$(command -v brew)" ]; then
l1="Can't find Homebrew, which is needed to download 'gist'."
l2="Download Homebrew at brew.sh or install ruby & run 'gem install gist',"
l3="and run this installation again."
GIST_MISSING_ERROR="\n⚠️ ERROR: $l1\n$l2\n$l3\n"
printf "$GIST_MISSING_ERROR"
unset l1 l2 l3 GIST_MISSING_ERROR
else
#################################### GIST ####################################
# Retrieve path for token and URL from configuration
gist_token_path="$HOME/$(bin/ballin_config get gu.token_file)"
gist_config_url=$(bin/ballin_config get gu.url)
### DOWNLOAD GIST
# Check if gist is already installed, if not, install it
if [ ! -x "$(command -v gist)" ]; then
printf "\n🍺 brew installing gist...\n\n"
brew install gist
fi
### LOGIN GIST
# Check if token file exists and is valid, if not, delete it
if [ -f $gist_token_path ] && ! $(gist -l > /dev/null); then
printf "\n🗑 Deleting $gist_token_path because token is expired/invalid"
rm $gist_token_path
fi
# Prompt user for GitHub URL and token until a valid token file is created
while [ ! -f $gist_token_path ]; do
printf "\n🙏 Please enter your Gist base URL (for example, 'https://gist.github.com' for personal accounts or 'https://gist.[your GitHub Enterprise domain]' for enterprise accounts):\n"
read -p "URL: " URL
# Save entered URL to configuration
bin/ballin_config set gu.url $URL
gist_config_url=$(bin/ballin_config get gu.url)
# Check if entered URL is valid
if ! $(curl -s -o /dev/null $gist_config_url); then
printf "\n⛔️ Unable to reach $gist_config_url. Please verify your connection and the URL, and try again.\n"
continue
fi
# Guide user to generate a new token on GitHub
printf "\n1. Go to $gist_config_url/settings/tokens/new"
printf "\n2. Generate a new token with the 'gist' scope"
printf "\n3. Copy the token and paste it here\n"
read -sp "Token: " TOKEN
# Save entered token to file
echo $TOKEN > $gist_token_path
unset TOKEN
# Set secure permissions for the token file
chmod 600 $gist_token_path
done
########################## CREATE/UPDATE CONFIG FILE #########################
### CREATE/UPDATE CONFIG FILE
(
cd $HOME/.ballin-scripts/config/
if [ ! -f ballin.json ]; then
# create config
cp .defaultConfig.json ballin.json
printf "\n🧠 Created 'ballin.json' file in /config using default settings\n"
else
# update config
UPDATE_RESULT=$(node $HOME/.ballin-scripts/config/updateConfig.js)
if [ ! -z "$UPDATE_RESULT" ]; then
printf "\n🙌 $UPDATE_RESULT\n"
fi
fi
)
(
l1='### Backup of your dev environment'
l2='Created by [ballin-scripts](https://github.com/JBallin/ballin-scripts)'
GIST_DESCRIPTION="$l1\n$l2\n"
### CHECK IF USER ALREADY HAS GIST ID
cd $HOME/.ballin-scripts/
if [ $(bin/ballin_config get gu.id) == 'null' ]; then
echo ''
read -p "🤔 Do you already have a ballin-scripts backup gist? [y/N] " YN
if [[ $YN == "y" || $YN == "Y" ]]; then
unset YN
VALID_GIST_ID=1
printf "\nWelcome Back!\n"
while [ $VALID_GIST_ID == 1 ]; do
read -ep "Enter your gist ID: " GIST_ID
if [ "$(gist -r $GIST_ID)" == "$(printf "$GIST_DESCRIPTION")" ]; then
printf "\n👍 Storing your previous gist ID in your config:\n"
bin/ballin_config set gu.id $GIST_ID
VALID_GIST_ID=0
# TODO: overwrite ballin.json config file from ballin.json in gist (if it exists) and echo that to user (both action and the stored config?). what if there were updates to the default though? maybe just copy the default and then overwrite any values that exist in the previous ballin.json
else
printf "\n⚠️ INVALID: Expected \e[1mgist -r '$GIST_ID'\e[0m to output:\n$GIST_DESCRIPTION\n"
fi
done
fi
unset YN GIST_ID VALID_GIST_ID
fi
### GENERATE + STORE GIST ID
if [ $(bin/ballin_config get gu.id) == 'null' ]; then
printf "$GIST_DESCRIPTION" > .MyConfig.md
GIST_URL=$(gist -p .MyConfig.md)
printf "\n💥 Created a private gist titled '.MyConfig' at the following URL:\n$GIST_URL\n"
GIST_ID=${GIST_URL##*/}
printf "\n🧳 Storing your new gist ID in your config...\n"
bin/ballin_config set gu.id $GIST_ID
if [ -d .gu-cache ]; then
rm -rf .gu-cache
printf "\n🗑 Deleted existing .gu-cache folder\n"
fi
unset GIST_URL GIST_ID l1 l2 GIST_DESCRIPTION
rm .MyConfig.md
fi
################################# NPM INSTALL ################################
# production === don't install devDeps
printf "\n🧐 installing any missing dependencies...\n"
npm i --production > /dev/null 2>&1
)
############################## SYMLINK BINARIES ##############################
for bin in $HOME/.ballin-scripts/bin/*; do
ln -sf $bin /usr/local/bin
done
printf "\n💪 symlinked binaries\n"
printf "\n😎 ballin!\n"
fi