added subcommands structure

This commit is contained in:
Lucas Frérot 2024-12-30 23:59:22 +01:00
parent 1bc69e350b
commit 2e6f71842f
No known key found for this signature in database
GPG Key ID: 03B54A50E3FBA7E8
1 changed files with 78 additions and 7 deletions

85
rtenets
View File

@ -104,17 +104,17 @@ get_gitea_token(){
else else
TOKEN="$(enter "gitea token: ")" TOKEN="$(enter "gitea token: ")"
fi fi
readonly TOKEN
} }
gitea() { gitea() {
check_api_prerequisites check_api_prerequisites
readonly TOKEN
if ! is_set TOKEN; then if ! is_set TOKEN; then
get_gitea_token get_gitea_token
fi fi
readonly TOKEN
local method="$1" local method="$1"
local request="$2" local request="$2"
local data="" local data=""
@ -172,6 +172,8 @@ init_repo() {
\git init \git init
} }
# ----------------- Tree commands -----------------------
# Make bash script stub # Make bash script stub
script_stub() { script_stub() {
local script_name="$1" local script_name="$1"
@ -263,6 +265,8 @@ create_tenet_file_tree() {
script_stub make_all_figures "Generating figures..." script_stub make_all_figures "Generating figures..."
} }
# ----------------- Gitea commands -----------------------
get_gitea_owner() { get_gitea_owner() {
gitea /user GET | jq -r '.login' gitea /user GET | jq -r '.login'
} }
@ -327,9 +331,25 @@ setup_dalembert_gitea() {
setup_software_heritage_hook "${owner}" "${repo_name}" "${remote_http}" setup_software_heritage_hook "${owner}" "${repo_name}" "${remote_http}"
} }
# ----------------- Script subcommands commands -----------------------
init-tree() {
declare desc="usage: rtenets init-tree <directory>"
local directory="$1"
}
init-gitea() {
true
}
init-workflow() {
true
}
# Create git repository # Create git repository
create_repo() { create() {
declare desc="usage: rtenets create <directory>"
local repo_name="$1" local repo_name="$1"
info "recursively creating directory '${repo_name}'" info "recursively creating directory '${repo_name}'"
mkdir -p "${repo_name}" mkdir -p "${repo_name}"
@ -338,19 +358,70 @@ create_repo() {
cd "${repo_name}" cd "${repo_name}"
init_repo init_repo
create_tenet_file_tree create_tenet_file_tree
setup_dalembert_gitea
) )
} }
# Print usage and exit # Print usage and exit
usage() { usage() {
true cat <<USAGE
usage: $0 [--help,-h] [--version] command [args...]
Available commands:
- create: create and populate a repository
- init-tree: populate a repository with README, AUTHORS, COPYING and test/
- init-gitea: setup a remote on a gitea server with SoftwareHeritage hook
- init-workflow: setup a Python virtual environment and Snakemake template
- check: verify tenent compliance for a repository
USAGE
}
version() {
cat <<VERSION
rtenets 0.0.1
Copyright © 2024 Lucas Frérot
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
under certain conditions;
VERSION
} }
main() { main() {
check_git_config local other_args=()
create_tenet_file_tree local help_mode=0
for i in "$@"; do
case $i in
-v|--version)
version
return 0
;;
-h|--help)
help_mode=1
;;
*)
other_args+=("${i}")
;;
esac
done
gitea /user GET local command="${other_args[0]}"
if [[ "${help_mode}" == 1 ]]; then
type "${command}" | sed -n -e 's/^.*declare desc="\(.*\)";/\1/p'
fi
# Subcommands trick
# https://blogsh.github.io/2020/03/21/subcommands-in-bash-scripts.html
declare -A COMMANDS=(
[default]=usage
[init-tree]=init-tree
[init-gitea]=init-gitea
[init-workflow]=init-workflow
[create]=create
)
"${COMMANDS[${command:-default}]:-${COMMANDS[default]}}" "${other_args}"
} }
main "$@" main "$@"