Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load command #80

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
FROM ubuntu:14.04
MAINTAINER desk
FROM ubuntu:jammy

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ENV DEBIAN_FRONTEND=noninteractive

ENV USERNAME desktester
RUN adduser --disabled-password --gecos "" desktester
RUN apt-get update && apt-get install -y vim expect zsh fish

RUN set -eux; \
\
apt-get update -y; \
apt-get install -y \
vim \
expect \
zsh \
fish \
; \
apt-get clean; \
rm -f /var/lib/apt/lists/*_*

WORKDIR /home/$USERNAME/
ADD desk /usr/local/bin/desk
ADD test/zshrc .zshrc
ADD test/bashrc .bashrc
ADD test/run_tests.sh run_tests.sh
ADD test/run_tests.fish run_tests.fish
ADD examples examples
RUN mkdir -p .config/fish && touch .config/fish/config.fish

# Set up test Deskfile
RUN mkdir -p example-project && cp examples/hello.sh example-project/Deskfile
COPY desk /usr/local/bin/desk
COPY test/zshrc .zshrc
COPY test/bashrc .bashrc
COPY test/run_tests.sh run_tests.sh
COPY test/run_tests.fish run_tests.fish
COPY examples examples

RUN chown -R $USERNAME:$USERNAME .zshrc example-project examples run_tests.fish run_tests.sh .bashrc .config
RUN set -eux; \
\
mkdir -p .config/fish; \
touch .config/fish/config.fish; \
# Set up test Deskfile \
mkdir -p example-project; \
cp examples/hello.sh example-project/Deskfile; \
\
chown -R $USERNAME:$USERNAME .zshrc example-project examples run_tests.fish run_tests.sh .bashrc .config

USER $USERNAME
79 changes: 59 additions & 20 deletions desk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Usage:
Activate a desk. Extra arguments are passed onto shell. If called with
no arguments, look for a Deskfile in the current directory. If not a
recognized desk, try as a path to directory containing a Deskfile.
$PROGRAM load [<desk-name-or-path>]
Display the commands needed to load a desk into the current shell. If
called with no arguments, look for a Deskfile in the current directory.
If not a recognized desk, try as a path to directory containing a
Deskfile. Normal usage: eval \$($PROGRAM load <name>)
$PROGRAM run <desk-name> '<cmd>'
$PROGRAM run <desk-name> <cmd> <arg>...
Run a command within a desk's environment then exit. In the first form
Expand Down Expand Up @@ -103,27 +108,10 @@ cmd_go() {
# - a directory containing a Deskfile
# - empty -> `./Deskfile`
#
local TODESK="$1"
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}" 2>/dev/null)"

local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}"
if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then
POSSIBLE_DESKFILE_DIR="."
fi

# If nothing could be found in $DESKS/, check to see if this is a path to
# a Deskfile.
if [[ -z "$DESKPATH" && -d "$POSSIBLE_DESKFILE_DIR" ]]; then
if [ ! -f "${POSSIBLE_DESKFILE_DIR}/${DESKFILE_NAME}" ]; then
echo "No Deskfile found in '${POSSIBLE_DESKFILE_DIR}'"
exit 1
fi

local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd )
DESKPATH="${REALPATH}/${DESKFILE_NAME}"
TODESK=$(basename "$REALPATH")
fi
local RESULT="$(get_desk_name_and_path $1)"
local TODESK="${RESULT%:*}"
local DESKPATH="${RESULT#*:}"

# Shift desk name so we can forward on all arguments to the shell.
shift;
Expand All @@ -138,6 +126,30 @@ cmd_go() {
}


cmd_load() {
# TODESK ($1) may either be
#
# - the name of a desk in $DESKS/
# - a path to a Deskfile
# - a directory containing a Deskfile
# - empty -> `./Deskfile`
#

local RESULT="$(get_desk_name_and_path $1)"
local TODESK="${RESULT%:*}"
local DESKPATH="${RESULT#*:}"

# Shift desk name so we can forward on all arguments to the shell.
shift;

if [ -z "$DESKPATH" ]; then
echo "Desk $TODESK (${TODESK}${DESKEXT}) not found in $DESKS"
exit 1
else
echo "export DESK_NAME=\"${TODESK}\"; export DESK_ENV=\"${DESKPATH}\"; source \${DESK_ENV}"
fi
}

cmd_run() {
local TODESK="$1"
shift;
Expand Down Expand Up @@ -324,6 +336,32 @@ print_aligned() {
}'
}

get_desk_name_and_path() {
local TODESK="$1"
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}" 2>/dev/null)"

local POSSIBLE_DESKFILE_DIR="${TODESK%$DESKFILE_NAME}"
if [ -z "$POSSIBLE_DESKFILE_DIR" ]; then
POSSIBLE_DESKFILE_DIR="."
fi

# If nothing could be found in $DESKS/, check to see if this is a path to
# a Deskfile.
if [[ -z "$DESKPATH" && -d "$POSSIBLE_DESKFILE_DIR" ]]; then
if [ ! -f "${POSSIBLE_DESKFILE_DIR}/${DESKFILE_NAME}" ]; then
>&2 echo "No Deskfile found in '${POSSIBLE_DESKFILE_DIR}'"
exit 1
fi

local REALPATH=$( cd $POSSIBLE_DESKFILE_DIR && pwd )
DESKPATH="${REALPATH}/${DESKFILE_NAME}"
TODESK=$(basename "$REALPATH")
fi

echo "${TODESK}:${DESKPATH}"
}


PROGRAM="${0##*/}"

Expand All @@ -333,6 +371,7 @@ case "$1" in
version|--version) shift; cmd_version "$@" ;;
ls|list) shift; cmd_list "$@" ;;
go|.) shift; cmd_go "$@" ;;
load) shift; cmd_load "$@" ;;
run) shift; cmd_run "$@" ;;
edit) shift; cmd_edit "$@" ;;
*) cmd_current "$@" ;;
Expand Down
18 changes: 16 additions & 2 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ ensure $? "Deskfile invocation didn't work (example-project/)"
echo "$RAN" | grep -E "howdy\s+" >/dev/null
ensure $? "Deskfile invocation didn't work (example-project/)"

pushd example-project
pushd example-project >/dev/null

RAN=$(desk go . -c 'desk ; exit')
echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null
Expand All @@ -171,6 +171,20 @@ ensure $? "Deskfile invocation didn't work (example-project/)"
echo "$RAN" | grep -E "howdy\s+" >/dev/null
ensure $? "Deskfile invocation didn't work (example-project/)"

popd
popd >/dev/null

## `desk load`

pushd example-project >/dev/null

RAN=$($SHELL -c 'eval $(desk load); desk; exit')
echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null
ensure $? "Deskfile load didn't work (./)"
echo "$RAN" | grep -E "hi\s+" >/dev/null
ensure $? "Deskfile load didn't work (example-project/)"
echo "$RAN" | grep -E "howdy\s+" >/dev/null
ensure $? "Deskfile load didn't work (example-project/)"

popd >/dev/null

echo "tests pass."