Skip to content

Commit

Permalink
Merge pull request #66 from brhahlen/fr-dc-to-shell
Browse files Browse the repository at this point in the history
Add `shell` command - basic
  • Loading branch information
brhahlen authored Jun 20, 2024
2 parents de8a949 + e598f58 commit 1505218
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Version 3.4.0
## New Feature
- `dc shell <container>` to get a shell

## Removal
- Removed `network` command

# Version 3.3.1
## Bugfix
- New commands were not showing up in tab completion
Expand Down
9 changes: 6 additions & 3 deletions build-files/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Version 3.3.1
## Bugfix
- New commands were not showing up in tab completion
# Version 3.4.0
## New Feature
- `dc shell <container>` to get a shell

## Removal
- Removed `network` command
41 changes: 29 additions & 12 deletions dc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
set -m

# VERSION
export DC_VERSION=v3.3.1
export DC_VERSION=v3.4.0

###### VARIABLES
if [[ -z "${DC_DIR}" ]]; then
Expand Down Expand Up @@ -173,9 +173,9 @@ function show_usage (){
printf " %brestart%b Restarts one or more services \n" "${RED}" "${NC}"
printf " %bpull%b Pulls an image of a service \n" "${RED}" "${NC}"
printf " %blogs%b Shows logs for a service or services \n" "${RED}" "${NC}"
printf " %shell%b Enters the shell of a container (defaults to /bin/sh) \n" "${RED}" "${NC}"
printf "============================= System Commands ============================= \n"
printf " %bips%b Shows assigned IPs of containers \n" "${RED}" "${NC}"
printf " %bnetwork%b Create the MacVLAN network, needs sudo \n" "${RED}" "${NC}"
printf " %bprune%b Prunes images and containers (basic) or the system \n" "${RED}" "${NC}"
printf "======================= Install, Update and Version ======================= \n"
printf " %binstall%b Install dc for the user, needs sudo \n" "${RED}" "${NC}"
Expand Down Expand Up @@ -664,18 +664,35 @@ function logs(){
fi
}

function shell(){
if [ "$#" -ne 1 ]; then
printf "No or multiple arguments provided, please provide the name of one service \n"
else
printf "Accessing shell for the following service: %s \n" "$1"
tmp_stack_files_age
SERVICE="$1"
DC_CMD_PARAM="exec ${SERVICE} /bin/sh"
if grep -r -q -x "${SERVICE}" "${TMP_DIR}"; then
STACK_NAME=$(basename "$(grep -r -x "${SERVICE}" "${TMP_DIR}" | cut -d : -f 1 )")
STACK="${DC_DIR}/${STACK_NAME}"
verbose_stack "${STACK}" "${STACK_NAME}" "${SERVICE}"
cd "${STACK}" || { echo "cd failed"; exit 1; }
create_env_file "${STACK}" "${STACK_NAME}"
dc_cmd_create "${DC_CMD_PARAM}"
cd - > /dev/null || { echo "cd failed"; exit 1; }
else
printf "ERROR: Service %s was not found in any of the stacks. Does it show up when you run %bdc list%b? \n" "${SERVICE}" "${RED}" "${NC}"
fi
fi
}

##################################################
# SYSTEM COMMANDS #
##################################################
# IPS
function ips(){
printf "Showing IPs \n"
(echo "CONTAINER IP-ADRESS MAC-ADDRESS"; docker inspect --format='{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{.MacAddress}}{{end}}' $(docker ps -aq)| sort -k 2 -V) | column -t -s ' '
}

# NETWORK
function network(){
printf "This function does nothing (yet) \n"
(echo "CONTAINER IP-ADRESS MAC-ADDRESS"; docker inspect --format='{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{.MacAddress}}{{end}}' "$(docker ps -aq)"| sort -k 2 -V) | column -t -s ' '
}

function prune() {
Expand Down Expand Up @@ -754,14 +771,14 @@ case ${1} in
"logs")
logs "${@:2}"
;;
# ----------- shell ------------
"shell")
shell "${@:2}"
;;
# ----------- ips ------------
"ips")
ips
;;
# ----------- network ------------
"network")
network
;;
# ----------- prune ------------
"prune")
prune "${@}"
Expand Down
8 changes: 4 additions & 4 deletions dc-completion
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (C) 2022 - 2023 Ben Hählen
# This is the completion file for `dc`
##############
# Version 3.3.1
# Version 3.4.0
##############
_dc_completions()
{
Expand All @@ -21,7 +21,7 @@ _dc_completions()
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 network clean list help update version prune"
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
Expand All @@ -33,7 +33,7 @@ _dc_completions()
COMPREPLY=( $(compgen -W "${STACKS}" -- "${CUR}") )
return 0
;;
start|stop|restart|pull|logs)
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
Expand Down Expand Up @@ -69,7 +69,7 @@ _dc_completions()
COMPREPLY=( $(compgen -W "${SERVICES}" -- "${CUR}") )
return 0
;;
install|ips|network|clean|list|help|version)
install|ips|clean|list|help|version)
return
;;
prune)
Expand Down

0 comments on commit 1505218

Please sign in to comment.