Skip to content

Commit

Permalink
Add get script
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Gee <[email protected]>
  • Loading branch information
rgee0 committed Dec 3, 2023
1 parent 006e1a0 commit c72fc1e
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# snowmachine

A Go port of the python script by (John Anderson)[https://github.com/sontek/snowmachine] that brings a little festive cheer to your terminal. View it in action here:
A Go port of the python script by [John Anderson](https://github.com/sontek/snowmachine) that brings a little festive cheer to your terminal. View it in action here:

![Rainbow ascii tree with snowfall](./images/animatedtree.gif)

## Installation

### Linux / MacOs

Getting Started
```sh
curl -sLS https://raw.githubusercontent.com/rgee0/snowmachine/main/get.sh | sudo sh
```

## Getting Started
---------------
You can make it snow:

```bash
$ snowmachine snow

or using the alias `sm`

$ sm snow
```
![Example of snow command](./images/snow.png)

Expand Down
185 changes: 185 additions & 0 deletions get.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/usr/bin/env bash

export VERIFY_CHECKSUM=0
export ALIAS_NAME="sm"
export OWNER=rgee0
export REPO=snowmachine
export BINLOCATION="/usr/local/bin"

###############################
# Content common across repos #
###############################

version=$(curl -sI https://github.com/$OWNER/$REPO/releases/latest | grep -i "location:" | awk -F"/" '{ printf "%s", $NF }' | tr -d '\r')
if [ ! $version ]; then
echo "Failed while attempting to install $REPO. Please manually install:"
echo ""
echo "1. Open your web browser and go to https://github.com/$OWNER/$REPO/releases"
echo "2. Download the latest release for your platform. Call it '$REPO'."
echo "3. chmod +x ./$REPO"
echo "4. mv ./$REPO $BINLOCATION"
if [ -n "$ALIAS_NAME" ]; then
echo "5. ln -sf $BINLOCATION/$REPO /usr/local/bin/$ALIAS_NAME"
fi
exit 1
fi

hasCli() {

hasCurl=$(which curl)
if [ "$?" = "1" ]; then
echo "You need curl to use this script."
exit 1
fi
}

checkHash(){

sha_cmd="sha256sum"

if [ ! -x "$(command -v $sha_cmd)" ]; then
sha_cmd="shasum -a 256"
fi

if [ -x "$(command -v $sha_cmd)" ]; then

targetFileDir=${targetFile%/*}

(cd "$targetFileDir" && curl -sSL $url.sha256|$sha_cmd -c >/dev/null)

if [ "$?" != "0" ]; then
rm "$targetFile"
echo "Binary checksum didn't match. Exiting"
exit 1
fi
fi
}

getPackage() {
uname=$(uname)
userid=$(id -u)

suffix=""
case $uname in
"Darwin")
arch=$(uname -m)
case $arch in
"x86_64")
suffix="-darwin"
;;
esac
case $arch in
"arm64")
suffix="-darwin-arm64"
;;
esac
;;

"MINGW"*)
suffix=".exe"
BINLOCATION="$HOME/bin"
mkdir -p $BINLOCATION

;;
"Linux")
arch=$(uname -m)
echo $arch
case $arch in
"aarch64")
suffix="-arm64"
;;
esac
case $arch in
"armv6l" | "armv7l")
suffix="-armhf"
;;
esac
;;
esac

targetFile="/tmp/$REPO$suffix"

if [ "$userid" != "0" ]; then
targetFile="$(pwd)/$REPO$suffix"
fi

if [ -e "$targetFile" ]; then
rm "$targetFile"
fi

url=https://github.com/$OWNER/$REPO/releases/download/$version/$REPO$suffix
echo "Downloading package $url as $targetFile"

curl -sSL $url --output "$targetFile"

if [ "$?" = "0" ]; then

if [ "$VERIFY_CHECKSUM" = "1" ]; then
checkHash
fi

chmod +x "$targetFile"

echo "Download complete."

if [ ! -w "$BINLOCATION" ]; then

echo
echo "============================================================"
echo " The script was run as a user who is unable to write"
echo " to $BINLOCATION. To complete the installation the"
echo " following commands may need to be run manually."
echo "============================================================"
echo
echo " sudo cp $REPO$suffix $BINLOCATION/$REPO"

if [ -n "$ALIAS_NAME" ]; then
echo " sudo ln -sf $BINLOCATION/$REPO $BINLOCATION/$ALIAS_NAME"
fi

echo

else

echo
echo "Running with sufficient permissions to attempt to move $REPO to $BINLOCATION"

if [ ! -w "$BINLOCATION/$REPO" ] && [ -f "$BINLOCATION/$REPO" ]; then

echo
echo "================================================================"
echo " $BINLOCATION/$REPO already exists and is not writeable"
echo " by the current user. Please adjust the binary ownership"
echo " or run sh/bash with sudo."
echo "================================================================"
echo
exit 1

fi

mv "$targetFile" $BINLOCATION/$REPO

if [ "$?" = "0" ]; then
echo "New version of $REPO installed to $BINLOCATION"
fi

if [ -e "$targetFile" ]; then
rm "$targetFile"
fi

if [ $(which $ALIAS_NAME) ]; then
echo "There is already a command '$ALIAS_NAME' in the path, NOT creating alias"
else
if [ -n "$ALIAS_NAME" ]; then
if [ ! -L $BINLOCATION/$ALIAS_NAME ]; then
ln -s $BINLOCATION/$REPO $BINLOCATION/$ALIAS_NAME
echo "Creating alias '$ALIAS_NAME' for '$REPO'."
fi
fi
fi
fi
fi
}

hasCli
getPackage

0 comments on commit c72fc1e

Please sign in to comment.