Skip to content

Commit

Permalink
improve shell code in oidc-agent-service
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmann committed Aug 28, 2024
1 parent f1797d5 commit 392a8e4
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/oidc-agent-service/oidc-agent-service
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ set -e
TMP="${TMPDIR:-/tmp}"

AGENTSERVICEDIR=${TMP}/oidc-agent-service-${UID}
PID_FILE=$AGENTSERVICEDIR/oidc-agent.pid
SOCK=$AGENTSERVICEDIR/oidc-agent.sock
PID_FILE=${AGENTSERVICEDIR}/oidc-agent.pid
SOCK=${AGENTSERVICEDIR}/oidc-agent.sock

JQ=jq
CAT=/bin/cat
Expand All @@ -17,30 +17,30 @@ REALPATH=realpath
OIDC_INCLUDE

function stop() {
if [ -f "$PID_FILE" ]; then
OIDCD_PID=$($CAT $PID_FILE)
OIDC_SOCK=$($REALPATH $SOCK)
$OIDC_AGENT --kill 2>/dev/null
$ECHO "unset OIDCD_PID_FILE;"
$RM -rf $AGENTSERVICEDIR
if [[ -f "${PID_FILE}" ]]; then
OIDCD_PID=$(${CAT} "${PID_FILE}")
OIDC_SOCK=$(${REALPATH} "${SOCK}")
${OIDC_AGENT} --kill 2>/dev/null
${ECHO} "unset OIDCD_PID_FILE;"
${RM} -rf "${AGENTSERVICEDIR}"
fi
}

function echo_vars() {
$ECHO "OIDC_SOCK=$SOCK; export OIDC_SOCK;"
$ECHO "OIDCD_PID=$OIDCD_PID; export OIDCD_PID;"
$ECHO "OIDCD_PID_FILE=$PID_FILE; export OIDCD_PID_FILE;"
$ECHO "$CAT $PID_FILE"
${ECHO} "OIDC_SOCK=${SOCK}; export OIDC_SOCK;"
${ECHO} "OIDCD_PID=${OIDCD_PID}; export OIDCD_PID;"
${ECHO} "OIDCD_PID_FILE=${PID_FILE}; export OIDCD_PID_FILE;"
${ECHO} "${CAT} ${PID_FILE}"
}

function start() {
json=$($OIDC_AGENT -a $SOCK $OIDC_AGENT_OPTS --pid-file="$PID_FILE" --json)
OIDCD_PID=$($ECHO "$json" | $JQ -r ".dpid")
json=$(${OIDC_AGENT} -a "${SOCK}" "${OIDC_AGENT_OPTS}" --pid-file="${PID_FILE}" --json)
OIDCD_PID=$(${ECHO} "${json}" | "${JQ}" -r ".dpid")
echo_vars
}

function use() {
if [ -f "${PID_FILE}" ] && kill -0 $($CAT $PID_FILE) 2>/dev/null; then
if [[ -f "${PID_FILE}" ]] && kill -0 "$(${CAT} "${PID_FILE}")" 2>/dev/null; then
echo_vars
else
start
Expand All @@ -63,8 +63,8 @@ function help() {

}

for arg in $@; do
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
for arg in "$@"; do
if [[ "${arg}" == "-h" || "${arg}" == "--help" ]]; then
help
exit
fi
Expand All @@ -75,7 +75,7 @@ if [[ "$1" == "kill" || "$1" == "stop" ]]; then
exit
fi
if [[ "$1" == "start-from-x" ]]; then
if [ "x${START_AGENT_WITH_XSESSION}" == "xTrue" ]; then
if [[ "${START_AGENT_WITH_XSESSION}" == "True" ]]; then
use
fi
exit
Expand All @@ -85,22 +85,22 @@ if [[ "$1" == "use" ]]; then
exit
fi
if [[ "$1" == "start" ]]; then
if [ -f "${PID_FILE}" ] && kill -0 $($CAT $PID_FILE) 2>/dev/null; then
echo "$ECHO 'It seems like oidc-agent-service already started an oidc-agent
if [[ -f "${PID_FILE}" ]] && kill -0 "$(${CAT} "${PID_FILE}")" 2>/dev/null; then
echo "${ECHO} 'It seems like oidc-agent-service already started an oidc-agent
for you.' >&2;
$ECHO 'Run \"oidc-agent-service stop\" to stop that agent or' >&2;
$ECHO 'run \"oidc-agent-service restart\" to restart the agent.' >&2"
${ECHO} 'Run \"oidc-agent-service stop\" to stop that agent or' >&2;
${ECHO} 'run \"oidc-agent-service restart\" to restart the agent.' >&2"
exit 1
fi
start
exit
fi
if [[ "$1" == "restart" ]]; then
if [ "x${OIDC_AGENT_RESTART_WITH_SAME_OPTS}" == "xTrue" ]; then
if [[ "${OIDC_AGENT_RESTART_WITH_SAME_OPTS}" == "True" ]]; then
set +e
STATUS=$($OIDC_AGENT --status --json 2>/dev/null)
if [ $? ]; then
OIDC_AGENT_OPTS=$($ECHO "$STATUS" | $JQ -r ".command_line_options")
STATUS=$(${OIDC_AGENT} --status --json 2>/dev/null)
if [[ $? ]]; then
OIDC_AGENT_OPTS=$(${ECHO} "${STATUS}" | ${JQ} -r ".command_line_options")
fi
set -e
fi
Expand All @@ -113,5 +113,5 @@ if [[ "$1" == "restart-s" ]]; then
start >/dev/null
exit
fi
echo "$ECHO \"Usage: $0 use | start | restart | restart-s | stop | kill\""
echo "${ECHO} \"Usage: $0 use | start | restart | restart-s | stop | kill\""
exit 1

0 comments on commit 392a8e4

Please sign in to comment.