From 943b36722c80a4e188ae87a659102e84c7a885b8 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 18 Jan 2022 15:54:39 -0600 Subject: [PATCH] refactor tools scripts as installed binary with better defaults --- home/private_bin/executable_tools | 183 ++++++++++++++++++++++++++++++ info/tools.yml | 3 + tools.sh | 86 -------------- 3 files changed, 186 insertions(+), 86 deletions(-) create mode 100755 home/private_bin/executable_tools delete mode 100755 tools.sh diff --git a/home/private_bin/executable_tools b/home/private_bin/executable_tools new file mode 100755 index 0000000..5a44411 --- /dev/null +++ b/home/private_bin/executable_tools @@ -0,0 +1,183 @@ +#!/usr/bin/env bash + +BIN_DIR=$HOME/bin +mkdir -p $BIN_DIR +YAMLDOC="$DOTFILES_DIR/info/tools.yml" + +#colors +MAGENTA=$(tput setaf 5) +CYAN=$(tput setaf 6) +YELLOW=$(tput setaf 3) +RED=$(tput setaf 1) +GREEN=$(tput setaf 2) +NORMAL=$(tput sgr0) + +alias eget="eget --system linux/amd64" + +post_download_install() { + + tool=$1 + temp_file=$(mktemp -p . ${tool}.XXX.sh) + key=$tool yq e 'explode(.) | .[env(key)].post-download' $YAMLDOC >$temp_file + . $temp_file + rm $temp_file + +} + +eget_tool() { + + tool=$1 + + echo -n $tool + + user=$(key=$tool yq e 'explode(.) | .[env(key)].user' $YAMLDOC) + asset=$(key=$tool yq e 'explode(.) | .[env(key)].asset // ""' $YAMLDOC) + file=$(key=$tool yq e 'explode(.) | .[env(key)].file // ""' $YAMLDOC) + to=$(key=$tool yq e 'explode(.) | .[env(key)].to // ""' $YAMLDOC) + download_only=$(key=$tool yq e 'explode(.) | .[env(key)].download-only // ""' $YAMLDOC) + + eget $user/$tool \ + ${asset:+--asset $asset} \ + ${file:+--file $file} \ + ${to:+--to $to} \ + ${download_only:+--download-only} \ + -q + + if [[ $download_only ]]; then + echo -n ' --> ' + echo -n 'running post-download script ' + post_download_install $tool + fi + + echo +} + +deps_check() { + + if ! $(is-executable eget); then + echo "I don't see eget on your path..." + read -p "Do you want to download it to ${BIN_DIR}? " -n 1 -r + echo # (optional) move to a new line + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + else + curl https://zyedidia.github.io/eget.sh | sh + mv eget $BIN_DIR/eget + fi + fi + + if ! $(is-executable yq); then + eget mikefarah/yq --asset yq_linux_amd64 + fi + +} + +list() { + deps_check + + user_len=$(yq e '.[] | .user ' $YAMLDOC | tail -n +2 | sort | uniq | awk '{print length}' | sort -nr | head -n 1) + tool_len=$(yq e 'keys| .[]' $YAMLDOC | tail -n +2 | sort | uniq | awk '{print length}' | sort -nr | head -n 1) + table_width=$((user_len + tool_len + 4)) + cell_width=$((table_width / 2 - 2)) + cell=$(printf "%${cell_width}s" "") + + echo "${YELLOW}Configured Tools${NORMAL}" + printf "%${table_width}s\n" "" | tr ' ' = + printf "${CYAN}tool${NORMAL} %s | ${CYAN}author${NORMAL}\n" "${cell:5}" + printf "%${table_width}s\n" "" | tr ' ' - + readarray tools < <(yq e 'keys | .[]' $YAMLDOC) + + #sort tools for readability + tools=($(echo "${tools[@]:1}" | tr ' ' '\n' | sort | uniq)) + for tool in "${tools[@]}"; do + user=$(key=$tool yq e 'explode(.) | .[env(key)].user' $YAMLDOC) + printf "${MAGENTA}%s${NORMAL}%s | ${GREEN}$user${NORMAL}\n" $tool "${cell:${#tool}}" + + done + +} + +download() { + if ! [ -z "$1" ]; then + tool=$1 + info=$(key=$tool yq e 'explode(.) | .[env(key)]' $YAMLDOC) + if [[ $info == "null" ]]; then + echo "$tool not found in $YAMLDOC" + echo "exiting" + exit 1 + else + eget_tool $tool + fi + fi +} + +download_all() { + + echo "Downloading binaries to ${GREEN}$BIN_DIR${NORMAL}" + + deps_check + + readarray tools < <(yq e 'keys | .[]' $YAMLDOC) + echo "installing......" + + for tool in "${tools[@]:1}"; do + eget_tool $tool + done + +} + +help() { + cat <$temp_file - . $temp_file - rm $temp_file - -} - -eget_tool() { - - tool=$1 - - echo -n $tool - - user=$(key=$tool yq e 'explode(.) | .[env(key)].user' $YAMLDOC) - asset=$(key=$tool yq e 'explode(.) | .[env(key)].asset // ""' $YAMLDOC) - file=$(key=$tool yq e 'explode(.) | .[env(key)].file // ""' $YAMLDOC) - to=$(key=$tool yq e 'explode(.) | .[env(key)].to // ""' $YAMLDOC) - download_only=$(key=$tool yq e 'explode(.) | .[env(key)].download-only // ""' $YAMLDOC) - - eget $user/$tool \ - ${asset:+--asset $asset} \ - ${file:+--file $file} \ - ${to:+--to $to} \ - ${download_only:+--download-only} \ - -q - - if [[ $download_only ]]; then - echo -n ' --> ' - echo -n 'running post-download script' - post_download_install $tool - fi - - echo -} - -if ! $(is-executable eget); then - echo "I don't see eget on your path..." - read -p "Do you want to download it to ${BIN_DIR}? " -n 1 -r - echo # (optional) move to a new line - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - exit 1 - else - curl https://zyedidia.github.io/eget.sh | sh - mv eget $BIN_DIR/eget - fi -fi - -echo "downloading binaries from github to $EGET_BIN" - -if ! $(is-executable yq); then - eget mikefarah/yq --asset yq_linux_amd64 -fi - -if ! [ -z "$1" ]; then - tool=$1 - - echo "installing $tool" - info=$(key=$tool yq e 'explode(.) | .[env(key)]' $YAMLDOC) - if [[ $info == "null" ]]; then - echo "$tool not found in $YAMLDOC" - echo "exiting" - exit 1 - else - eget_tool $tool - fi - exit -fi - -echo "no tool specified installing everything" - -readarray tools < <(yq e 'keys | .[]' $YAMLDOC) - -for tool in "${tools[@]:1}"; do - eget_tool $tool -done