diff --git a/bin/alfa.dart b/bin/alfa.dart index 0021ae1..fdffc1a 100644 --- a/bin/alfa.dart +++ b/bin/alfa.dart @@ -196,7 +196,8 @@ void main(List args) async { var functionName = functionMap['install_function']; - String command = 'source functions/$baseName/install.sh; $functionName'; + String command = + 'source tools/download.sh; source functions/$baseName/install.sh; $functionName'; // checks if there are any options to pass when installing this if (config[name].containsKey('options') && diff --git a/functions/_example/install.sh b/functions/_example/install.sh index 625a099..0ea0504 100755 --- a/functions/_example/install.sh +++ b/functions/_example/install.sh @@ -1,11 +1,13 @@ #!/bin/bash # A bash script to install something -# Do not name the function to install something the same as an existing command. For example, the function to install brew -# is not called "brew", but rather "install_brew". -# To access the list of "options" in the config.toml file you pass to the installer, use the "$@" variable. -# To access the user that called the installer, do not user the environment variable "$SUDO_USER", but rather use "$ALFA_USER". -# To access the uname -m output (system architecture), you can use the environment variable "$ALFA_ARCH" +# 1. Do not name the function to install something the same as an existing command. For example, the function to install brew +# is not called "brew", but rather "install_brew". +# 2. To access the list of "options" in the config.toml file you pass to the installer, use the "$@" variable. +# 3. To access the user that called the installer, do not user the environment variable "$SUDO_USER", but rather use "$ALFA_USER". +# 4. To access the uname -m output (system architecture), you can use the environment variable "$ALFA_ARCH" +# 5. To download a file from the web, you can use the curl_or_wget function (tools/download.sh) which will use curl or wget depending upon +# what the user has installed. install_example() { # example function diff --git a/functions/anaconda/install.sh b/functions/anaconda/install.sh index c01ed38..e3f3293 100755 --- a/functions/anaconda/install.sh +++ b/functions/anaconda/install.sh @@ -1,12 +1,5 @@ #!/bin/bash -install_anaconda3_macos() { - # installs anaconda3 - echo "Installing Anaconda3..." - curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-${ALFA_ARCH}.sh}" -o ~/anaconda3.sh - install_anaconda3_common -} - install_anaconda3_common() { chmod +x ~/anaconda3.sh bash ~/anaconda3.sh -b -p ~/anaconda3 @@ -23,9 +16,16 @@ install_anaconda3_common() { fi } +install_anaconda3_macos() { + # installs anaconda3 + echo "Installing Anaconda3..." + curl_or_wget "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-${ALFA_ARCH}.sh}" ~/anaconda3.sh + install_anaconda3_common +} + install_anaconda3_linux() { # installs anaconda3 echo "Installing Anaconda3..." - curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-${ALFA_ARCH}.sh}" -o ~/anaconda3.sh + curl_or_wget "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-${ALFA_ARCH}.sh}" ~/anaconda3.sh install_anaconda3_common } diff --git a/functions/brew/install.sh b/functions/brew/install.sh index 325f279..08830a9 100755 --- a/functions/brew/install.sh +++ b/functions/brew/install.sh @@ -3,5 +3,5 @@ install_brew() { # Installs homebrew NONINTERACTIVE=1 - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + /bin/bash -c "$(curl_or_wget -s https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" } diff --git a/functions/deb_package/install.sh b/functions/deb_package/install.sh index eea7fc7..75726f4 100755 --- a/functions/deb_package/install.sh +++ b/functions/deb_package/install.sh @@ -3,7 +3,7 @@ install_deb_package() { # installs a deb package filepath="/tmp/file_to_install.deb" - wget -O "$filepath" "$@" + curl_or_wget "$1" "$filepath" apt-get install -y "$filepath" rm -f "$filepath" } diff --git a/functions/docker/install.sh b/functions/docker/install.sh index a12fd45..676849b 100755 --- a/functions/docker/install.sh +++ b/functions/docker/install.sh @@ -5,7 +5,7 @@ install_docker() { apt-get update apt-get install -y --no-install-recommends ca-certificates curl gnupg lsb-release mkdir -p /etc/apt/keyrings - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + curl_or_wget -s https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null apt-get update apt-get install -y --no-install-recommends docker-ce docker-ce-cli containerd.io diff --git a/functions/jetbrains_toolbox/install.sh b/functions/jetbrains_toolbox/install.sh index 46325de..4b02cb2 100755 --- a/functions/jetbrains_toolbox/install.sh +++ b/functions/jetbrains_toolbox/install.sh @@ -6,7 +6,7 @@ install_jetbrains_toolbox() { output_folder="/tmp/jetbrains-toolbox-tmp" version="jetbrains-toolbox-1.25.12627" mkdir -p "$output_folder" - wget -O "$filepath" "https://download.jetbrains.com/toolbox/${version}.tar.gz" + curl_or_wget "https://download.jetbrains.com/toolbox/${version}.tar.gz" "$filepath" tar -xvf "$filepath" -C "$output_folder" pushd "${output_folder}/${version}" ./jetbrains-toolbox diff --git a/functions/nvm/install.sh b/functions/nvm/install.sh index af00030..1dfff8b 100755 --- a/functions/nvm/install.sh +++ b/functions/nvm/install.sh @@ -6,5 +6,5 @@ install_nvm() { # nvm installer will not change your profile or rc file. export PROFILE="/dev/null" fi - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash + curl_or_wget https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash } diff --git a/functions/ohmyzsh/install.sh b/functions/ohmyzsh/install.sh index 4b191db..7a900a2 100755 --- a/functions/ohmyzsh/install.sh +++ b/functions/ohmyzsh/install.sh @@ -2,5 +2,5 @@ install_ohmyzsh() { # install ohmyzsh - sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended + sh -c "$(curl_or_wget -s https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended } diff --git a/functions/sdkman/install.sh b/functions/sdkman/install.sh index e6f6d82..2c11d77 100755 --- a/functions/sdkman/install.sh +++ b/functions/sdkman/install.sh @@ -2,7 +2,7 @@ install_sdkman() { # installs sdkman - curl -s "https://get.sdkman.io?rcupdate=false" | bash + curl_or_wget -s "https://get.sdkman.io?rcupdate=false" | bash if [[ -f "$HOME/.bashrc" ]]; then cat templates/sdkman.zsh >> ~/.bashrc fi diff --git a/install.sh b/install.sh index 8652bc2..539df7d 100755 --- a/install.sh +++ b/install.sh @@ -41,14 +41,7 @@ if [[ ! -f "$alfaCommand" ]]; then url="https://github.com/nyoungstudios/alfa/releases/download/${version}/${alfaCommand}" - if command -v "curl" > /dev/null 2>&1; then - curl -sL "$url" -o "$alfaCommand" - elif command -v "wget" > /dev/null 2>&1; then - wget -q "$url" -O "$alfaCommand" - else - echo "Must have curl or wget installed" - exit 1 - fi + source tools/download.sh; curl_or_wget -s "$url" "$alfaCommand" chmod +x "$alfaCommand" diff --git a/tools/create_function.sh b/tools/create_function.sh index 932db45..9f60db2 100755 --- a/tools/create_function.sh +++ b/tools/create_function.sh @@ -14,6 +14,11 @@ if [[ -z "${1:-}" ]]; then exit 1 fi +if [[ "$1" == "-h" || "$1" == "--help" ]]; then + echo "Usage: ./tools/create_function.sh entry_name" + exit 0 +fi + folder="functions/$1" if [[ -f "$folder/install.sh" || -f "$folder/config.toml" || -f "$folder/README.md" ]]; then diff --git a/tools/download.sh b/tools/download.sh new file mode 100755 index 0000000..64ebaaa --- /dev/null +++ b/tools/download.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +set -eu + +curl_or_wget() { + # downloads file from url with curl or wget + # Usage: curl_or_wget [-s] url [output file] + # -s is for silent mode + # url is the url to download + # output file is where to save the url contents to. Will output to stdout if not provided. + + # flags + silent=0 + + # sets input arguments + newArgs=() + + for arg in "$@" + do + if [[ "$arg" == "-s" ]]; + then + silent=1 + else + newArgs+=("$arg") + fi + done + + set -- "${newArgs[@]}" + + # sets positional arguments + url="${1:-}" + output="${2:-}" + + if [[ -z "$url" ]]; + then + echo "Must provide url" + exit 1 + fi + + commandToRun="" + + if command -v "curl" > /dev/null 2>&1; then + # if curl is installed + commandToRun="curl -L" + if [[ "$silent" == 1 ]]; + then + commandToRun="$commandToRun -fsS" + fi + + if ! [[ -z "$output" ]]; + then + commandToRun="$commandToRun -o '$output'" + fi + elif command -v "wget" > /dev/null 2>&1; then + # if wget is installed + commandToRun="wget" + if [[ "$silent" == 1 ]]; + then + commandToRun="$commandToRun -q" + fi + + if [[ -z "$output" ]]; + then + commandToRun="$commandToRun -O-" + else + commandToRun="$commandToRun -O '$output'" + fi + else + echo "Must have curl or wget installed" + exit 1 + fi + + commandToRun="$commandToRun '$url'" + + eval $commandToRun + +}