This commit is contained in:
a 2024-05-01 19:43:43 -05:00
parent 69392aa7f1
commit c8f01255ae
Signed by: a
GPG Key ID: 374BC539FE795AF0
8 changed files with 112 additions and 22 deletions

View File

@ -1,10 +1,16 @@
.PHONY: all .PHONY: all install
SOURCES_LIBS:=$(shell find lib -type f) SOURCES_LIBS:=$(shell find lib -type f)
SOURCES_SRC:=$(shell find src -type f ) SOURCES_SRC:=$(shell find src -type f )
INSTALL_PATH:= $(shell echo ${HOME}/repo/.bin)
all: repotool all: repotool
install: repotool
mkdir -p ${INSTALL_PATH}
install repotool repotool.zsh ${INSTALL_PATH}
src/lib/repotool/stdlib.sh: $(SOURCES_LIBS) src/lib/repotool/stdlib.sh: $(SOURCES_LIBS)
bashly add --source . stdlib -f bashly add --source . stdlib -f

View File

@ -34,5 +34,14 @@ lecho()
echo $@ >&2 echo $@ >&2
} }
linspect()
{
if [[ -z "$DEBUG_LOG" || "$DEBUG_LOG" == 0 ]]; then
return 0
fi
inspect_args>&2
}

View File

@ -53,7 +53,7 @@ repotool_usage() {
printf "%s\n" "Environment Variables:" printf "%s\n" "Environment Variables:"
# :environment_variable.usage # :environment_variable.usage
printf " %s\n" "REPOS_PATH" printf " %s\n" "REPOTOOL_PATH"
printf " default path to clone to\n" printf " default path to clone to\n"
printf " Default: $HOME/repo\n" printf " Default: $HOME/repo\n"
echo echo
@ -247,13 +247,23 @@ lecho()
echo $@ >&2 echo $@ >&2
} }
linspect()
{
if [[ -z "$DEBUG_LOG" || "$DEBUG_LOG" == 0 ]]; then
return 0
fi
inspect_args>&2
}
# :command.command_functions # :command.command_functions
# :command.function # :command.function
repotool_get_command() { repotool_get_command() {
# src/get_command.sh # src/get_command.sh
#inspect_args>&2
local resp
linspect
local resp
resp=$(valid_url ${args[repo]}) resp=$(valid_url ${args[repo]})
if [[ $resp == -1 ]]; then if [[ $resp == -1 ]]; then
echo "${args[repo]} is not a valid repo" echo "${args[repo]} is not a valid repo"
@ -262,6 +272,10 @@ repotool_get_command() {
local regex="${deps[perl]} -n -l -e" local regex="${deps[perl]} -n -l -e"
local git="${deps[git]}"
local base_path=$REPOTOOL_PATH
local ssh_user; local ssh_user;
# the ssh user to clone with # the ssh user to clone with
@ -315,8 +329,7 @@ http_user: $http_user
http_pass: $http_pass http_pass: $http_pass
EOF EOF
local target_dir="$REPOS_PATH/$domain/$path" local target_dir="$base_path/$domain/$path"
#mkdir for our target directory
if [[ ! -d $target_dir ]]; then if [[ ! -d $target_dir ]]; then
mkdir -p $target_dir mkdir -p $target_dir
@ -335,23 +348,26 @@ EOF
repo_url="$method://$domain/$path.git" repo_url="$method://$domain/$path.git"
;; ;;
*) *)
>&2 echo "unrecognized clone method $method" echo "unrecognized clone method $method"
exit 1 exit 1
esac esac
# we check if we have cloned the repo via the if the .git folder exists # we check if we have cloned the repo via the if the .git folder exists
if [[ ! -d .git ]]; then if [[ ! -d .git ]]; then
# check if the remote actually exists # check if the remote actually exists
git ls-remote $repo_url > /dev/null && RC=$? || RC=$? $git ls-remote $repo_url > /dev/null && RC=$? || RC=$?
if [[ $RC == 0 ]]; then if [[ $RC == 0 ]]; then
git clone $repo_url $target_dir >&2 $git clone $repo_url $target_dir >&2
else else
>&2 echo "Could not find repo: $repo_url" echo "Could not find repo: $repo_url"
exit 1
fi fi
fi fi
echo dir=$target_dir domain=$domain path=$path repo_url=$repo_url echo dir=$target_dir domain=$domain path=$path repo_url=$repo_url
exit 0
} }
# :command.parse_requirements # :command.parse_requirements
@ -379,10 +395,10 @@ parse_requirements() {
# :command.environment_variables_filter # :command.environment_variables_filter
# :command.environment_variables_default # :command.environment_variables_default
export REPOS_PATH="${REPOS_PATH:-$HOME/repo}" export REPOTOOL_PATH="${REPOTOOL_PATH:-$HOME/repo}"
export DEBUG_LOG="${DEBUG_LOG:-0}" export DEBUG_LOG="${DEBUG_LOG:-0}"
env_var_names+=("REPOS_PATH") env_var_names+=("REPOTOOL_PATH")
env_var_names+=("DEBUG_LOG") env_var_names+=("DEBUG_LOG")
# :command.command_filter # :command.command_filter
@ -579,7 +595,7 @@ initialize() {
set -e set -e
# :command.environment_variables_default # :command.environment_variables_default
export REPOS_PATH="${REPOS_PATH:-$HOME/repo}" export REPOTOOL_PATH="${REPOTOOL_PATH:-$HOME/repo}"
export DEBUG_LOG="${DEBUG_LOG:-0}" export DEBUG_LOG="${DEBUG_LOG:-0}"
} }

5
repotool.plugin.zsh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env zsh
[[ -z "$REPOTOOL_PATH" ]] && export REPOTOOL_PATH="$HOME/repo"
alias repo=". $REPOTOOL_PATH/.bin/repotool.zsh"

38
repotool.zsh Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env zsh
[[ -z "$REPOTOOL_PATH" ]] && export REPOTOOL_PATH="$HOME/repo"
_activate_hook() {
if [[ -f "$REPOTOOL_PATH/.hooks/$1.zsh" ]]; then
. "$REPOTOOL_PATH/.hooks/$1.zsh" $2
return 0
fi
if [[ -f "$REPOTOOL_PATH/.hooks/$1.sh" ]]; then
. "$REPOTOOL_PATH/.hooks/$1.sh" $2
return 0
fi
if [[ -f "$REPOTOOL_PATH/.hooks/$1" ]]; then
. "$REPOTOOL_PATH/.hooks/$1" $2
return 0
fi
return 1
}
case "$1" in
get)
shift;
response=$($REPOTOOL_PATH/.bin/repotool get $@)
if [[ $? != 0 ]] then;
exit $?
fi
declare -A obj
for item in ${(z)response}; do
parts=(${(s[=])item})
# NOTE: zsh is 1 indexed arrays
obj[${parts[1]}]=${parts[2]}
done
_activate_hook "before_cd" $response
cd ${obj[dir]}
_activate_hook "after_cd" $response
;;
'help' | "-h"| "-help" | "--help")
esac

View File

@ -3,7 +3,7 @@ help: repo tool
version: 0.1.0 version: 0.1.0
environment_variables: environment_variables:
- name: REPOS_PATH - name: REPOTOOL_PATH
default: $HOME/repo default: $HOME/repo
help: default path to clone to help: default path to clone to
- name: DEBUG_LOG - name: DEBUG_LOG

View File

@ -1,6 +1,7 @@
#inspect_args>&2
local resp
linspect
local resp
resp=$(valid_url ${args[repo]}) resp=$(valid_url ${args[repo]})
if [[ $resp == -1 ]]; then if [[ $resp == -1 ]]; then
echo "${args[repo]} is not a valid repo" echo "${args[repo]} is not a valid repo"
@ -9,6 +10,10 @@ fi
local regex="${deps[perl]} -n -l -e" local regex="${deps[perl]} -n -l -e"
local git="${deps[git]}"
local base_path=$REPOTOOL_PATH
local ssh_user; local ssh_user;
# the ssh user to clone with # the ssh user to clone with
@ -65,8 +70,7 @@ http_pass: $http_pass
EOF EOF
local target_dir="$REPOS_PATH/$domain/$path" local target_dir="$base_path/$domain/$path"
#mkdir for our target directory
if [[ ! -d $target_dir ]]; then if [[ ! -d $target_dir ]]; then
mkdir -p $target_dir mkdir -p $target_dir
@ -85,7 +89,7 @@ case $method in
repo_url="$method://$domain/$path.git" repo_url="$method://$domain/$path.git"
;; ;;
*) *)
>&2 echo "unrecognized clone method $method" echo "unrecognized clone method $method"
exit 1 exit 1
esac esac
@ -94,12 +98,15 @@ esac
# we check if we have cloned the repo via the if the .git folder exists # we check if we have cloned the repo via the if the .git folder exists
if [[ ! -d .git ]]; then if [[ ! -d .git ]]; then
# check if the remote actually exists # check if the remote actually exists
git ls-remote $repo_url > /dev/null && RC=$? || RC=$? $git ls-remote $repo_url > /dev/null && RC=$? || RC=$?
if [[ $RC == 0 ]]; then if [[ $RC == 0 ]]; then
git clone $repo_url $target_dir >&2 $git clone $repo_url $target_dir >&2
else else
>&2 echo "Could not find repo: $repo_url" echo "Could not find repo: $repo_url"
exit 1
fi fi
fi fi
echo dir=$target_dir domain=$domain path=$path repo_url=$repo_url echo dir=$target_dir domain=$domain path=$path repo_url=$repo_url
exit 0

View File

@ -34,5 +34,14 @@ lecho()
echo $@ >&2 echo $@ >&2
} }
linspect()
{
if [[ -z "$DEBUG_LOG" || "$DEBUG_LOG" == 0 ]]; then
return 0
fi
inspect_args>&2
}