From 9f3ae5d0787fd44d0d401565f59b2f08472d601d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Thu, 8 Oct 2015 23:44:26 +0200 Subject: [PATCH] Display infos from `list` command in columns Also add a LINKS column to easily view which services are linked to which apps --- functions | 33 +++++++++++++++++++++------------ tests/service_list.bats | 14 +++++++++++--- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/functions b/functions index 1a4aee6..a4ae683 100755 --- a/functions +++ b/functions @@ -48,10 +48,11 @@ service_list() { if [[ -z $SERVICES ]]; then dokku_log_warn "There are no $PLUGIN_SERVICE services" else - dokku_log_info1_quiet "$PLUGIN_SERVICE services:" + LIST="NAME,VERSION,STATUS,EXPOSED PORTS,LINKS\n" for SERVICE in $SERVICES; do - dokku_log_verbose "$SERVICE, $(service_version "$SERVICE") $(service_status "$SERVICE")$(service_exposed_ports "$SERVICE")" + LIST+="$SERVICE,$(service_version "$SERVICE"),$(service_status "$SERVICE"),$(service_exposed_ports "$SERVICE"),$(service_linked_apps "$SERVICE")\n" done + printf "%b" "$LIST" | column -t -s, fi } @@ -59,13 +60,11 @@ service_exposed_ports() { local SERVICE="$1" local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" local PORT_FILE="$SERVICE_ROOT/PORT" - [[ ! -f $PORT_FILE ]] && return 0 + [[ ! -f $PORT_FILE ]] && echo '-' && return 0 local PORTS=($(cat "$PORT_FILE")) - echo -n ", exposed port(s):" for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do - echo -n " ${PLUGIN_DATASTORE_PORTS[i]}->${PORTS[i]}" + echo -n "${PLUGIN_DATASTORE_PORTS[i]}->${PORTS[i]} " done - echo "" } service_link() { @@ -140,12 +139,12 @@ service_status() { local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" local ID="$(cat "$SERVICE_ROOT/ID")" - is_container_status "$ID" "Dead" && echo "(dead)" && return 0 - is_container_status "$ID" "OOMKilled" && echo "(oomkilled)" && return 0 - is_container_status "$ID" "Paused" && echo "(paused)" && return 0 - is_container_status "$ID" "Restarting" && echo "(restarting)" && return 0 - is_container_status "$ID" "Running" && echo "(running)" && return 0 - echo "(stopped)" && return 0 + is_container_status "$ID" "Dead" && echo "dead" && return 0 + is_container_status "$ID" "OOMKilled" && echo "oomkilled" && return 0 + is_container_status "$ID" "Paused" && echo "paused" && return 0 + is_container_status "$ID" "Restarting" && echo "restarting" && return 0 + is_container_status "$ID" "Running" && echo "running" && return 0 + echo "stopped" && return 0 } service_port_expose() { @@ -342,3 +341,13 @@ remove_from_links_file() { sed -i "/^$APP\$/d" "$LINKS_FILE" sort "$LINKS_FILE" -u -o "$LINKS_FILE" } + +service_linked_apps() { + local SERVICE="$1" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + local LINKS_FILE="$SERVICE_ROOT/LINKS" + + [[ -z $(< "$LINKS_FILE") ]] && echo '-' && return 0 + + tr '\n' ' ' < "$LINKS_FILE" +} diff --git a/tests/service_list.bats b/tests/service_list.bats index f32d3f2..76c1742 100755 --- a/tests/service_list.bats +++ b/tests/service_list.bats @@ -9,15 +9,23 @@ teardown() { dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 } -@test "($PLUGIN_COMMAND_PREFIX:list) with no exposed ports" { +@test "($PLUGIN_COMMAND_PREFIX:list) with no exposed ports, no linked apps" { run dokku "$PLUGIN_COMMAND_PREFIX:list" - assert_contains "${lines[*]}" "l, redis:3.0.4 (running)" + assert_contains "${lines[*]}" "l redis:3.0.4 running - -" } @test "($PLUGIN_COMMAND_PREFIX:list) with exposed ports" { dokku "$PLUGIN_COMMAND_PREFIX:expose" l 4242 run dokku "$PLUGIN_COMMAND_PREFIX:list" - assert_contains "${lines[*]}" "l, redis:3.0.4 (running), exposed port(s): 6379->4242" + assert_contains "${lines[*]}" "l redis:3.0.4 running 6379->4242 -" +} + +@test "($PLUGIN_COMMAND_PREFIX:list) with linked app" { + dokku apps:create my_app + dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app + run dokku "$PLUGIN_COMMAND_PREFIX:list" + assert_contains "${lines[*]}" "l redis:3.0.4 running - my_app" + dokku --force apps:destroy my_app } @test "($PLUGIN_COMMAND_PREFIX:list) when there are no services" {