Skip to content

Commit

Permalink
feat: added a brain remote search (not good looking though) and some …
Browse files Browse the repository at this point in the history
…improvements
  • Loading branch information
AyushSehrawat committed May 25, 2023
1 parent b0d974e commit 2a7bcdd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
56 changes: 26 additions & 30 deletions brain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,31 +27,30 @@ 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"
}

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"
}

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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -101,45 +104,38 @@ 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

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
;;
Expand All @@ -160,15 +156,15 @@ 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

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

Expand All @@ -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
Expand All @@ -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
;;
Expand Down
13 changes: 12 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a7bcdd

Please sign in to comment.