starting writing tenet helper bash script
This commit is contained in:
parent
faa3858401
commit
61b4c80291
|
@ -0,0 +1,173 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Sane bash options
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
# Colors
|
||||
readonly RED='\033[0;31m'
|
||||
readonly ORANGE='\033[0;33m'
|
||||
readonly GREEN='\033[0;32m'
|
||||
readonly BLUE='\033[0;34m'
|
||||
readonly CYAN='\033[0;36m'
|
||||
readonly NC='\033[0m'
|
||||
|
||||
# Gitea
|
||||
readonly ENDPOINT="https://git.dalembert.upmc.fr/api/v1"
|
||||
|
||||
# ----------------- Logging commands -----------------------
|
||||
|
||||
# Print error and exit
|
||||
error() {
|
||||
printf "${RED}error${NC}: %s\\n" "$@" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Print warning
|
||||
warning() {
|
||||
printf "${ORANGE}warning${NC}: %s\\n" "$@" 1>&2
|
||||
}
|
||||
|
||||
# Print info
|
||||
info() {
|
||||
printf "${GREEN}info${NC}: %b\\n" "$@" 1>&2
|
||||
}
|
||||
|
||||
# Enter value
|
||||
enter() {
|
||||
printf "${BLUE}input${NC}: %b" "$@" 1>&2
|
||||
local input_var=''
|
||||
read input_var
|
||||
printf "${input_var}"
|
||||
}
|
||||
|
||||
|
||||
# Check that command exists
|
||||
has_command() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Check curl and jq to process API calls to gitea
|
||||
check_api_prerequisites() {
|
||||
if ! has_command curl; then
|
||||
error "curl not found, please install"
|
||||
fi
|
||||
|
||||
if ! has_command jq; then
|
||||
error "jq not found, please install"
|
||||
fi
|
||||
}
|
||||
|
||||
# ----------------- Gitea API commands -----------------------
|
||||
get_gitea_token(){
|
||||
if [ -f token ]; then
|
||||
read TOKEN < token
|
||||
else
|
||||
TOKEN="$(enter "gitea token: ")"
|
||||
fi
|
||||
readonly TOKEN
|
||||
info "gitea token: '${TOKEN}'"
|
||||
}
|
||||
|
||||
gitea() {
|
||||
check_api_prerequisites
|
||||
|
||||
if [ -z ${TOKEN+x} ]; then
|
||||
get_gitea_token
|
||||
fi
|
||||
|
||||
local method="$1"
|
||||
local request="$2"
|
||||
curl -X "${request}" -H "Content-Type: application/json" -H "Authorization: token ${TOKEN}" "${ENDPOINT}/${method}"
|
||||
}
|
||||
|
||||
# ----------------- Git commands -----------------------
|
||||
|
||||
# Set value of git config parameter
|
||||
set_git_config() {
|
||||
local param="$1"
|
||||
local value=''
|
||||
while ! [ -n "${value}" ]; do
|
||||
value="$(enter "new value for ${param}: ")"
|
||||
done
|
||||
\git config --global "${param}" "${value}"
|
||||
info "setting new value for ${param}: '$(git config "${param}")'"
|
||||
printf "${value}"
|
||||
}
|
||||
|
||||
# Get value of git config parameter, set if unset
|
||||
get_git_config() {
|
||||
local param="$1"
|
||||
local value="$(git config "${param}")"
|
||||
if ! [ -n "${value}" ]; then
|
||||
warning "git ${param} is unset"
|
||||
value="$(set_git_config "${param}")"
|
||||
fi
|
||||
printf "${value}"
|
||||
}
|
||||
|
||||
# Check git configuration and correct if necessary
|
||||
check_git_config() {
|
||||
if ! has_command git; then
|
||||
error "git not found, please install"
|
||||
fi
|
||||
|
||||
readonly USER="$(get_git_config user.name)"
|
||||
readonly EMAIL="$(get_git_config user.email)"
|
||||
info "found git credentials:\\n - user.name: '${USER}'\\n - user.email: '${EMAIL}'"
|
||||
}
|
||||
|
||||
# Initialize git repository
|
||||
init_repo() {
|
||||
check_git_config
|
||||
\git init
|
||||
}
|
||||
|
||||
# Create required files
|
||||
create_tenet_file_tree() {
|
||||
local readme="README.md~"
|
||||
|
||||
if ! [ -f "${readme}" ]; then
|
||||
local project_name="$(enter "project name: ")"
|
||||
local project_desc="$(enter "project short description: ")"
|
||||
|
||||
cat << READMEMSG > "${readme}"
|
||||
# ${project_name}
|
||||
|
||||
${project_desc}
|
||||
READMEMSG
|
||||
|
||||
info "wrote '${readme}'"
|
||||
else
|
||||
warning "found '${readme}', not touching"
|
||||
fi
|
||||
}
|
||||
|
||||
# Create git repository
|
||||
create_repo() {
|
||||
local repo_name="$1"
|
||||
info "recursively creating directory '${repo_name}'"
|
||||
mkdir -p "${repo_name}"
|
||||
(
|
||||
info "initializing repository '${repo_name}'"
|
||||
cd "${repo_name}"
|
||||
init_repo
|
||||
create_tenet_file_tree
|
||||
)
|
||||
}
|
||||
|
||||
# Print usage and exit
|
||||
usage() {
|
||||
true
|
||||
}
|
||||
|
||||
main() {
|
||||
check_git_config
|
||||
create_tenet_file_tree
|
||||
|
||||
get_gitea_token
|
||||
gitea /user GET
|
||||
}
|
||||
|
||||
main "$@"
|
Loading…
Reference in New Issue