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

Implement Deskfile recognition #55

Merged
merged 2 commits into from
Feb 24, 2016
Merged
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
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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
RUN chown -R $USERNAME:$USERNAME .zshrc examples run_tests.fish run_tests.sh .bashrc .config

# Set up test Deskfile
RUN mkdir -p example-project && cp examples/hello.sh example-project/Deskfile

RUN chown -R $USERNAME:$USERNAME .zshrc example-project examples run_tests.fish run_tests.sh .bashrc .config

USER $USERNAME
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ There are no dependencies other than `bash`. Desk is explicitly tested with `bas
`zsh`, and `fish`.

```sh
◲ desk 0.4.1
◲ desk 0.5.0

Usage:

Expand All @@ -44,8 +44,10 @@ Usage:
Initialize desk configuration.
desk (list|ls)
List all desks along with a description.
desk (.|go) <desk-name> [shell-args...]
Activate a desk. Extra arguments are passed onto shell.
desk (.|go) [<desk-name-or-path> [shell-args...]]
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.
desk run <desk-name> <cmd>
Run a command within a desk's environment then exit. Think '$SHELL -c'.
desk edit [desk-name]
Expand Down Expand Up @@ -95,9 +97,9 @@ $ desk
tf
desk for doing work on a terraform repo

set_aws_env - Set up AWS env variables: <key id> <secret>
plan - Run `terraform plan` with proper AWS var config
apply - Run `terraform apply` with proper AWS var config
set_aws_env Set up AWS env variables: <key id> <secret>
plan Run `terraform plan` with proper AWS var config
apply Run `terraform apply` with proper AWS var config
```

Basically, desk just associates a shell script (`name.sh`) with a name. When
Expand Down Expand Up @@ -179,7 +181,23 @@ Desk does pay attention to certain kinds of comments, though.

- *alias and function docs*: if the line above an alias or function is a
comment, it will be used as documentation.

### Deskfiles

Deskfiles are just shell scripts at the root of a project directory that
adhere to the conventions above. These can be put into version control to
formalize and ease common development tasks like running tests or doing
local builds.

For example, if we have some directory or repository called `myproject`, if
we create `myproject/Deskfile`, we'll be able to do any of the following:
```sh
$ desk go /path/to/myproject/
$ desk go /path/to/myproject/Deskfile
myproject/ $ desk go .
myproject/ $ desk go
```

### Sharing deskfiles across computers

Of course, the desk config directory (by default `~/.desks`) can be a symlink
Expand Down
38 changes: 32 additions & 6 deletions desk
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

PREFIX="${DESK_DIR:-$HOME/.desk}"
DESKS="${DESK_DESKS_DIR:-$PREFIX/desks}"
DESKFILE_NAME=Deskfile


## Commands

cmd_version() {
echo "◲ desk 0.4.1"
echo "◲ desk 0.5.0"
}


Expand All @@ -25,8 +26,10 @@ Usage:
Initialize desk configuration.
$PROGRAM (list|ls)
List all desks along with a description.
$PROGRAM (.|go) <desk-name> [shell-args...]
Activate a desk. Extra arguments are passed onto shell.
$PROGRAM (.|go) [<desk-name-or-path> [shell-args...]]
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 run <desk-name> <cmd>
Run a command within a desk's environment then exit. Think '\$SHELL -c'.
$PROGRAM edit [desk-name]
Expand Down Expand Up @@ -90,9 +93,34 @@ cmd_init() {


cmd_go() {
# 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 TODESK="$1"
local DESKEXT=$(get_deskfile_extension)
local DESKPATH="$(find "${DESKS}/" -name "${TODESK}${DESKEXT}")"
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

# Shift desk name so we can forward on all arguments to the shell.
shift;
Expand Down Expand Up @@ -165,8 +193,6 @@ cmd_current() {
fi

local DESKPATH=$DESK_ENV
local DESK_NAME=${DESKPATH##*/}
local DESK_NAME=${DESK_NAME%.*}
local CALLABLES=$(get_callables "$DESKPATH")
local AUTO_ALIGN len longest out

Expand Down
30 changes: 30 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,34 @@ RAN=$(desk run hello 'hi j')
echo "$RAN" | grep 'hi, j!' >/dev/null
ensure $? "Run in desk 'hello' didn't work with hi function"

## `desk go`

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

RAN=$(desk go example-project/ -c 'desk ; exit')
echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null
ensure $? "Deskfile invocation didn't work (example-project/)"
echo "$RAN" | grep -E "hi\s+" >/dev/null
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

RAN=$(desk go . -c 'desk ; exit')
echo "$RAN" | grep "example-project - simple desk that says hello" >/dev/null
ensure $? "Deskfile invocation didn't work (./)"
echo "$RAN" | grep -E "hi\s+" >/dev/null
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

echo "tests pass."