Last active
September 17, 2024 03:11
-
-
Save Silva97/5d32064b0a1f7f0cb45c0cd37bff0c29 to your computer and use it in GitHub Desktop.
Create alias commands to run inside a Docker environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script migrated to repository: https://github.com/freedev-org/dalias | |
CONFIG_DIR=~/.dalias | |
CONFIG_FILE="$CONFIG_DIR/config" | |
ALIAS_DIR="$CONFIG_DIR/bin" | |
DOCKER_CMD='docker run -it --rm -v $(pwd):/host -w /host' | |
BGDOCKER_CMD='docker run -id --rm -v $(pwd):/host -w /host' | |
DEFAULT_VALUE="bitnami/minideb:latest bash" | |
dry_mode=false | |
delete_mode=false | |
function show_help() { | |
cat <<:EOF | |
Developed by Luiz Felipe Silva <[email protected]> | |
Create alias commands to run inside a Docker environment. | |
USAGE | |
dalias [options] [name[=value] ... ] | |
-h,--help Show this help message. | |
-p,--print Print all defined dalias. | |
-d,--dry Don't set a new dalias, just show the command that will be | |
used to run the dalias after create it. You can use this | |
option to check if your dalias is set properly. | |
-r,--reload Update the daliases from the configuration file. | |
-D,--delete Delete all the given daliases. | |
FIRST STEPS | |
To be able to use the created daliases, you should add the following | |
directory to your PATH variable: | |
$ALIAS_DIR | |
To ensure that the alias will be used otherwise any binary on your system, | |
you should add this path before any other. Example: | |
export PATH="$ALIAS_DIR:\$PATH" | |
DALIAS DEFINITION | |
The dalias value is expected to be part of the docker command line. Consider | |
it will concatened with the following: | |
$DOCKER_CMD <your-alias-value-here> | |
Or if in background mode: | |
$BGDOCKER_CMD <your-alias-value-here> | |
To set a dalias to run in background mode, add "&" as the first character of | |
the dalias' value. | |
Note: You can also add extra flags at the start of the alias value. | |
You can use any valid shell code on your dalias value. Including: | |
- Variables, like: \$USER | |
- Script parameters, like: \$1, \$2 or \$@ | |
- Subshell expansion, like: \$(echo hello) | |
- etc. | |
EXAMPLES | |
$ dalias node='node:20-alpine node "\$@"' | |
$ dalias yarnd='&node:20-alpine yarn "\$@"' # Run in background mode | |
$ dalias php='php:8.2-cli "\$@"' | |
:EOF | |
} | |
function main() { | |
while [ "${1:0:1}" == "-" ]; do | |
case "$1" in | |
-h|--help) | |
show_help | |
exit 0 | |
;; | |
-p|--print) | |
print_all_aliases | |
exit 0 | |
;; | |
-r|--reload) | |
reload_aliases | |
exit 0 | |
;; | |
-d|--dry) | |
dry_mode=true | |
;; | |
-D|--delete) | |
delete_mode=true | |
;; | |
esac | |
shift 1 | |
done | |
for dalias in "$@"; do | |
if [ "$delete_mode" == true ]; then | |
delete_alias "$dalias" | |
else | |
set_alias "$dalias" | |
fi | |
done | |
} | |
function delete_alias() { | |
IFS='=' read -e name value <<< "$@" | |
if [ ! -f "$CONFIG_FILE" ]; then | |
return 0 | |
fi | |
if [ "$dry_mode" == true ]; then | |
echo "* '$name' dalias will be deleted!" | |
return 0 | |
fi | |
local tmpfile="$(mktemp)" | |
rm -f "$ALIAS_DIR/$name" | |
grep -v "^$name:" "$CONFIG_FILE" > "$tmpfile" | |
mv "$tmpfile" "$CONFIG_FILE" | |
} | |
function set_alias() { | |
local regex='^[a-zA-Z0-9_\-\.]+$' | |
IFS='=' read -e name value <<< "$@" | |
if ! [[ "$name" =~ $regex ]]; then | |
echo "The dalias name '$name' is invalid! The name should match: $regex" >&2 | |
return 1 | |
fi | |
if [ -z "$value" ]; then | |
local value="$DEFAULT_VALUE" | |
fi | |
if [ "$dry_mode" == true ]; then | |
echo "$(make_cmd $value)" | |
return 0 | |
fi | |
mkdir -p "$CONFIG_DIR" | |
local alias_line="$name:$value" | |
local tmpfile="$(mktemp)" | |
if [ ! -f "$CONFIG_FILE" ]; then | |
touch "$CONFIG_FILE" | |
fi | |
awk -v name="$name" -v value="$value" ' | |
BEGIN { FS=":" } | |
$1 == name { | |
$0 = name ":" value; | |
found = 1; | |
} | |
{ print } | |
END { | |
if (! found) { | |
print name ":" value; | |
} | |
} | |
' "$CONFIG_FILE" > "$tmpfile" | |
sort -o "$tmpfile" "$tmpfile" | |
mv "$tmpfile" "$CONFIG_FILE" | |
reload_aliases | |
} | |
function reload_aliases() { | |
if [ ! -f "$CONFIG_FILE" ]; then | |
{ | |
echo "Configuration file '$CONFIG_FILE' not found!" | |
echo "Please, create at least one dalias before run the reload." | |
} >&2 | |
exit 1 | |
fi | |
mkdir -p "$ALIAS_DIR" | |
while IFS=":" read -e name value; do | |
{ | |
echo "#!/bin/sh" | |
echo "# This file is auto-generated by 'dalias' tool. Please, do not" | |
echo "# update this file manually. See \`dalias --help\` for help." | |
echo "$(make_cmd $value)" | |
} > "$ALIAS_DIR/$name" | |
chmod +x "$ALIAS_DIR/$name" | |
done < "$CONFIG_FILE" | |
} | |
function print_all_aliases() { | |
if [ ! -f "$CONFIG_FILE" ]; then | |
return 0 | |
fi | |
while IFS=":" read -e name value; do | |
echo "dalias $name='$value'" | |
done < "$CONFIG_FILE" | |
} | |
function make_cmd() { | |
local value="$@" | |
if [ "${value:0:1}" == "&" ]; then | |
echo "$BGDOCKER_CMD ${value:1}" | |
else | |
echo "$DOCKER_CMD $value" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment