Skip to content

Commit

Permalink
normalize the variable format used
Browse files Browse the repository at this point in the history
Use the "${var}" format for variables where possible
  • Loading branch information
miabbott committed Apr 8, 2022
1 parent 2d4f77e commit 0483acc
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions rhcos-toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ run() {
elif ! image_fresh; then
read -r -p "There is a newer version of ${TOOLBOX_IMAGE} available. Would you like to pull it? [y/N] "

if [[ $REPLY =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
if [[ ${REPLY} =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
image_pull
if container_exists; then
sudo podman rm "${TOOLBOX_NAME}"
Expand All @@ -48,15 +48,15 @@ run() {
container_create_runlabel
fi
else
echo "Container '$TOOLBOX_NAME' already exists. Trying to start..."
echo "(To remove the container and start with a fresh toolbox, run: sudo podman rm '$TOOLBOX_NAME')"
echo "Container '${TOOLBOX_NAME}' already exists. Trying to start..."
echo "(To remove the container and start with a fresh toolbox, run: sudo podman rm '${TOOLBOX_NAME}')"
fi

local state=$(container_state)
if [[ "$state" == configured ]] || [[ "$state" == exited ]] || [[ "$state" == stopped ]]; then
if [[ "${state}" == configured ]] || [[ "${state}" == exited ]] || [[ "${state}" == stopped ]]; then
container_start
elif [[ "$state" != running ]]; then
echo "Container '$TOOLBOX_NAME' in unknown state: '$state'"
elif [[ "${state}" != running ]]; then
echo "Container '${TOOLBOX_NAME}' in unknown state: '$state'"
return 1
fi

Expand All @@ -67,7 +67,7 @@ run() {
}

cleanup() {
sudo podman stop "$TOOLBOX_NAME" &>/dev/null
sudo podman stop "${TOOLBOX_NAME}" &>/dev/null
}

container_exists() {
Expand Down Expand Up @@ -99,17 +99,17 @@ image_fresh() {
}

image_runlabel() {
sudo podman image inspect "$TOOLBOX_IMAGE" --format '{{- if index .Labels "run" -}}{{.Labels.run}}{{- end -}}'
sudo podman image inspect "${TOOLBOX_IMAGE}" --format '{{- if index .Labels "run" -}}{{.Labels.run}}{{- end -}}'
}


image_pull() {
if ! sudo --preserve-env podman pull --authfile "${AUTHFILE}" "$TOOLBOX_IMAGE"; then
if ! sudo --preserve-env podman pull --authfile "${AUTHFILE}" "${TOOLBOX_IMAGE}"; then
read -r -p "Would you like to manually authenticate to registry: '${REGISTRY}' and try again? [y/N] "

if [[ $REPLY =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
if [[ ${REPLY} =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
sudo --preserve-env podman login --authfile "${AUTHFILE}" "${REGISTRY}"
sudo --preserve-env podman pull --authfile "${AUTHFILE}" "$TOOLBOX_IMAGE"
sudo --preserve-env podman pull --authfile "${AUTHFILE}" "${TOOLBOX_IMAGE}"
else
echo "Exiting..."
exit 1
Expand All @@ -120,31 +120,31 @@ image_pull() {
container_create() {
if ! sudo podman create \
--hostname toolbox \
--name "$TOOLBOX_NAME" \
--name "${TOOLBOX_NAME}" \
--privileged \
--net=host \
--pid=host \
--ipc=host \
--tty \
--interactive \
-e HOST=/host \
-e NAME="$TOOLBOX_NAME" \
-e IMAGE="$IMAGE" \
-e NAME="${TOOLBOX_NAME}" \
-e IMAGE="${IMAGE}" \
--security-opt label=disable \
--volume /run:/run \
--volume /var/log:/var/log \
--volume /etc/machine-id:/etc/machine-id \
--volume /etc/localtime:/etc/localtime \
--volume /:/host \
"$TOOLBOX_IMAGE" 2>&1; then
echo "$0: failed to create container '$TOOLBOX_NAME'"
"${TOOLBOX_IMAGE}" 2>&1; then
echo "$0: failed to create container '${TOOLBOX_NAME}'"
exit 1
fi
}

container_start() {
if ! sudo podman start "$TOOLBOX_NAME" 2>&1; then
echo "$0: failed to start container '$TOOLBOX_NAME'"
if ! sudo podman start "${TOOLBOX_NAME}" 2>&1; then
echo "$0: failed to start container '${TOOLBOX_NAME}'"
exit 1
fi
}
Expand All @@ -153,14 +153,14 @@ container_create_runlabel() {
# Variable replacement logic reproduced from:
# https://github.com/containers/podman/blob/29d7ab3f82e38c442e449739e218349b9a4a16ea/pkg/domain/infra/abi/containers_runlabel.go#L226
local pod
pod="$(echo "$runlabel" \
pod="$(echo "${runlabel}" \
| sed 's/podman run/sudo podman create/' \
| sed 's/--name NAME/--name $TOOLBOX_NAME/' \
| sed 's/NAME=NAME/NAME=$TOOLBOX_NAME/' \
| sed 's/IMAGE=IMAGE/IMAGE=$TOOLBOX_IMAGE/' \
| sed 's/host IMAGE/host $TOOLBOX_IMAGE/')"
if ! eval "$pod" ; then
echo "$0: failed to create container from runlabel '$TOOLBOX_NAME'"
| sed 's/--name NAME/--name ${TOOLBOX_NAME}/' \
| sed 's/NAME=NAME/NAME=${TOOLBOX_NAME}/' \
| sed 's/IMAGE=IMAGE/IMAGE=${TOOLBOX_IMAGE}/' \
| sed 's/host IMAGE/host ${TOOLBOX_IMAGE}/')"
if ! eval "${pod}" ; then
echo "$0: failed to create container from runlabel '${TOOLBOX_NAME}'"
exit 1
fi
}
Expand All @@ -169,19 +169,19 @@ container_exec() {
if [[ "$#" -eq 0 ]]; then
cmd=$(sudo podman image inspect "${TOOLBOX_IMAGE}" | jq -re ".[].Config.Cmd[0]") || cmd="/bin/sh"
sudo podman exec \
--env LANG="$LANG" \
--env TERM="$TERM" \
--env LANG="${LANG}" \
--env TERM="${TERM}" \
--tty \
--interactive \
"$TOOLBOX_NAME" \
"$cmd"
"${TOOLBOX_NAME}" \
"${cmd}"
else
sudo podman exec \
--env LANG="$LANG" \
--env TERM="$TERM" \
--env LANG="${LANG}" \
--env TERM="${TERM}" \
--tty \
--interactive \
"$TOOLBOX_NAME" \
"${TOOLBOX_NAME}" \
"$@"
fi
}
Expand All @@ -196,9 +196,9 @@ Options:
-h/--help: Shows this help message
You may override the following variables by setting them in ${TOOLBOXRC}:
- REGISTRY: The registry to pull from. Default: $REGISTRY
- IMAGE: The image and tag from the registry to pull. Default: $IMAGE
- TOOLBOX_NAME: The name to use for the local container. Default: $TOOLBOX_NAME
- REGISTRY: The registry to pull from. Default: ${REGISTRY}
- IMAGE: The image and tag from the registry to pull. Default: ${IMAGE}
- TOOLBOX_NAME: The name to use for the local container. Default: ${TOOLBOX_NAME}
- AUTHFILE: The location where your registry credentials are stored. Default: ${AUTHFILE}
Example toolboxrc:
Expand Down

0 comments on commit 0483acc

Please sign in to comment.