Skip to content

Instantly share code, notes, and snippets.

@jrnewell
Last active November 16, 2022 12:20
Show Gist options
  • Save jrnewell/cb2f8663c4b8f898a0d4 to your computer and use it in GitHub Desktop.
Save jrnewell/cb2f8663c4b8f898a0d4 to your computer and use it in GitHub Desktop.
Bash functions
# list path env variable
function list-path() {
echo $PATH | tr ':' '\n'
}
# find text in files
function search() {
find . -type f -print0 | xargs -0 grep "$1"
}
# tar up directory
function tar-dir() {
local TARGET=$(basename "$1")
tar -zcvf "$TARGET".tar.gz -C "$(dirname "$1")" "$TARGET"
}
# update apt-get in one command
function apt-up() {
sudo apt-get update && sudo apt-get -y upgrade
}
# to easily switch between go projects
function go-path() {
local new_go_path="$(pwd)/lib"
if [ "$#" -ge "1" -a "$1" = "default" ]; then
local use_default="TRUE"
new_go_path=$DEFAULT_GOPATH
fi
if [ "$new_go_path" == "$GOPATH" ]; then
echo "GOPATH is already set to $new_go_path"
return 1
fi
if [ "$DEFAULT_GOPATH" != "$GOPATH" ]; then
echo "Removing $GOPATH/bin from PATH"
PATH=${PATH//$GOPATH\/bin?/}
fi
if [ "$use_default" != "TRUE" ]; then
echo "Adding $new_go_path/bin to PATH"
PATH=$new_go_path/bin:$PATH
fi
echo "Setting GOPATH to $new_go_path"
GOPATH="$new_go_path"
echo "Setting GOBIN to $new_go_path/bin"
GOBIN="$new_go_path/bin"
return 0
}
function default-go-path() {
go-path "default"
}
function use-node() {
if [[ "$NPM_HOME" == "$LOCAL_PATH/npm/node" ]]; then
echo "Already using node.js"
return
fi
echo "Switching to node.js"
brew unlink iojs
brew link node
echo "Changing .npmrc file to ~/local/npm/node"
local NPMRC_FILE="$HOME/.npmrc"
if [[ -f "$NPMRC_FILE" ]]; then
sed -i'.bak' "s/\/npm\/io/\/npm\/node/g" "$NPMRC_FILE"
if [[ $? -eq 0 ]]; then
rm -f "$NPMRC_FILE.bak"
fi
fi
echo "Updating NPM_HOME and PATH"
local NEW_HOME_HOME=$LOCAL_PATH/npm/node
PATH=${PATH//$NPM_HOME\/bin/$NEW_HOME_HOME/bin}
export NPM_HOME=$NEW_HOME_HOME
echo "node.js is ready"
}
function use-io() {
if [[ "$NPM_HOME" == "$LOCAL_PATH/npm/io" ]]; then
echo "Already using io.js"
return
fi
echo "Switching to io.js"
brew unlink node
brew link --force iojs
echo "Changing .npmrc file to ~/local/npm/io"
local NPMRC_FILE="$HOME/.npmrc"
if [[ -f "$NPMRC_FILE" ]]; then
sed -i'.bak' "s/\/npm\/node/\/npm\/io/g" "$NPMRC_FILE"
if [[ $? -eq 0 ]]; then
rm -f "$NPMRC_FILE.bak"
fi
fi
echo "Updating NPM_HOME and PATH"
local NEW_HOME_HOME=$LOCAL_PATH/npm/io
PATH=${PATH//$NPM_HOME\/bin/$NEW_HOME_HOME/bin}
export NPM_HOME=$NEW_HOME_HOME
echo "io.js is ready"
}
function env-node() {
if hash iojs 2>/dev/null; then
export NPM_HOME=$LOCAL_PATH/npm/io
PATH=$NPM_HOME/bin:$PATH
else
export NPM_HOME=$LOCAL_PATH/npm/node
PATH=$NPM_HOME/bin:$PATH
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment