-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdce.plugin.zsh
More file actions
80 lines (71 loc) · 2.29 KB
/
dce.plugin.zsh
File metadata and controls
80 lines (71 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# upsearch function to find docker-compose.yml file in the custom folder
upsearch () {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/$1" && echo "$directory/$1" && return
directory="$directory/.."
done
}
# If the docker-compose.yml file is found, execute the command in the container
dce() {
# Check if at least one argument is provided
if [ $# -eq 0 ]; then
echo "Usage: dce <container_name> [command]"
echo "Examples:"
echo " dce node # Open shell in 'node' container"
echo " dce node bash # Run bash in 'node' container"
echo " dce node npm install # Run npm install in 'node' container"
return 1
fi
COMPOSE_PATH=$(upsearch docker-compose.yml)
if [ -n "$COMPOSE_PATH" ]; then
COMPOSE_DIR=$(cd "$(dirname "$COMPOSE_PATH")" || exit; pwd)
# If only one argument is provided, assume they want a shell
if [ $# -eq 1 ]; then
echo "Opening shell in container: $1"
docker compose -f "$COMPOSE_PATH" exec "$1" /bin/bash
else
# Multiple arguments provided, execute the command
docker compose -f "$COMPOSE_PATH" exec "$@"
fi
else
echo "docker-compose.yml not found."
return 1
fi
}
# Helper function to get docker-compose.yml path
_dce_get_compose_path() {
upsearch docker-compose.yml
}
# Helper function to get available containers from docker-compose.yml
_dce_get_containers() {
local compose_path=$(_dce_get_compose_path)
if [ -n "$compose_path" ]; then
# Try to get services from docker compose config (works even if containers aren't running)
docker compose -f "$compose_path" config --services 2>/dev/null
fi
}
# Autocomplete function using Zsh's completion system
_dce() {
local curcontext="$curcontext" state line
typeset -A opt_args
_arguments \
'*:container:->containers'
case $state in
containers)
local compose_path=$(_dce_get_compose_path)
if [ -n "$compose_path" ]; then
local -a containers
# Get containers/services from docker compose
containers=("${(@f)$(_dce_get_containers)}")
if [ ${#containers[@]} -gt 0 ]; then
_describe 'containers' containers
fi
fi
;;
esac
}
# Register the completion function
compdef _dce dce