patternbashMinor
Shell script wrapper for Docker build and run
Viewed 0 times
scriptdockershellwrapperforandbuildrun
Problem
I have written a simple wrapper for
Here's the layout of my code:
#!/usr/bin/env bash
#
# Build esp8266 development container
readonly CURRENT_SCRIPT="$(basename -- ${BASH_SOURCE[0]})"
readonly CURRENT_DIRECTORY="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly DOCKER_BINARY="$(command -v docker)"
source "${CURRENT_DIRECTORY}/scripts/config.sh"
source "${CURRENT_DIRECTORY}/scripts/util.sh"
# usage:
docker build and docker run that allows me to build and run my image without having to use the docker command directly.Here's the layout of my code:
esp8266
├── scripts
│ ├── config.sh
And the files themselves:
scripts/config.sh
readonly DOCKER_IMAGE_NAME="jackwilsdon/esp8266"
readonly DOCKER_IMAGE_TAG="0.1.0"
readonly DOCKER_IMAGE_MOUNT="/mnt/data"
readonly DOCKER_LOCAL_SOURCE_DIRECTORY="docker"
readonly DOCKER_LOCAL_MOUNT_DIRECTORY="data"
scripts/util.sh
#!/usr/bin/env bash
#
# Utilities for esp8266 development container scripts
# usage: print [args...]
print() {
echo "${CURRENT_SCRIPT}: $*"
}
# usage: print_raw [args...]
print_raw() {
echo "$*"
}
# usage: error [args...]
error() {
print "error: $*" >&2
}
# usage: error_raw [args...]
error_raw() {
print_raw "$*" >&2
}
# usage: check_requirements
check_requirements() {
if [[ -z "${CURRENT_SCRIPT}" ]]; then
error "CURRENT_SCRIPT not set"
exit 1
fi
if [[ -z "${CURRENT_DIRECTORY}" ]]; then
error "CURRENT_DIRECTORY not set"
exit 1
fi
if [[ -z "${DOCKER_BINARY}" ]]; then
error "unable to find docker (DOCKER_BINARY not set)"
return 1
fi
}
# usage: docker [args...]
docker() {
"${DOCKER_BINARY}" "$@"
local exit_code="$?"
if [[ "${exit_code}" -ne 0 ]]; then
error "${DOCKER_BINARY} exited with code ${exit_code}"
return 1
fi
}
# usage: arg_or_default arg default [new arg value]
arg_or_default() {
if [[ -n "$1" ]]; then
if [[ "$#" -eq 3 ]]; then
echo "$3"
else
echo "$1"
fi
else
echo "$2"
fi
}
build
``#!/usr/bin/env bash
#
# Build esp8266 development container
readonly CURRENT_SCRIPT="$(basename -- ${BASH_SOURCE[0]})"
readonly CURRENT_DIRECTORY="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly DOCKER_BINARY="$(command -v docker)"
source "${CURRENT_DIRECTORY}/scripts/config.sh"
source "${CURRENT_DIRECTORY}/scripts/util.sh"
# usage:
Solution
Looking very good. Honestly I only have nitpicks.
The purpose of the 3rd argument of
From the comment in the code I don't understand it,
and it's not used by any of the scripts.
Either explain it better in the comment, or drop it,
since you're not using it anyway.
The
When you don't need to match a pattern but a literal string,
as in
then you can replace it with a single
You don't need to double-quote the
You don't need to quote string literals like
You don't need the single-quote pairs when setting a variable to empty as in
The purpose of the 3rd argument of
arg_or_default is unclear.From the comment in the code I don't understand it,
and it's not used by any of the scripts.
Either explain it better in the comment, or drop it,
since you're not using it anyway.
The
== operator in [[ is for pattern matching.When you don't need to match a pattern but a literal string,
as in
[[ "$0" == "${BASH_SOURCE[0]}" ]],then you can replace it with a single
=.You don't need to double-quote the
$? variable, it's always safe.You don't need to quote string literals like
print_raw.You don't need the single-quote pairs when setting a variable to empty as in
docker_volume='', you can write simply docker_volume=Context
StackExchange Code Review Q#137877, answer score: 2
Revisions (0)
No revisions yet.