-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc-completion
executable file
·92 lines (86 loc) · 3.77 KB
/
dc-completion
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
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
# Copyright (C) 2022 - 2023 Ben Hählen
# This is the completion file for `dc`
##############
# Version 3.4.8
##############
_dc_completions()
{
local CUR OPTS
COMPREPLY=()
CUR="${COMP_WORDS[COMP_CWORD]}"
if [ "${COMP_WORDS[1]}" == "-v" ]; then
cmd="${COMP_WORDS[2]}"
else
cmd="${COMP_WORDS[1]}"
fi
## Variables
export DC_CMD="docker compose"
# Basic/main options that will be completed
OPTS="install up down restart-stack-hard restart-stack-soft logs-stack start stop restart pull logs ips clean list help update version prune shell"
# Based on the arguments, we can run functions
case "$cmd" in
up|down|restart-stack-hard|restart-stack-soft|logs-stack)
# shellcheck disable=SC2155
local STACKS=$(for STACK in $(find "${DC_DIR}" \( -name compose.yaml -or -name compose.yml -or -name docker-compose.yaml -or -name docker-compose.yml \) -printf '%h\n' | sort -u | rev | cut -d / -f 1 | rev); do echo "${STACK}" ; done )
if [ ! "$cmd" == "logs-stack" ]; then
STACKS="all ${STACKS}"
fi
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${STACKS}" -- "${CUR}") )
return 0
;;
start|stop|restart|pull|logs|shell)
# We might need to create the tmp-files, so reusing the command from dc, however, this is duplicating stuff, which is not nice, but for a MVP, it works
for STACK in $(find "${DC_DIR}" \( -name compose.yaml -or -name compose.yml -or -name docker-compose.yaml -or -name docker-compose.yml \) -printf '%h\n' | sort -u);
do
STACK_NAME=$(echo "${STACK}" | rev | cut -d / -f 1 | rev)
ENV="${DC_DIR}/.env"
DC_CMD_INIT="${DC_CMD} --env-file ${ENV}"
DC_COMMAND="${DC_CMD_INIT} --env-file ${STACK}/${STACK_NAME}.env"
# echo "${DC_COMMAND}"
TMP_DIR="/tmp/dc_stack_services"
if [ ! -d "${TMP_DIR}" ]; then
# echo "${TMP_DIR} not found. Creating..."
mkdir -p ${TMP_DIR}
fi
if [ -f ${TMP_DIR}/"${STACK_NAME}" ]; then
# echo "File exists, checking age"
if [ "$(find ${TMP_DIR}/"${STACK_NAME}" -mmin +30)" ]; then
# echo "File is too old, refreshing"
cd "${STACK}" || { echo "cd failed"; exit 1; }
# echo "${DC_COMMAND}"
${DC_COMMAND} config --services > ${TMP_DIR}/"${STACK_NAME}"
cd - > /dev/null || { echo "cd failed"; exit 1; }
fi
else
# echo "File does not exist, creating"
cd "${STACK}" || { echo "cd failed"; exit 1; }
# echo "${DC_COMMAND}"
${DC_COMMAND} config --services > ${TMP_DIR}/"${STACK_NAME}"
cd - > /dev/null || { echo "cd failed"; exit 1; }
fi
done
# On to the completion
# shellcheck disable=SC2155
local SERVICES=$(while IFS= read -r SERVICE; do echo "${SERVICE}"; done < <(cat /tmp/dc_stack_services/*) )
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${SERVICES}" -- "${CUR}") )
return 0
;;
install|ips|clean|list|help|version)
return
;;
prune)
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "basic system" -- "${CUR}") )
return 0
;;
*)
;;
esac
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${OPTS}" -- "${CUR}") )
return 0
}
complete -F _dc_completions dc