Skip to content

Instantly share code, notes, and snippets.

@KBalderson
Last active February 7, 2018 16:23
Show Gist options
  • Save KBalderson/629bc4523d7188b02914a882c8a1ce7a to your computer and use it in GitHub Desktop.
Save KBalderson/629bc4523d7188b02914a882c8a1ce7a to your computer and use it in GitHub Desktop.
Muck - A simple bash script to run your Procfile in tmux

Muck

Prerequisites

  • tmux: brew install tmux
  • a .muck configuration file

Configuration

~/.muck Should be a YAML file.

Each line will have a key: value format where:

  • key: the name of the session (e.g. rails)
  • value: The fully expanded path to the direcory. (e.g. /Users/kbalderson/Projects/flashcards) (~/ does not work)
# ~/.muck
flashcards: /Users/kbalderson/Projects/flashcards

Usage

When you run muck SESSION_NAME, there are two tmux windows by default, and two optional that are present based

  1. editor: runs vim ./
  2. utility: no commands by default, i use it when i need the console, or anything else
  3. web: if you have a web: line in your Procfile, it is put in it's own window.
  4. services: Anything listed in your Procfile other than web: is given it's own pane in this window, and the even-vertical layout is used.
#!/bin/bash
getWorkspace() {
cat $HOME/.muck | tr -s ' ' | eval "sed -n -e '/$SESSION_NAME/p'" | \
awk -F$ '{ split($1,a,": "); printf("%s", a[2]); }'
}
getWebCommand() {
cat $WORKSPACE/Procfile | tr -s ' ' | sed -n -e '/web/p' | \
awk -F$ '{
split($1,a,": ");
printf("neww -n %s \\\; send-keys '\''%s'\'' C-m \\\; ", a[1],a[2]);
}'
}
getServiceCommands() {
cat $WORKSPACE/Procfile | tr -s ' ' | sed -e '/web/d' | \
awk -F$ '{
split($1,a,": ");
if (NR == 1) { command = "neww -n services \\\;" }
else { command = "split-window -h \\\;" }
printf("%s send-keys '\''%s'\'' C-m \\\; ", command,a[2]);
}'
}
: ${SESSION_NAME:="$1"}
: ${WORKSPACE:="$(getWorkspace)"}
echo -ne "\033]0;"$SESSION_NAME"\007"
tmux has-session -t ${SESSION_NAME} &> /dev/null
if [ $? != 0 ]
then
# Create the session
eval "tmux \
new-session -c ${WORKSPACE} -s ${SESSION_NAME} -n 'editor' \;\
send-keys 'vim ./' C-m \; \
neww -n 'utility' \; \
$(getWebCommand) \
$(getServiceCommands) \
select-layout even-horizontal \; \
select-window -t editor" &> /dev/null
else
tmux attach -t ${SESSION_NAME} &> /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment