Skip to content

Commit

Permalink
feat: add docker files and kiptup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 3, 2023
1 parent 0c2ca31 commit 75ee66c
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docker/Dockerfile.amd64
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM --platform=linux/amd64 alpine:latest

LABEL org.opencontainers.image.source=https://github.com/glihm/kipt

COPY --chmod=744 ./kipt /usr/bin/

ENTRYPOINT [ "kipt" ]
7 changes: 7 additions & 0 deletions docker/Dockerfile.arm64
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM --platform=linux/arm64/v8 alpine:latest

LABEL org.opencontainers.image.source=https://github.com/glihm/kipt

COPY --chmod=744 ./kipt /usr/bin/

ENTRYPOINT [ "kipt" ]
103 changes: 103 additions & 0 deletions kiptup/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/sh

set -e

# Fancy color setup:
# https://unix.stackexchange.com/questions/9957/how-to-check-if-bash-can-print-colors
if test -t 1; then
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi

insert_env_line() {
if [ -f "$1" ]; then
if [ -z "$(cat "$1" | grep "${ENV_LINE}")" ]; then
echo "${ENV_LINE}" >> "$1"
fi
fi
}

echo "Installing ${magenta}kiptup${normal}..."

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
KIPT_DIR=${KIPT_DIR-"$BASE_DIR/.kipt"}
KIPT_BIN_DIR="$KIPT_DIR/bin"
KIPT_MAN_DIR="$KIPT_DIR/share/man/man1"

BIN_URL="https://raw.githubusercontent.com/glihm/kipt/main/kiptup/kiptup"
BIN_PATH="$KIPT_BIN_DIR/kiptup"

ENV_PATH="$KIPT_DIR/env"


mkdir -p $KIPT_BIN_DIR
mkdir -p $KIPT_MAN_DIR

curl -# -L $BIN_URL -o $BIN_PATH
chmod +x $BIN_PATH

# Generates the env file on the fly
cat > $ENV_PATH <<EOF
#!/bin/sh
# Adds binary directory to PATH
case ":\${PATH}:" in
*:${KIPT_BIN_DIR}:*)
;;
*)
export PATH="${KIPT_BIN_DIR}:\$PATH"
;;
esac
EOF
chmod +x $ENV_PATH

# This detection here is just for showing the help message at the end.
IS_SUPPORTED_SHELL=""
if [ -n "$ZSH_NAME" ]; then
IS_SUPPORTED_SHELL="1"
fi
case $SHELL in
*/bash)
IS_SUPPORTED_SHELL="1"
;;
*/fish)
IS_SUPPORTED_SHELL="1"
;;
*/ash)
IS_SUPPORTED_SHELL="1"
;;
*/zsh)
IS_SUPPORTED_SHELL="1"
;;
esac

# Inserts this line into whatever shell profile we find, regardless of what the active shell is.
ENV_LINE=". \"${ENV_PATH}\""
insert_env_line "$HOME/.profile"
insert_env_line "$HOME/.bashrc"
insert_env_line "$HOME/.bash_profile"
insert_env_line "${ZDOTDIR-"$HOME"}/.zshenv"
insert_env_line "${ZDOTDIR-"$HOME"}/.zshrc"
insert_env_line "$HOME/.config/fish/config.fish"

echo

if [ -n "$IS_SUPPORTED_SHELL" ]; then
echo "Run '${yellow}. ${ENV_PATH}${normal}' or start a new terminal session to use kiptup."
echo "Then, simply run ${magenta}kiptup${normal} to install kipt."
else
echo "kiptup: could not detect shell. Add '${yellow}. ${ENV_PATH}${normal}' to your shell profile, or manually add '${yellow}${KIPT_BIN_DIR}${normal}' to your PATH environment variable."
fi
197 changes: 197 additions & 0 deletions kiptup/kiptup
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#!/bin/sh

set -eE

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
KIPT_DIR=${KIPT_DIR-"$BASE_DIR/.kipt"}
KIPT_BIN_DIR="$KIPT_DIR/bin"
KIPT_MAN_DIR="$KIPT_DIR/share/man/man1"

KIPT_BIN_PATH="${KIPT_BIN_DIR}/kipt"

# This MUST be updated whenever this file is changed.
# TODO: add CI check to ensure this.
KIPTUP_VERSION="2023-08-30"

# Fancy color setup:
# https://unix.stackexchange.com/questions/9957/how-to-check-if-bash-can-print-colors
if test -t 1; then
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi

main() {
RELEASE_TAG=latest

while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [ $# -gt 1 ]; then
case $2 in
"v"*)
RELEASE_TAG="$2"
;;
*)
RELEASE_TAG="v$2"
;;
esac
shift
else
echo "${red}Version tag is missing${normal}" 1>&2
exit 1
fi
;;
*)
echo "${red}Unknown option: $1${normal}" 1>&2
echo "Run ${yellow}kiptup --help${normal} to see usage." 1>&2
exit 1
;;
esac
shift
done

install

echo
echo "Installation successfully completed."
}

install() {
if [ $RELEASE_TAG = "latest" ]; then
echo "Installing the latest version of kipt..."
echo "Fetching the latest release tag from GitHub..."
RELEASE_TAG="$(curl -s "https://api.github.com/repos/xJonathanLEI/kipt/releases/latest" | grep "tag_name" | cut -d \" -f 4)"
echo "Latest version found: ${yellow}${RELEASE_TAG}${normal}"
else
echo "Installing version ${yellow}${RELEASE_TAG}${normal}..."
fi

detect_host_triple
if [ -z "$TRIPLE" ]; then
echo "${red}Unable to detect platform.${normal} Please install kipt from source." 1>&2
exit 1
fi

echo "Detected host triple: ${cyan}${TRIPLE}${normal}"

FILE_NAME="kipt-${TRIPLE}.tar.gz"
FILE_URL="https://github.com/xJonathanLEI/kipt/releases/download/${RELEASE_TAG}/${FILE_NAME}"

TEMP_DIR="$(mktemp -d)"
TEMP_FILE_NAME="${TEMP_DIR}/${FILE_NAME}"

# TODO: support wget if curl is not found
download_release_file

tar zxf $TEMP_FILE_NAME -C $TEMP_DIR

mv "${TEMP_DIR}/kipt" "${KIPT_BIN_PATH}"

rm -rf $TEMP_DIR

echo "Successfully installed kipt ${yellow}${LATEST_TAG}${normal}"
}

download_release_file() {
echo "Downloading release file from GitHub..."
if command -v curl >/dev/null 2>&1; then
HTTP_CODE="$(curl -# -L "$FILE_URL" -o "$TEMP_FILE_NAME" --write-out "%{http_code}")"
if [ "$HTTP_CODE" -ne 200 ]; then
echo "${red}Couldn't download release file for tag '${RELEASE_TAG}' from GitHub [${HTTP_CODE}].${normal}"
exit 1
fi
else
echo "${red}Command 'curl' is required.${normal}"
exit 1
fi
}

detect_host_triple() {
PLATFORM="$(uname -s)"
ARCHITECTURE="$(uname -m)"

case $PLATFORM in
Linux)
# Android Termux detection
case $PREFIX in
*com.termux*)
case $ARCHITECTURE in
aarch64|arm64)
TRIPLE="aarch64-linux-android"
;;
esac
;;
*)
# Likely very unreliable way to check musl
if [ -n "$(ls /lib | grep "libc.musl-")" ]; then
case $ARCHITECTURE in
x86_64)
TRIPLE="x86_64-unknown-linux-musl"
;;
aarch64|arm64)
TRIPLE="aarch64-unknown-linux-musl"
;;
esac
else
case $ARCHITECTURE in
x86_64)
TRIPLE="x86_64-unknown-linux-gnu"
;;
aarch64|arm64)
TRIPLE="aarch64-unknown-linux-gnu"
;;
esac
fi
esac
;;
Darwin)
case $ARCHITECTURE in
x86_64)
TRIPLE="x86_64-apple-darwin"
;;
aarch64|arm64)
TRIPLE="aarch64-apple-darwin"
;;
esac
;;
esac
}

usage() {
cat 1>&2 <<EOF
kiptup ${cyan}(${KIPTUP_VERSION})${normal}
The installer for kipt.
Install the latest version of kipt by running without any options.
More installation options will be implemented. To update kiptup itself, run the installation command again:
curl https://raw.githubusercontent.com/glihm/kipt/main/kiptup/install | sh
USAGE:
kiptup <OPTIONS>
OPTIONS:
-h, --help Print this message
-v, --version The version of kipt to install
EOF
}

main "$@"

0 comments on commit 75ee66c

Please sign in to comment.