From 92d68982280f569e1e1b99bad9d9ce9308085c05 Mon Sep 17 00:00:00 2001 From: "Roberto A. Foglietta" Date: Fri, 1 Sep 2023 23:50:32 +0200 Subject: [PATCH 1/3] install.sh: script review and minor changes Signed-off-by: Roberto A. Foglietta --- install.sh | 94 ++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/install.sh b/install.sh index 143d8b6..24178fb 100755 --- a/install.sh +++ b/install.sh @@ -5,17 +5,13 @@ if [[ $EUID -ne 0 ]]; then exit 1 fi # Check dependencies -if type curl &>/dev/null; then - echo "" &>/dev/null -else +if ! type curl &>/dev/null; then echo "You need to install 'curl' to use the chatgpt script." - exit + exit 0 fi -if type jq &>/dev/null; then - echo "" &>/dev/null -else +if ! type jq &>/dev/null; then echo "You need to install 'jq' to use the chatgpt script." - exit + exit 0 fi # Installing imgcat if using iTerm @@ -50,49 +46,49 @@ echo "The script will add the OPENAI_KEY environment variable to your shell prof echo "Would you like to continue? (Yes/No)" read -e answer if [ "$answer" == "Yes" ] || [ "$answer" == "yes" ] || [ "$answer" == "y" ] || [ "$answer" == "Y" ] || [ "$answer" == "ok" ]; then + : +else + echo "Please take a look at the instructions to install manually: https://github.com/0xacx/chatGPT-shell-cli/tree/main#manual-installation " + exit 0 +fi - read -p "Please enter your OpenAI API key: " key +read -p "Please enter your OpenAI API key: " key - # Adding OpenAI key to shell profile - # zsh profile - if [ -f ~/.zprofile ]; then - echo "export OPENAI_KEY=$key" >>~/.zprofile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.zprofile - fi - echo "OpenAI key and chatgpt path added to ~/.zprofile" - source ~/.zprofile - # zshrc profile for debian - elif [ -f ~/.zshrc ]; then - echo "export OPENAI_KEY=$key" >>~/.zshrc - if [[ ":$PATH:" == *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.zshrc - fi - echo "OpenAI key and chatgpt path added to ~/.zshrc" - source ~/.zshrc - # bash profile mac - elif [ -f ~/.bash_profile ]; then - echo "export OPENAI_KEY=$key" >>~/.bash_profile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.bash_profile - fi - echo "OpenAI key and chatgpt path added to ~/.bash_profile" - source ~/.bash_profile - # profile ubuntu - elif [ -f ~/.profile ]; then - echo "export OPENAI_KEY=$key" >>~/.profile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.profile - fi - echo "OpenAI key and chatgpt path added to ~/.profile" - source ~/.profile - else - export OPENAI_KEY=$key - echo "You need to add this to your shell profile: export OPENAI_KEY=$key" +# Adding OpenAI key to shell profile +# zsh profile +if [ -f ~/.zprofile ]; then + echo "export OPENAI_KEY=$key" >>~/.zprofile + if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >>~/.zprofile fi - echo "Installation complete" - + echo "OpenAI key and chatgpt path added to ~/.zprofile" + source ~/.zprofile +# zshrc profile for debian +elif [ -f ~/.zshrc ]; then + echo "export OPENAI_KEY=$key" >>~/.zshrc + if [[ ":$PATH:" == *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >>~/.zshrc + fi + echo "OpenAI key and chatgpt path added to ~/.zshrc" + source ~/.zshrc +# bash profile mac +elif [ -f ~/.bash_profile ]; then + echo "export OPENAI_KEY=$key" >>~/.bash_profile + if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >>~/.bash_profile + fi + echo "OpenAI key and chatgpt path added to ~/.bash_profile" + source ~/.bash_profile +# profile ubuntu +elif [ -f ~/.profile ]; then + echo "export OPENAI_KEY=$key" >>~/.profile + if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >>~/.profile + fi + echo "OpenAI key and chatgpt path added to ~/.profile" + source ~/.profile else - echo "Please take a look at the instructions to install manually: https://github.com/0xacx/chatGPT-shell-cli/tree/main#manual-installation " - exit + export OPENAI_KEY=$key + echo "You need to add this to your shell profile: export OPENAI_KEY=$key" fi +echo "Installation complete" From 970c51055f341a845e739a798741fba53109f8e7 Mon Sep 17 00:00:00 2001 From: "Roberto A. Foglietta" Date: Fri, 1 Sep 2023 23:59:47 +0200 Subject: [PATCH 2/3] install.sh: script review and improvements Signed-off-by: Roberto A. Foglietta --- install.sh | 65 ++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/install.sh b/install.sh index 24178fb..98e1eec 100755 --- a/install.sh +++ b/install.sh @@ -42,52 +42,39 @@ fi chmod +x /usr/local/bin/chatgpt echo "Installed chatgpt script to /usr/local/bin/chatgpt" -echo "The script will add the OPENAI_KEY environment variable to your shell profile and add /usr/local/bin to your PATH" -echo "Would you like to continue? (Yes/No)" -read -e answer -if [ "$answer" == "Yes" ] || [ "$answer" == "yes" ] || [ "$answer" == "y" ] || [ "$answer" == "Y" ] || [ "$answer" == "ok" ]; then - : -else - echo "Please take a look at the instructions to install manually: https://github.com/0xacx/chatGPT-shell-cli/tree/main#manual-installation " +echo "The script will add the OPENAI_KEY environment variable" +echo "to your shell profile and /usr/local/bin to your PATH" +read -ep "Would you like to continue? (Yes/No) " yn +yn=${yn/Y/y} +yn=${yn/ok/y} +if ! test "${yn:0:1}" == "y"; then + echo + echo "Please take a look at the instructions to install manually:" + echo "https://github.com/0xacx/chatGPT-shell-cli/tree/main#manual-installation" + echo exit 0 fi read -p "Please enter your OpenAI API key: " key # Adding OpenAI key to shell profile -# zsh profile -if [ -f ~/.zprofile ]; then - echo "export OPENAI_KEY=$key" >>~/.zprofile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.zprofile +# RAF: .bashrc was missing, added + using a for loop +envset=0 +for i in .zprofile .zshrc .bash_profile .bashrc .profile; do + if [ -f ~/$i ]; then + echo "export OPENAI_KEY=$key" >>~/$i + if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >>~/$i + fi + echo "OpenAI key and chatgpt path added to ~/$i" +# RAF: sourcing an enviroment within a sub-shell does not affect +# the parent shell enviroment unless using source install.sh +# source ~/$i + envset=1 fi - echo "OpenAI key and chatgpt path added to ~/.zprofile" - source ~/.zprofile -# zshrc profile for debian -elif [ -f ~/.zshrc ]; then - echo "export OPENAI_KEY=$key" >>~/.zshrc - if [[ ":$PATH:" == *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.zshrc - fi - echo "OpenAI key and chatgpt path added to ~/.zshrc" - source ~/.zshrc -# bash profile mac -elif [ -f ~/.bash_profile ]; then - echo "export OPENAI_KEY=$key" >>~/.bash_profile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.bash_profile - fi - echo "OpenAI key and chatgpt path added to ~/.bash_profile" - source ~/.bash_profile -# profile ubuntu -elif [ -f ~/.profile ]; then - echo "export OPENAI_KEY=$key" >>~/.profile - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/.profile - fi - echo "OpenAI key and chatgpt path added to ~/.profile" - source ~/.profile -else +done + +if [ $envset -eq 0 ]; then export OPENAI_KEY=$key echo "You need to add this to your shell profile: export OPENAI_KEY=$key" fi From 64200104528c4684da917412f54a1a14b277f255 Mon Sep 17 00:00:00 2001 From: "Roberto A. Foglietta" Date: Sat, 2 Sep 2023 00:25:50 +0200 Subject: [PATCH 3/3] install.sh: set the shell environment also for the $SUDO_USER Signed-off-by: Roberto A. Foglietta --- install.sh | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/install.sh b/install.sh index 98e1eec..1926077 100755 --- a/install.sh +++ b/install.sh @@ -60,18 +60,21 @@ read -p "Please enter your OpenAI API key: " key # Adding OpenAI key to shell profile # RAF: .bashrc was missing, added + using a for loop envset=0 -for i in .zprofile .zshrc .bash_profile .bashrc .profile; do - if [ -f ~/$i ]; then - echo "export OPENAI_KEY=$key" >>~/$i - if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo 'export PATH=$PATH:/usr/local/bin' >>~/$i +for u in '' $SUDO_USER; do + for i in .zprofile .zshrc .bash_profile .bashrc .profile; do + pf=$(eval readlink -e ~$u/$i) + if [ -f "$pf" ]; then + echo "export OPENAI_KEY=$key" >> $pf + if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then + echo 'export PATH=$PATH:/usr/local/bin' >> $pf + fi + echo "OpenAI key and chatgpt path added to ~$u/$i" + # RAF: sourcing an enviroment within a sub-shell does not affect + # the parent shell enviroment unless using source install.sh + # source ~/$i + envset=1 fi - echo "OpenAI key and chatgpt path added to ~/$i" -# RAF: sourcing an enviroment within a sub-shell does not affect -# the parent shell enviroment unless using source install.sh -# source ~/$i - envset=1 - fi + done done if [ $envset -eq 0 ]; then