diff --git a/brain.sh b/brain.sh index 7475da6..11342a9 100755 --- a/brain.sh +++ b/brain.sh @@ -2,6 +2,10 @@ set -e +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + readonly brain_dir="$HOME/.brain" readonly VERSION_FILE=~/.brain/version readonly _brain_bin="/usr/local/bin/brain" @@ -23,7 +27,7 @@ usage() { echo " install Install a package" echo " uninstall Uninstall a package" echo " update Update a package" - echo " neurons List (all / installed) / search / info about packages" + echo " neurons List (local / remote) / search / info about packages" echo "" echo "For more information, visit https://github.com/neobrains/brain" } @@ -31,9 +35,8 @@ usage() { neurons_usage() { echo "Usage: $(basename "$0") [options] [command]" echo "Commands:" - echo " list all List all available packages" - echo " list installed List locally installed packages" - echo " search Search for a package" + echo " list remote List all available packages" + echo " list local List locally installed packages" echo " info Show information about a package" echo "" echo "For more information, visit https://github.com/neobrains/brain" @@ -41,13 +44,13 @@ neurons_usage() { check_sudo() { if ! sudo -v &>/dev/null; then - printf "\e[31mError: You need to have sudo privileges to use this command\e[0m\n" + printf "${RED}Error: You need to have sudo privileges to use this command${NC}\n" exit 1 fi } if ! command -v curl &>/dev/null; then - printf "\e[31mError: curl is not installed. Please install curl to continue.\e[0m\n" + printf "${RED}Error: curl is not installed. Please install curl to continue.${NC}\n" exit 1 fi @@ -70,10 +73,10 @@ if [[ $1 =~ (-u|--upgrade) ]]; then if [ ! -d "$brain_dir" ]; then mkdir "$brain_dir" fi - latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | grep -o '"tag_name": "[^"]*"' | cut -d'"' -f4) + latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | jq -r '.tag_name') echo "$latest_version" >"$VERSION_FILE" sudo mv brain /usr/local/bin/ - printf "\e[32mbrain has been updated.\e[0m\n" + printf "\e[32mbrain has been updated.${NC}\n" } update_brain exit 0 @@ -88,7 +91,7 @@ if [[ $1 =~ (-r|--remove) ]]; then if [ -d ~/.brain ]; then sudo rm -rf ~/.brain fi - printf "\e[32mbrain has been removed.\e[0m\n" + printf "\e[32mbrain has been removed.${NC}\n" fi fi @@ -101,37 +104,30 @@ if [[ $1 =~ (neurons) ]]; then case "$2" in "list") if [ -z "$3" ]; then - echo -e "\e[31mError: You must provide a package name to list\e[0m" + echo -e "${RED}Error: You must provide a package name to list${NC}" neurons_usage exit 1 fi case "$3" in - "all") - echo "not implemented yet" + "remote") + NEURONS=$(curl -s https://api.github.com/repos/neobrains/brain/contents/neurons | jq -r '.[].name') + for neuron in $NEURONS; do + echo "${neuron%.*}" + done ;; - "installed") + "local") echo "not implemented yet" ;; *) - echo -e "\e[31mError: Unknown command '$3'\e[0m" + echo -e "${RED}Error: Unknown command '$3'${NC}" neurons_usage exit 1 ;; esac ;; - "search") - - if [ -z "$3" ]; then - echo -e "\e[31mError: You must provide a package name to search\e[0m" - neurons_usage - exit 1 - fi - - echo "not implemented yet" - ;; "info") if [ -z "$3" ]; then - echo -e "\e[31mError: You must provide a package name to info\e[0m" + echo -e "${RED}Error: You must provide a package name to info${NC}" neurons_usage exit 1 fi @@ -139,7 +135,7 @@ if [[ $1 =~ (neurons) ]]; then echo "not implemented yet" ;; *) - echo -e "\e[31mError: Unknown command '$2'\e[0m" + echo -e "${RED}Error: Unknown command '$2'${NC}" neurons_usage exit 1 ;; @@ -160,7 +156,7 @@ if [[ $1 =~ (install|update|uninstall) ]]; then ;; esac if [ -z "$2" ]; then - echo -e "\e[31mError: You must provide a package name to $1\e[0m" + echo -e "${RED}Error: You must provide a package name to $1${NC}" usage exit 1 fi @@ -168,7 +164,7 @@ if [[ $1 =~ (install|update|uninstall) ]]; then script=$(curl -sL "$neurons_git/$2.sh") if [ "$script" == "404: Not Found" ]; then - echo -e "\e[31mError: Package '$2' not found\e[0m" + echo -e "${RED}Error: Package '$2' not found${NC}" exit 1 fi @@ -177,7 +173,7 @@ if [[ $1 =~ (install|update|uninstall) ]]; then exit_status=$? if [ $exit_status -ne 0 ]; then - echo -e "\e[31mError: $2 failed to $1\e[0m" + echo -e "${RED}Error: $2 failed to $1${NC}" exit 1 fi exit 0 @@ -186,7 +182,7 @@ fi case $1 in "${valid_commands[@]}") ;; *) - echo -en "\e[31mError: Unknown command '$1'\e[0m\n" + echo -en "${RED}Error: Unknown command '$1'${NC}\n" usage exit 1 ;; diff --git a/install.sh b/install.sh index b107627..810fffa 100755 --- a/install.sh +++ b/install.sh @@ -22,9 +22,20 @@ check_sudo() { fi } +check_jq_installed() { + if ! command -v jq &>/dev/null; then + echo -e "${RED}Error: jq is not installed. Please install jq to continue.${NC}" + echo -e "On apt based systems, you can install it using: sudo apt install jq" + echo -e "For more information, visit https://stedolan.github.io/jq" + exit 1 + fi +} + +check_jq_installed + echo -e "Installing brain..." -latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | grep -o '"tag_name": "v[^"]*"' | grep -o 'v[^"]*' | cut -d '"' -f1) +latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | jq -r '.tag_name') if [ -x "$brain_bin" ]; then if [[ "$(brain --version)" == *"$latest_version"* ]]; then