From 3ce0431d2d8c21208cbfe36fb3cab436a6821d8e Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Sun, 6 Aug 2023 23:20:08 -0700 Subject: [PATCH] Feat: add help, improve CLI experience --- src/1-logr.sh | 2 +- src/2-init.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++----- src/3-loop.sh | 6 ++-- src/4-updt.sh | 2 +- src/5-main.sh | 2 +- 5 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/1-logr.sh b/src/1-logr.sh index f0a76ea..8676082 100644 --- a/src/1-logr.sh +++ b/src/1-logr.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash LPLUS="\e[0;33m[\e[0;32m+\e[0;33m]\e[0m" LFAIL="\e[0;33m[\e[1;31mx\e[0;33m]\e[0m" diff --git a/src/2-init.sh b/src/2-init.sh index 99267f1..cfb624e 100644 --- a/src/2-init.sh +++ b/src/2-init.sh @@ -1,22 +1,86 @@ -#!/usr/bin/env bash +#!/bin/bash + +function reqs() { + if ! command -v git &>/dev/null; then + fatal "please install git to use this tool" + fi +} + +function usage() { + echo -e "\n\t\tclone-all\neasily clone/update a target's github repos\n" + echo -e "usage: $(echo $0 | awk -F '/' '{print $NF}') [OPTION...] TARGET\n" + echo -e "OPTION\n" + echo -e " -v \e[90m--verbose\e[0m\n\tenable debug output" + echo -e " -d \e[90m--destination\e[0m\n\tset clone output folder" + echo -e " -s \e[90m--ssh\e[0m\n\tuse ssh instead of https" + echo -e " -h \e[90m--help\e[0m\n\tshow this help message" + echo -e " -t \e[90m--target\e[0m\n\talternative way to set TARGET explicitly" +} function setup() { + reqs shopt -s extglob _DESTINATION=${CLONEALL_DESTINATION:-"$HOME/Workshop"} - _DESTINATION="${_DESTINATION%%+(/)}" _SSH=${CLONEALL_SSH:-false} + _APIKEY=${GITHUB_TOKEN:-""} + _CONTEXT="users" if [ "$_DESTINATION" == "." ]; then _DESTINATION=$(pwd) fi - if [ "$1" == "--ssh" ]; then - _SSH=true - shift 1 + _HASTARG=false + while [[ $# -gt 0 ]]; do + case $1 in + "-v" | "--verbose") + _DEBUG=true + debug "debug mode enabled" + shift + ;; + "-d" | "--destination") + _DESTINATION="$1" + debug "_DESTINATION set to: $_DESTINATION" + shift + ;; + "-s" | "--ssh") + _SSH=true + debug "using ssh instead of https" + shift + ;; + "-h" | "--help") + usage + exit 0 + ;; + "-t" | "--target") + _USERNAME="$1" + debug "target set to: $_USERNAME" + shift + ;; + *) + if [[ "$1" == "-"* ]]; then + err "unknown argument: $1" + usage + exit 1 + fi + _USERNAME="$1" + debug "target set to: $_USERNAME" + shift + ;; + esac + done + + if [[ "$_USERNAME" == "" ]]; then + fatal "need target github user" + usage + exit 1 fi - _CONTEXT="users" - _USERNAME="$1" + _DESTINATION="${_DESTINATION%%+(/)}" + + export _CONTEXT + export _DESTINATION + export _SSH + export _USERNAME shift 1 debug "$(mkdir -vp "${_DESTINATION}/${_USERNAME}")" || return 1 } diff --git a/src/3-loop.sh b/src/3-loop.sh index c97e6ae..fade9a1 100644 --- a/src/3-loop.sh +++ b/src/3-loop.sh @@ -1,9 +1,11 @@ -#!/usr/bin/env bash +#!/bin/bash function get() { _PAGE=1 while :; do - _APIRES=$(curl -s "https://api.github.com/$_CONTEXT/$_USERNAME/repos?page=$_PAGE&per_page=100") + _MYURL="https://api.github.com/$_CONTEXT/$_USERNAME/repos?page=$_PAGE&per_page=100" + debug "GET $_MYURL" + _APIRES=$(curl -s "$_MYURL") if ! [[ "$_APIRES" =~ "clone_url" ]]; then if [[ "$_APIRES" =~ [a-zA-Z] ]]; then echo "$_APIRES" diff --git a/src/4-updt.sh b/src/4-updt.sh index d175b45..61fa96e 100644 --- a/src/4-updt.sh +++ b/src/4-updt.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash function update() { #debug "Update input: $1" diff --git a/src/5-main.sh b/src/5-main.sh index b1e6fec..5a7508d 100644 --- a/src/5-main.sh +++ b/src/5-main.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash if ! setup "$@"; then fatal "Failed to setup directory structure!"