Skip to content

Commit

Permalink
feat(functions): adds curl or wget function #patch (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungstudios authored Oct 9, 2022
1 parent 5efbe53 commit 3bec592
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 29 deletions.
3 changes: 2 additions & 1 deletion bin/alfa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ void main(List<String> 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') &&
Expand Down
12 changes: 7 additions & 5 deletions functions/_example/install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 8 additions & 8 deletions functions/anaconda/install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion functions/brew/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
2 changes: 1 addition & 1 deletion functions/deb_package/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion functions/docker/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion functions/jetbrains_toolbox/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion functions/nvm/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion functions/ohmyzsh/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion functions/sdkman/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions tools/create_function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions tools/download.sh
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit 3bec592

Please sign in to comment.