Permalink
Browse files

[+]: use a local free port for "phpserver" && auto-completion for "gr…

…unt"
  • Loading branch information...
voku committed Jun 21, 2014
1 parent 804e60e commit c50619a75f2b13db8381d0454d43ee7c084a6e55
Showing with 66 additions and 5 deletions.
  1. +39 −1 .bash_complete
  2. +27 −4 .functions
@@ -23,5 +23,43 @@ complete -W "$(echo `cat ~/.bash_history | egrep '^ssh ' | sort | uniq | sed 's/
# add tab completion for SSH hostnames based on ~/.ssh/config, ignoring wildcards
[ -e "~/.ssh/config" ] && complete -o "default" -o "nospace" -W "$(grep "^Host" ~/.ssh/config | grep -v "[?*]" | cut -d " " -f2 | tr ' ' '\n')" scp sftp ssh
# Add tab completion for openvpn configs based on ~/.openvpn/config, ignoring wildcards
[ -e "~/.openvpn/config/" ] && complete -o "default" -o "nospace" -W "$(find ~/.openvpn/config/ -name "*.ovpn" )" openvpn
# try to use tab auto-completion for Grunt
#[ -x /usr/bin/grunt ] && eval "$(grunt --completion=bash)"
# Search the current directory and all parent directories for a gruntfile.
_grunt_gruntfile()
{
local curpath="$PWD"
while [[ "$curpath" ]]; do
for gruntfile in "$curpath/"{G,g}runtfile.{js,coffee}; do
if [[ -e "$gruntfile" ]]; then
echo "$gruntfile"
return
fi
done
curpath="${curpath%/*}"
done
return 1
}
# Enable bash autocompletion.
_grunt_completions()
{
# The currently-being-completed word.
local cur="${COMP_WORDS[COMP_CWORD]}"
# The current gruntfile, if it exists.
local gruntfile="$(_grunt_gruntfile)"
# The current grunt version, available tasks, options, etc.
local gruntinfo="$(grunt --version --verbose 2>/dev/null)"
# Options and tasks.
local opts="$(echo "$gruntinfo" | awk '/Available options: / {$1=$2=""; print $0}')"
local compls="$(echo "$gruntinfo" | awk '/Available tasks: / {$1=$2=""; print $0}')"
# Only add -- or - options if the user has started typing -
[[ "$cur" == -* ]] && compls="$compls $opts"
# Tell complete what stuff to show.
COMPREPLY=($(compgen -W "$compls" -- "$cur"))
}
[ -x /usr/bin/grunt ] && complete -o default -F _grunt_completions grunt
@@ -62,10 +62,31 @@ testConnection()
}
# -------------------------------------------------------------------
# netstat_free_local_port: get a free tcp-port (from 32768-61000)
# netstat_used_local_ports: get used tcp-ports
netstat_used_local_ports()
{
netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$' | sort -n | uniq
}
# -------------------------------------------------------------------
# netstat_free_local_port: get one free tcp-port
netstat_free_local_port()
{
$(netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq 32768 61000) | sort -n | uniq -u | head -n 1
read lowerPort upperPort < /proc/sys/net/ipv4/ip_local_port_range
# create a local array of used ports
local all_used_ports=($(netstat_used_local_ports))
for port in $(seq $lowerPort $upperPort); do
for used_port in "${all_used_ports[@]}"; do
if [ $used_port -eq $port ]; then
continue
else
echo $port
return 0
fi
done
done
}
# -------------------------------------------------------------------
@@ -465,7 +486,8 @@ gitio()
# server: Start an HTTP server from a directory, optionally specifying the port
server()
{
local port="${1:-8000}"
local free_port=$(netstat_free_local_port)
local port="${1:-${free_port}}"
sleep 1 && xdg-open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
@@ -480,7 +502,8 @@ server()
# phpserver [port=4000] [ip=127.0.0.1]
phpserver()
{
local port="${1:-4000}"
local free_port=$(netstat_free_local_port)
local port="${1:-${free_port}}"
local ip="${2:-127.0.0.1}"
sleep 1 && xdg-open "http://${ip}:${port}/" &
php -S "${ip}:${port}"

0 comments on commit c50619a

Please sign in to comment.