added subcommands structure
This commit is contained in:
parent
1bc69e350b
commit
2e6f71842f
85
rtenets
85
rtenets
|
@ -104,17 +104,17 @@ get_gitea_token(){
|
|||
else
|
||||
TOKEN="$(enter "gitea token: ")"
|
||||
fi
|
||||
readonly TOKEN
|
||||
}
|
||||
|
||||
gitea() {
|
||||
check_api_prerequisites
|
||||
readonly TOKEN
|
||||
|
||||
if ! is_set TOKEN; then
|
||||
get_gitea_token
|
||||
fi
|
||||
|
||||
readonly TOKEN
|
||||
|
||||
local method="$1"
|
||||
local request="$2"
|
||||
local data=""
|
||||
|
@ -172,6 +172,8 @@ init_repo() {
|
|||
\git init
|
||||
}
|
||||
|
||||
# ----------------- Tree commands -----------------------
|
||||
|
||||
# Make bash script stub
|
||||
script_stub() {
|
||||
local script_name="$1"
|
||||
|
@ -263,6 +265,8 @@ create_tenet_file_tree() {
|
|||
script_stub make_all_figures "Generating figures..."
|
||||
}
|
||||
|
||||
# ----------------- Gitea commands -----------------------
|
||||
|
||||
get_gitea_owner() {
|
||||
gitea /user GET | jq -r '.login'
|
||||
}
|
||||
|
@ -327,9 +331,25 @@ setup_dalembert_gitea() {
|
|||
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_repo() {
|
||||
create() {
|
||||
declare desc="usage: rtenets create <directory>"
|
||||
local repo_name="$1"
|
||||
info "recursively creating directory '${repo_name}'"
|
||||
mkdir -p "${repo_name}"
|
||||
|
@ -338,19 +358,70 @@ create_repo() {
|
|||
cd "${repo_name}"
|
||||
init_repo
|
||||
create_tenet_file_tree
|
||||
setup_dalembert_gitea
|
||||
)
|
||||
}
|
||||
|
||||
# Print usage and exit
|
||||
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() {
|
||||
check_git_config
|
||||
create_tenet_file_tree
|
||||
local other_args=()
|
||||
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 "$@"
|
||||
|
|
Loading…
Reference in New Issue