Skip to content

Commit ffcda27

Browse files
committed
initial commit
0 parents  commit ffcda27

File tree

9 files changed

+300
-0
lines changed

9 files changed

+300
-0
lines changed

Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM ubuntu:14.04
2+
MAINTAINER desk
3+
4+
WORKDIR /desk
5+
ADD desk /usr/local/bin/desk
6+
ADD examples examples

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2015 James O'Beirne
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
.PHONY: dockerbuild
3+
dockerbuild:
4+
docker build -t desk/desk .
5+
6+
.PHONY: bash
7+
bash: dockerbuild
8+
docker run -it desk/desk /bin/bash
9+
10+
.PHONY: lint
11+
lint:
12+
shellcheck desk

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
# ◲ desk
3+
4+
Lightweight workspace manager for the shell.
5+
6+
Desk makes it easy to flip back and forth between different project contexts in
7+
your favorite shell.
8+
9+
Instead of relying on `CTRL-R` for to execute and recall ("that command's gotta
10+
be here somewhere..."), desk helps shorten and document those actions with
11+
shell aliases and functions.
12+
13+
There are no dependencies other than some kind of Unix shell.
14+
15+
![whoa screencasting](screencap.gif)
16+
17+
Basically, desk just associates a shell script (`name.sh`) with a name. When
18+
you call `desk . name`, desk drops you into a shell where `name.sh` has been
19+
executed, and then desk extracts out certain comments in `name.sh` for useful
20+
rendering.
21+
22+
For example, given this deskfile
23+
```sh
24+
# Description: desk for doing work on a terraform-based repository
25+
#
26+
27+
cd ~/terraform-repo
28+
29+
# Set up AWS env variables: <key id> <secret>
30+
set_aws_env() {
31+
export AWS_ACCESS_KEY_ID="$1"
32+
export AWS_SECRET_ACCESS_KEY="$2"
33+
}
34+
35+
# Run `terraform plan` with proper AWS var config
36+
plan() {
37+
terraform plan -module-depth=-1 \
38+
-var "access_key=${AWS_ACCESS_KEY_ID}" \
39+
-var "secret_key=${AWS_SECRET_ACCESS_KEY}"
40+
}
41+
42+
# Run `terraform apply` with proper AWS var config
43+
alias apply='terraform apply'
44+
```
45+
46+
we'd get
47+
48+
```sh
49+
$ desk . tf
50+
$ desk
51+
52+
tf
53+
desk for doing work on a terraform repo
54+
55+
set_aws_env - Set up AWS env variables: <key id> <secret>
56+
plan - Run `terraform plan` with proper AWS var config
57+
apply - Run `terraform apply` with proper AWS var config
58+
```
59+
60+
### Installing
61+
62+
0. `git clone <this repo>`
63+
0. `sudo make install` or `cp desk/desk ~/bin/desk`
64+
0. `desk init`
65+
66+
### Deskfile rules
67+
68+
Deskfiles are just shell scripts, nothing more. Desk does pay attention
69+
to certain kinds of comments, though.
70+
71+
- *description*: you can describe a deskfile by including `# Description: ...`
72+
somewhere in the file.
73+
74+
- *alias and function docs*: if the line above an alias or function is a
75+
comment, it will be used as documentation.
76+
77+
### Sharing deskfiles across computers
78+
79+
Of course, the desk config directory (by default `~/.desks`) can be a symlink
80+
so that deskfiles can be stored in some centralized place, like Dropbox,
81+
and so shared across many computers.

desk

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
# vim: set filetype=sh:
3+
4+
PREFIX="${DESK_DIR:-$HOME/.desk}"
5+
DESKS="${DESK_DESKS_DIR:-$PREFIX/desks}"
6+
7+
8+
cmd_version() {
9+
echo "◲ desk 0.1.1"
10+
}
11+
12+
13+
cmd_usage() {
14+
cmd_version
15+
echo
16+
cat <<_EOF
17+
Usage:
18+
19+
$PROGRAM
20+
List the current desk and any associated aliases. If no desk
21+
is being used, display available desks.
22+
$PROGRAM init
23+
Initialize desk configuration.
24+
$PROGRAM (list|ls)
25+
List all desks along with a description.
26+
$PROGRAM (.|go) desk-name
27+
Activate a desk.
28+
$PROGRAM help
29+
Show this text.
30+
$PROGRAM version
31+
Show version information.
32+
33+
Since desk spawns a shell, to deactivate and "pop" out a desk, you
34+
simply need to exit or otherwise end the current shell process.
35+
_EOF
36+
}
37+
38+
cmd_init() {
39+
if [ -d "$PREFIX" ]; then
40+
echo "Desk dir already exists at ${PREFIX}"
41+
exit 1
42+
fi
43+
read -p "Where do you want to store your deskfiles? (default: ${PREFIX}): " \
44+
NEW_PREFIX
45+
[ -z "${NEW_PREFIX}" ] && NEW_PREFIX="$PREFIX"
46+
47+
if [ ! -d "${NEW_PREFIX}" ]; then
48+
echo "${NEW_PREFIX} doesn't exist, attempting to create."
49+
mkdir -p "$NEW_PREFIX/desks"
50+
fi
51+
echo "Done. Start adding desks to ${NEW_PREFIX}/desks!"
52+
}
53+
54+
55+
cmd_go() {
56+
local TODESK="$1"
57+
local DESKPATH="$(find "$DESKS" -name "${TODESK}".sh)"
58+
if [ -z "$DESKPATH" ]; then
59+
echo "Desk" "$TODESK" "not found in" "$DESKS"
60+
exit 1
61+
else
62+
DESK_ENV="${DESKPATH}" $SHELL
63+
fi
64+
}
65+
66+
67+
cmd_list() {
68+
if [ ! -d "$DESKS" ]; then
69+
echo "No desk dir! Run 'desk init'."
70+
exit 1
71+
fi
72+
73+
find "$DESKS" -name '*.sh' -print0 | while read -d '' -r f; do
74+
local name=$(basename "${f/.sh//}")
75+
local desc=$(echo_description "$f")
76+
77+
if [ -z "$desc" ]; then
78+
echo "$name"
79+
else
80+
echo "$name" "-" "$desc"
81+
fi
82+
done
83+
}
84+
85+
86+
cmd_current() {
87+
local DESKPATH=$DESK_ENV
88+
if [ -z "$DESKPATH" ]; then
89+
echo "No desk activated."
90+
exit 2
91+
else
92+
basename "${DESKPATH/.sh//}"
93+
echo_description "$DESKPATH"
94+
local CALLABLES=$(get_callables "$DESKPATH")
95+
96+
[ -z "$CALLABLES" ] || echo ""
97+
98+
for NAME in $CALLABLES; do
99+
local DOCLINE=$(
100+
grep -B 1 -E \
101+
"^(alias ${NAME}=|(function )?${NAME}\()" "$DESKPATH" \
102+
| grep "#")
103+
104+
if [ -z "$DOCLINE" ]; then
105+
echo " ${NAME}"
106+
else
107+
echo " ${NAME} -" "${DOCLINE##\# }"
108+
fi
109+
done
110+
fi
111+
}
112+
113+
# Echo the description of a desk. $1 is the deskfile.
114+
echo_description() {
115+
local descline=$(grep -E "#\s+Description" "$1")
116+
echo "${descline##*Description: }"
117+
}
118+
119+
# Return a list of aliases and functions for a given desk
120+
get_callables() {
121+
local DESKPATH=$1
122+
grep -E "^(alias |(function )?[a-zA-Z0-9_]+\()" "$DESKPATH" \
123+
| sed 's/alias \([^= ]*\)=.*/\1/' \
124+
| sed 's/\(function \)\?\([a-zA-Z0-9]*\)().*/\2/'
125+
}
126+
127+
128+
PROGRAM="${0##*/}"
129+
130+
case "$1" in
131+
init) shift; cmd_init "$@" ;;
132+
help|--help) shift; cmd_usage "$@" ;;
133+
version|--version) shift; cmd_version "$@" ;;
134+
ls|list) shift; cmd_list "$@" ;;
135+
go|.) shift; cmd_go "$@" ;;
136+
clear) shift; cmd_clear "$@" ;;
137+
*) cmd_current "$@" ;;
138+
esac
139+
exit 0

examples/desk.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Description: the desk I use to work on desk :)
2+
3+
cd ~/code/desk
4+
5+
# Run `make lint`
6+
alias lint="make lint"

examples/python_project.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Description: desk for working on a Python project
2+
3+
cd ~/python_project
4+
5+
source venv/bin/activate
6+
7+
PROJECT_NAME=python_project
8+
9+
# Run unittests with nose
10+
alias t="nosetests ${PROJECT_NAME}/tests"
11+
12+
# Install requirements
13+
alias req="pip install -r requirements.txt"
14+
15+
# Push the package to PyPI
16+
alias pypipush="python setup.py sdist upload -r ${PROJECT_NAME}"

examples/terraform.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Description: desk for doing work on a terraform-based repository
2+
#
3+
4+
cd ~/terraform-repo
5+
6+
# Set up AWS env variables: <key id> <secret>
7+
set_aws_env() {
8+
export AWS_DEFAULT_REGION=us-west-1
9+
export AWS_ACCESS_KEY_ID="$1"
10+
export AWS_SECRET_ACCESS_KEY="$2"
11+
}
12+
13+
# Run `terraform plan` with proper AWS var config
14+
plan() {
15+
terraform plan -module-depth=-1 \
16+
-var "access_key=${AWS_ACCESS_KEY_ID}" \
17+
-var "secret_key=${AWS_SECRET_ACCESS_KEY}"
18+
}
19+
20+
# Run `terraform apply` with proper AWS var config
21+
apply() {
22+
terraform apply \
23+
-var "access_key=${AWS_ACCESS_KEY_ID}" \
24+
-var "secret_key=${AWS_SECRET_ACCESS_KEY}"
25+
}
26+
27+
# Set up terraform config: <config_key>
28+
config() {
29+
local KEY=$1
30+
terraform remote config -backend=s3 \
31+
-backend-config="bucket=some.bucket.secrets.terraform" \
32+
-backend-config="key=${KEY}"
33+
}

screencap.gif

364 KB
Loading

0 commit comments

Comments
 (0)