🐛 Refactored to remove bug from init script

This commit is contained in:
z3rOR0ne 2022-09-08 02:53:05 -07:00
parent 476b538fa7
commit c65ee90624
2 changed files with 150 additions and 140 deletions

View file

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
# so far requires gitmojis (txt file) and also
# bgit_gitmoji for function echomoji() that displays gitmojis in terminal and also
# bgit_init for function b_init()
# Error handling # Error handling
set -e set -e
# For styling/colorizing output # For styling/colorizing output
@ -35,10 +39,9 @@ tput bold & tput setaf 7
function bgit() { function bgit() {
# Intro Prompt # Intro Prompt
echo "${txtblue} bgit: a handy shell script for simple git inits/commits${txtred}" echo "${txtblue} bgit: a handy shell script for automating simple git inits/commits${txtred}"
numrepos=$(git remote | wc -l) numrepos=$(git remote | wc -l)
numargs="$#" numargs="$#"
# args=("$@") # args=("$@")
@ -92,7 +95,7 @@ function bgit() {
bat $HOME/.config/bgit/gitmojis bat $HOME/.config/bgit/gitmojis
else else
dependencycheck less dependencycheck less
less $HOME/.config/bgit/gitmojis less ~/scripts/gitmojis
fi fi
else else
echo "${txtblue} no gitmoji specified...${txtwhite}" echo "${txtblue} no gitmoji specified...${txtwhite}"
@ -126,7 +129,7 @@ function bgit() {
git commit -m "$gitmoji $cmessage" > /dev/null 2>&1; git commit -m "$gitmoji $cmessage" > /dev/null 2>&1;
if [[ "$gitmoji" != "" ]] ; then if [[ "$gitmoji" != "" ]] ; then
source $HOME/.config/bgit/bgit_echomoji source $HOME/scripts/bgit_echomoji
echomoji $gitmoji echomoji $gitmoji
else else
echo "${txtblue}$cmessage${txtwhite}" echo "${txtblue}$cmessage${txtwhite}"

View file

@ -1,31 +1,36 @@
#!/bin/bash #!/bin/bash
function b_init() { function b_init() {
while true while true
do do
dependencycheck read #Prompts the user if they'd like to initialize repository
read -e -r -p "${txtwhite} initialize repository?(y/n): ${txtyellow}" init read -e -r -p "${txtblue} initialize repository?(y/n): ${txtyellow}" init
if [[ $init == "n" || $init = "no" ]]; then if [[ $init == "n" || $init = "no" ]]; then
echo "${txtred} no git repository initialized...exiting${txtwhite}" echo "${txtred} no git repository initialized...exiting${txtwhite}"
break break
elif [[ $init == "y" || $init == "yes" ]] ; then elif [[ $init == "y" || $init == "yes" ]] ; then
# Creates a default README.md
if [ ! -f "README.md" ] ; then if [ ! -f "README.md" ] ; then
echo -e "## A Simple README\n\nSome Text\n\n__Some Checklist__\n\n- [x] Completed task\n- [ ] Incomplete task" > README.md echo -e "## A Simple README\n\nSome Text\n\n__Some Checklist__\n\n- [x] Completed task\n- [ ] Incomplete task" > README.md
fi fi
# Creates a default .gitignore
echo -e "node_modules\n*.env\n*.out\n.pretterrc\n.gitignore" > .gitignore echo -e "node_modules\n*.env\n*.out\n.pretterrc\n.gitignore" > .gitignore
git init # Initializes git repository cwd...
git init > /dev/null 2>&1
# defaults to not requiring gpg key sign
git config commit.gpgsign false git config commit.gpgsign false
while true while [ true ];
do do
read -e -r -p "${txtwhite} would you like to change username/password from global defaults?(y/n): ${txtyellow}" changedefaults # prompts user to change default username/email and changes them if they'd like (no input defaults to global values)
read -e -r -p "${txtblue} would you like to change username/password from global defaults?(y/n): ${txtyellow}" changedefaults
if [[ $changedefaults == "y" || $changedefaults == "yes" ]] ; then if [[ $changedefaults == "y" || $changedefaults == "yes" ]] ; then
read -e -r -p "${txtwhite} enter your username: ${txtyellow}" uname read -e -r -p "${txtblue} enter your username: ${txtyellow}" uname
if [[ -n $uname ]] ; then if [[ -n $uname ]] ; then
git config user.name $uname git config user.name $uname
read -e -r -p "${txtwhite}﫯 enter your email: ${txtyellow}" uemail read -e -r -p "${txtblue}﫯 enter your email: ${txtyellow}" uemail
if [[ -n $uemail ]] ; then if [[ -n $uemail ]] ; then
git config user.email $uemail git config user.email $uemail
break break
@ -38,16 +43,16 @@ function b_init() {
break break
fi fi
else else
echo "${txtblue} defaulting to global username/password${txtwhite}" break echo "${txtblue} defaulting to global username/password${txtwhite}"
break
fi fi
done done
while true while true
do do
read -e -r -p "${txtwhite} would you like to make this an npm project?(y/n): ${txtyellow}" confirmnpm read -e -r -p "${txtblue} would you like to make this an npm project?(y/n): ${txtyellow}" confirmnpm
if [[ $confirmnpm == "y" || $confirmnpm == "yes" ]] ; then if [[ $confirmnpm == "y" || $confirmnpm == "yes" ]] ; then
tput bold & tput setaf 7 dependencycheck node npm
dependencycheck sed node npm prettier
npm init -y > /dev/null 2>&1 npm init -y > /dev/null 2>&1
npm install -D nodemon prettier jest > /dev/null 2>&1 npm install -D nodemon prettier jest > /dev/null 2>&1
@ -55,16 +60,12 @@ function b_init() {
echo -e 'trailingComma: "all"\ntabWidth: 4\nsemi: false\nsingleQuote: true\nbracketSpacing: true\narrowParens: "avoid"' > .prettierrc echo -e 'trailingComma: "all"\ntabWidth: 4\nsemi: false\nsingleQuote: true\nbracketSpacing: true\narrowParens: "avoid"' > .prettierrc
echo -e '<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width">\n <title>My Website</title>\n <link rel="stylesheet" href="css/styles.css" />\n </head>\n <body>\n <h1>Header 1</h1>\n <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n <script src="scripts/index.js" defer></script>\n </body>\n</html>' > index.html echo -e '<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width">\n <title>My Website</title>\n <link rel="stylesheet" href="css/styles.css" />\n </head>\n <body>\n <h1>Header 1</h1>\n <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>\n <script src="scripts/index.js" defer></script>\n </body>\n</html>' > index.html
mkdir scripts mkdir scripts
mkdir css mkdir css
mkdir tests mkdir tests
touch tests/index.test.js touch tests/index.test.js
echo 'console.log("Hello World!");' > scripts/index.js echo 'console.log("Hello World!");' > scripts/index.js
echo -e '* {\n font-family: monospace;\n text-align: center;\n background-color: #dedede;\n}' > css/styles.css echo -e '* {\n font-family: monospace;\n text-align: center;\n background-color: #dedede;\n}' > css/styles.css
dependencycheck sed
sed 's/1"/1",/' package.json > package2.json && sed -i '/no test/a\ \ \ \ "start":\ "nodemon\ scripts/index.js"' package2.json && mv package2.json package.json sed 's/1"/1",/' package.json > package2.json && sed -i '/no test/a\ \ \ \ "start":\ "nodemon\ scripts/index.js"' package2.json && mv package2.json package.json
break break
else else
@ -74,20 +75,20 @@ function b_init() {
tput bold & tput setaf 7 tput bold & tput setaf 7
git add . >/dev/null 2>&1 git add . > /dev/null 2>&1
git commit -m ":tada: Initial commit!" >/dev/null 2>&1 git commit -m ":tada: Initial commit!" > /dev/null 2>&1
echo "${txtblue} Initial commit!${txtwhite}" echo "${txtblue} Initial commit!${txtwhite}"
while true while true
do do
read -e -r -p "${txtwhite} change branch name?(y/n): ${txtyellow}" chbranch read -e -r -p "${txtblue} change branch name?(y/n): ${txtyellow}" chbranch
if [[ $chbranch == "y" || $chbranch == "yes" ]] ; then if [[ $chbranch == "y" || $chbranch == "yes" ]] ; then
read -e -r -p "${txtwhite} branch name?(i.e. 'main'): ${txtyellow}" branch read -e -r -p "${txtblue} branch name?(i.e. 'main'): ${txtyellow}" branch
if [ -n "$branch" ] ; then if [ -n "$branch" ] ; then
git branch -M $branch git branch -M $branch
break break
else else
echo "${txtred}✗ branch name is empty, please enter branch name!${txtwhite}" echo "${txtred} branch name is empty, please enter branch name!${txtwhite}"
fi fi
else else
echo "${txtblue} branch name will be left as ${txtyellow}'master'${txtwhite}" echo "${txtblue} branch name will be left as ${txtyellow}'master'${txtwhite}"
@ -96,65 +97,71 @@ function b_init() {
fi fi
done done
# main git init loop
while true while true
do do
uname=$(git config user.name) uname=$(git config user.name)
read -e -r -p "${txtwhite} Please enter remote to add(i.e. origin): ${txtyellow}" remote read -e -r -p "${txtblue} Please enter remote to add(i.e. origin): ${txtyellow}" remote
# if the user doesn't enter anything for the remote prompt, it defaults to origin
if [[ -z $remote ]]; then if [[ -z $remote ]]; then
remote="origin" remote="origin"
fi fi
while [ -z $host ] while [ -z $host ]
do do
read -e -r -p "${txtwhite} Please enter host site (i.e. github.com): ${txtyellow}" host read -e -r -p "${txtblue} Please enter host site (i.e. github.com): ${txtyellow}" host
done done
while [ -z $repo ] while [ -z $repo ]
do do
read -e -r -p "${txtwhite} Please enter name of repository: ${txtyellow}" repo read -e -r -p "${txtblue} Please enter name of repository: ${txtyellow}" repo
done done
tput bold & tput setaf 7 tput bold & tput setaf 7
if [[ -n $host && $host == "github.com" ]] ; then if [[ -n $host && $host == "github.com" ]] ; then
read -e -r -p "${txtwhite} Host site is github.com, would you like bgit to create the repository?(y/n): ${txtyellow}" crepo read -e -r -p "${txtblue} Host site is github.com, would you like bgit to create the repository?(y/n): ${txtyellow}" crepo
tput bold & tput setaf 7 tput bold & tput setaf 7
if [[ ! -f $HOME/.gh_pat ]]; then if [[ ! -f $HOME/.gh_pat ]]; then
echo "${txtred} Github personal access token doesn't exist" echo "${txtred} Github personal access token doesn't exist"
echo " See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token${txtwhite}" echo " See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token"
echo " create a $HOME/.gh_pat file and put your personal access token in it like so:"
echo ' echo > "GH_TOKEN=your_gh_token_goes_here'
tput bold & tput setaf 7
exit 1 exit 1
else
source $HOME/.gh_pat
fi fi
source $HOME/.gh_pat
if [[ $crepo == 'y' || $crepo == 'yes' ]] ; then if [[ $crepo == 'y' || $crepo == 'yes' ]] ; then
dependencycheck curl dependencycheck curl
echo "${txtgreen} Host Site is github.com, creating repository...${txtwhite}" echo "${txtgreen} Host Site is github.com, creating repository...${txtwhite}"
curl -u $uname:$GH_TOKEN https://api.github.com/user/repos -d '{"name": "'${repo}'"}' curl -u $uname:$GH_TOKEN https://api.github.com/user/repos -d '{"name": "'${repo}'"}' > /dev/null 2>&1
fi fi
echo -e "${txtblue} Your \$GH_TOKEN is saved to your clipboard...\nsimply enter CTRL+SHIFT+V to paste your token...\nwhen prompted for your password${txtwhite}" echo -e "${txtblue} Your \$GH_TOKEN is saved to your clipboard...\nsimply enter CTRL+SHIFT+V to paste your token...\nwhen prompted for your password${txtwhite}"
dependencycheck xclip
echo $GH_TOKEN | xclip -sel clip echo $GH_TOKEN | xclip -sel clip
fi fi
git remote add $remote "https://$host/$uname/$repo" git remote add $remote "https://$host/$uname/$repo"
git push --set-upstream $remote $branch git push --set-upstream $remote $branch > /dev/null 2>&1
if [[ $host == "github.com" ]] ; then if [[ $host == "github.com" ]] ; then
echo "${txtgreen} Github repository created and initiated!!${txtwhite}" echo "${txtgreen} GitHub repository created and initiated!!${txtwhite}"
echo '' | xclip && xclip -selection clipboard /dev/null echo '' | xclip && xclip -selection clipboard /dev/null
exit 0
else else
echo "${txtgreen} Git repository initiated!!${txtwhite}" echo "${txtgreen} Git repository initiated!!${txtwhite}"
exit 0
fi fi
break break
done done
break
else else
break break
fi fi