From 116857fa472ccc0e29cf4e9dc8459669f304058a Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Sat, 1 Jan 2022 16:41:23 +0100 Subject: [PATCH] Adding more comments, renaming RunningContainer to Container. --- RunningContainer.shc => Container.shc | 86 +++++++++++++++------------ Docker.shn | 2 +- Utils.shc | 4 +- 3 files changed, 52 insertions(+), 40 deletions(-) rename RunningContainer.shc => Container.shc (76%) diff --git a/RunningContainer.shc b/Container.shc similarity index 76% rename from RunningContainer.shc rename to Container.shc index 6b16bc7..e780cdb 100644 --- a/RunningContainer.shc +++ b/Container.shc @@ -16,34 +16,46 @@ # along with this program. If not, see . # -property RunningContainer.found false -property RunningContainer.count 0 +# Only true when some containers matching all filters passed to the object have been found. +# Type: bool +property Container.found false -property RunningContainer.lines -property RunningContainer.id -property RunningContainer.image -property RunningContainer.created -property RunningContainer.status -property RunningContainer.ports -property RunningContainer.name +# Count of containers matching all filters found. +# Type: int +property Container.count 0 + +# Summary of all informations found on containers. +# Containers are separated by a new line and values by " ; ". +property Container.lines +# Docker container ids of matched containers. +property Container.id +# Docker container images of matched containers. +property Container.image +# Dates of creation of matched containers. +property Container.created +# Statuses summary of matched containers. +property Container.status +# List of ports of matched containers. +property Container.ports +# Names of matched containers. +property Container.name # Constructor to fetch one or many running container. -# Arguments are the various filters to find containers. See: RunningContainer.filter for reference. +# Arguments are the various filters to find containers. See: Container.filter for reference. # NOTE: Multiple containers can be matched. If so, all properties contain the value for each container per line. # NOTE: If no container is found, a message will be printed, and the constructor will exit with false. # Signature: ([string[] filters]) -RunningContainer.constructor() { +Container.constructor() { args=( "$@" ) this.lines = "$($(Docker.Utils.DockerCommand) ps --all | awk -F " {2,}" 'NR>1 { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }')" # Apply filters for ((i=0; i < ${#args[@]}; i++)); do - filterName=${args[$i]} - + filterName="${args[$i]}" case $filterName in with_id | with_image | created | with_status | with_port | named) # The next argument is the value of the filter, so we skip it. i=$(expr "$i" + 1) - filterValue=${args[$i]} + filterValue="${args[$i]}" this._baseFilter $filterName "$filterValue" ;; running | exited | stopped) @@ -65,8 +77,8 @@ RunningContainer.constructor() { } # Finds all containers in "docker ps" with a certain field matching a value and returns their lines. -# Signature: (, , ) -RunningContainer.findLineByValue() { +# Signature: (, , ) -> string +Container.findLineByValue() { fieldId=$1 operation=$2 fieldValue=$3 @@ -74,12 +86,12 @@ RunningContainer.findLineByValue() { this.lines | awk -F " ; " '$'"$fieldId"' '"$operation"' "'"$fieldValue"'" { print $1,";",$2,";",$3,";",$4,";",$5,";",$6,($7 != "" ? (";"$7) : "") }' } -# Internal function which filters. See: RunningContainer.filter for reference. +# Internal function which filters. See: Container.filter for reference. # Signature: (, [string filterValue]) -RunningContainer._baseFilter() { +Container._baseFilter() { filterName=$1 filterValue=$2 - #echo -n "Applying filter $filterName (previous count $(this.lines | wc -l))..." 1>&2 + #echo "Applying filter $filterName (previous count $(this.lines | wc -l))..." 1>&2 case $filterName in with_id) this.lines = "$(this.findLineByValue 1 == "$filterValue")" @@ -94,7 +106,7 @@ RunningContainer._baseFilter() { this.lines = "$(this.findLineByValue 5 == "$filterValue")" ;; running) - this.lines = "$(RunningContainer.findLineByValue 5 ~ "Up")" + this.lines = "$(Container.findLineByValue 5 ~ "Up")" ;; exited | stopped) this.lines = "$(this.findLineByValue 5 "~" "Exited")" @@ -115,7 +127,7 @@ RunningContainer._baseFilter() { # Updates property data from fetched lines. # No argument -RunningContainer._updateProperties() { +Container._updateProperties() { if [ ! -z "$(this.lines)" ]; then this.found = true this.count = $(this.lines | wc -l) @@ -143,8 +155,8 @@ RunningContainer._updateProperties() { # - "with_port" to find all having this port forwarded # - "running" to find all containers running # - "exited" or "stopped" to find all containers stopped. -# Signature: (, [string filterValue]) -RunningContainer.filter() { +# Signature: (, [string filterValue]) -> void +Container.filter() { filterName=$1 filterValue=$2 this._baseFilter $filterName $filterValue @@ -152,8 +164,8 @@ RunningContainer.filter() { } # Applies a docker command to all containers -# Signature: (, [string[] arguments]) -RunningContainer.applyToAll() { +# Signature: (, [string[] arguments]) -> void|string +Container.applyToAll() { cmd=$1 args=${@:2} for container in $(this.id); do @@ -162,38 +174,38 @@ RunningContainer.applyToAll() { } # Kills all containers assigned to this instance. -# Signature: ([string[] arguments]) -RunningContainer.kill() { +# Signature: ([string[] arguments]) -> void +Container.kill() { $this.applyToAll kill $@ } # Removes all containers assigned to this instance. -# Signature: ([string[] arguments]) -RunningContainer.rm() { +# Signature: ([string[] arguments]) -> void +Container.rm() { $this.applyToAll rm $@ } # Removes all containers assigned to this instance. -# Signature: ([string[] arguments]) -RunningContainer.remove() { +# Signature: ([string[] arguments]) -> void +Container.remove() { $this.applyToAll rm $@ } # Waits for all containers assigned to this instance to stop and echo their exit code. -# Signature: ([string[] arguments]) -RunningContainer.wait() { +# Signature: ([string[] arguments]) -> void +Container.wait() { $this.applyToAll wait $@ } # Displays the logs of the containers -# Signature: ([string[] arguments]) -RunningContainer.logs() { +# Signature: ([string[] arguments]) -> string +Container.logs() { $this.applyToAll logs $@ } # Executes a command to all the containers assigned to the instance. -# Signature: ([string[] options] ) -RunningContainer.exec() { +# Signature: ([string[] options] ) -> void|string +Container.exec() { cmd=${@[-1]} options=${@:$(exec ${#@} - 1)} for container in $(this.id); do diff --git a/Docker.shn b/Docker.shn index 2875946..aedb8ec 100644 --- a/Docker.shn +++ b/Docker.shn @@ -28,6 +28,6 @@ property Docker.Executable "$(which docker)" static_class Utils "Utils.shc" -class RunningContainer "RunningContainer.shc" +class Container "Container.shc" class Image "Image.shc" diff --git a/Utils.shc b/Utils.shc index ab27280..3532307 100644 --- a/Utils.shc +++ b/Utils.shc @@ -19,14 +19,14 @@ # This class contains various utils required for Docker. # Returns the base command for docker, with it's executable and sudo if necessary. -# Returns string. +# Signature: () -> string Utils.DockerCommand() { if [ $(Docker.RequiresSudo) == true ]; then echo -n "sudo "; fi echo "$(Docker.Executable)" } # Checks if the user has permission to use docker. -# Retuns bool. +# Signature: () -> bool Utils.hasPermission() { $(Utils.DockerCommand) ps 2>&1 | grep -q "permission denied" # Check status of last command. If permission denied is found, then we do not have permission to use docker.