mj5s.sh is a Bash script designed to automate various tasks related to managing multiple Git repositories. It provides a set of functions to simplify tasks like cloning repositories, fetching updates, checking out branches, pushing changes, building JAR files, creating Docker images, and more.
The best thing is you can extend the functionalities by adding your own custom functions, taking advantage of built-in parsing and infrastructure automation.
./mj5s.sh pull -remote=gitea -branch=alpha./mj5s.sh push -remote=github -branch=alpha./mj5s.sh pull -remote=gitea -branch=alpha push -remote=github -branch=alpha./mj5s.sh _service -except=repo-test-1 jar image -prefix=noman5237/prod -tag=2.0.0./mj5s.sh _service -only=repo-test-1 jar image -prefix=noman5237/prod -tag=2.0.0The script defines a modular Bash framework for managing services, particularly for operations on Git repositories and Docker containers. Here's a high-level overview of its key components:
- Global Variables: Variables like
__args,__func, and arrays (__urls,__context_args) manage inputs and state. - Associative Arrays: Arrays like
__servicesmap repository names to URLs for service management.
- Standardized Output: Functions like
log,debug,warn, anderrorprovide color-coded output. - Verbose Control: Conditional aliasing controls verbosity via
LOG_LEVEL.
- Dynamically builds a list of services (
__services) from predefined Git repository URLs. - Filters services based on inclusion (
only) or exclusion (except) criteria.
- Supports operations like cloning, fetching, checking out branches, merging, pulling, tagging, and remote management.
- Automates tasks such as permission setting, Maven-based Java builds, and Docker image handling (e.g., build, push).
- Includes Docker registry login and image management commands.
- Functions like
capture_func_argsandcopy_to_context_argsparse and handle command-line arguments for flexibility. - Ensures smooth argument passing and context management.
- Processes function names and arguments sequentially.
- Handles service filtering before executing specific operations.
The script allows chaining commands for multi-step operations. For example:
./mj5s.sh -only=repo-test-1 clone checkout -branch=main- Filters services to include only repo-test-1.
- Clones the repository if not already cloned.
- Checks out the main branch in the cloned repository.
- Dynamic Service Filtering: Enables precise targeting of repositories for specific actions.
- Modularity: Functions like
_service,clone, andfetchcan be composed flexibly. - Custom Logging: Facilitates easy debugging and status tracking.
- Reusable Components: Easily extendable for additional services or operations.
The script supports dynamic input arguments in the -key=value format, making scripting functions highly customizable.
capture_func_args() {
__func_args=()
for (( ; __args_i < ${#__args[@]}; ++__args_i)); do
local arg="${__args[$__args_i]}"
debug "arg: $arg"
if [[ "$arg" == -* ]]; then
if [[ "$arg" == *"="* ]]; then
# Parse key-value pairs (-key=value)
local key="${arg%%=*}"
key="${key#-}"
local value="${arg#*=}"
__func_args["$key"]="$value"
else
# Parse flags (-key)
local key="$arg"
key="${key#-}"
__func_args["$key"]=true
fi
else
break
fi
done
}clone() {
: '
This function clones a repository based on the __services array.
If the -force argument is provided, the repository will be forcibly cloned.
Usage:
./mj5s.sh clone -force=true
'
if [ -z "${__func_args[force]}" ]; then
debug "force is unset, setting it to false"
__func_args[force]=false
fi
local force="${__func_args[force]}"
debug "force: $force"
for __service in "${!__services[@]}"; do
if [ "$force" == true ]; then
log "force cloning $__service"
rm -rf "$__service"
git clone "${__services[$__service]}"
elif [ ! -d "$__service" ]; then
log "cloning $__service"
git clone "${__services[$__service]}"
else
log "service $__service already exists!"
fi
done
}./mj5s.sh clone -force=truecapture_func_argsprocesses-force=trueand sets__func_args[force]totrue.- The
clonefunction reads__func_args[force]and decides whether to force clone the repositories.
- Dynamic Input Arguments: Use
-key=valueor-flagformats for flexible operations. - Extendable Functions: Customize behaviors using parsed arguments.
- Modular Design: Easily reusable components for a variety of tasks.