add command alias

This commit is contained in:
Jintin Lin
2018-05-01 12:54:58 +08:00
parent 89a9effb1a
commit 156e853cc7
5 changed files with 305 additions and 171 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.DS_Store
list
path
cmd

View File

@@ -18,16 +18,22 @@ echo "source ~/.aliasme/aliasme.sh" >> ~/.bash_profile
```
## Usage
```bash
$ al add [name] [value] # add alias with name and value
$ al path [name] [value] # add alias path with name
$ al cmd [name] [command] # add alias command with name
$ al rm [name] # remove alias by name
$ al ls # alias list
$ al ls # list all alias
$ al [name] # execute alias associate with [name]
$ al -v # version information
$ al -h # help
```
## Migrate from 1.X to 2.X
Please execute this command
```
mv ~/.aliasme/list ~/.aliasme/path
```
## Contributing
Bug reports and pull requests are welcome on GitHub at [https://github.com/Jintin/aliasme](https://github.com/Jintin/aliasme).

View File

@@ -1,14 +1,26 @@
#!/usr/bin/env bash
_list() {
while read name
do
read value
echo "$name : $value"
done < ~/.aliasme/list
if [ -s ~/.aliasme/path ];then
echo "PATH:"
while read name
do
read value
echo "$name : $value"
done < ~/.aliasme/path
fi
if [ -s ~/.aliasme/cmd ];then
echo "CMD:"
while read name
do
read value
echo "$name : $value"
done < ~/.aliasme/cmd
fi
}
_add() {
_path() {
#read name
name=$1
if [ -z $1 ]; then
@@ -16,14 +28,33 @@ _add() {
fi
#read path
path_alias=$2
path=$2
if [ -z $2 ]; then
read -ep "Input path to add:" path_alias
read -ep "Input path to add:" path
fi
path_alias=$(cd $path_alias;pwd)
path=$(cd $path;pwd)
echo $name >> ~/.aliasme/list
echo $path_alias >> ~/.aliasme/list
echo $name >> ~/.aliasme/path
echo $path >> ~/.aliasme/path
_autocomplete
}
_cmd() {
#read name
name=$1
if [ -z $1 ]; then
read -ep "Input name to add:" name
fi
#read path
cmd=$2
if [ -z $2 ]; then
read -ep "Input cmd to add:" cmd
fi
echo $name >> ~/.aliasme/cmd
echo $cmd >> ~/.aliasme/cmd
_autocomplete
}
@@ -34,17 +65,28 @@ _remove() {
if [ -z $1 ]; then
read -pr "Input name to remove:" name
fi
touch ~/.aliasme/listtemp
touch ~/.aliasme/pathtemp
touch ~/.aliasme/cmdtemp
# read and replace file
while read line
do
if [ $line = $name ]; then
read line #skip one more line
else
echo $line >> ~/.aliasme/listtemp
echo $line >> ~/.aliasme/pathtemp
fi
done < ~/.aliasme/list
mv ~/.aliasme/listtemp ~/.aliasme/list
done < ~/.aliasme/path
mv ~/.aliasme/pathtemp ~/.aliasme/path
while read line
do
if [ $line = $name ]; then
read line #skip one more line
else
echo $line >> ~/.aliasme/cmdtemp
fi
done < ~/.aliasme/cmd
mv ~/.aliasme/cmdtemp ~/.aliasme/cmd
_autocomplete
}
@@ -54,10 +96,22 @@ _jump() {
if [ $1 = $line ]; then
read line
cd $line
return
return 0
fi
done < ~/.aliasme/list
echo "not found"
done < ~/.aliasme/path
return 1
}
_excute() {
while read line
do
if [ $1 = $line ]; then
read line
eval $line
return 0
fi
done < ~/.aliasme/cmd
return 1
}
_bashauto()
@@ -71,7 +125,12 @@ _bashauto()
do
opts+=" $line"
read line
done < ~/.aliasme/list
done < ~/.aliasme/path
while read line
do
opts+=" $line"
read line
done < ~/.aliasme/cmd
COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
return 0
}
@@ -85,7 +144,12 @@ _autocomplete()
do
opts+="$line "
read line
done < ~/.aliasme/list
done < ~/.aliasme/path
while read line
do
opts+="$line "
read line
done < ~/.aliasme/cmd
compctl -k "($opts)" al
else
# bash
@@ -99,23 +163,28 @@ al(){
if [ ! -z $1 ]; then
if [ $1 = "ls" ]; then
_list
elif [ $1 = "add" ]; then
_add $2 $3
elif [ $1 = "path" ]; then
_path $2 $3
elif [ $1 = "cmd" ]; then
_cmd $2 $3
elif [ $1 = "rm" ]; then
_remove $2
elif [ $1 = "-h" ]; then
echo "Usage:"
echo "al add [name] [value] # add alias with name and value"
echo "al path [name] [value] # add alias path with name"
echo "al cmd [name] [command] # add alias command with name"
echo "al rm [name] # remove alias by name"
echo "al ls # alias list"
echo "al [name] # execute alias associate with [name]"
echo "al -v # version information"
echo "al -h # help"
elif [ $1 = "-v" ]; then
echo "aliasme 1.1.2"
echo "aliasme 2.0.0"
echo "visit https://github.com/Jintin/aliasme for more information"
else
_jump $1
if ! _jump $1 && ! _excute $1 ; then
echo "not found"
fi
fi
fi
}

View File

@@ -4,39 +4,41 @@
. aliasme.sh
testInit() {
if [[ ! -f ~/.aliasme/list ]]; then
mkdir -p ~/.aliasme && touch ~/.aliasme/list
if [[ ! -f ~/.aliasme/path ]]; then
mkdir -p ~/.aliasme && touch ~/.aliasme/path
fi
}
testAlias() {
path_alias=$(pwd)
name1=testaaa
testPath $name1 $path_alias
name2=testbbb
data=$(_list)
testAdd $name1 $path_alias "$data"
data1=$(_list)
testAdd $name2 $path_alias "$data1"
testPath $name2 $path_alias
testRemove $name2 "$data1"
testRemove $name1 "$data"
assert_end
testRemove $name1
testRemove $name2
}
testAdd() {
_add $1 $2
if [[ ! -z $3 ]]; then
assert _list "$3\n$1 : $2"
testPath() {
_path $1 $2
if [[ $(_list) = *"$1 : $2"* ]]; then
log_success "path test success"
else
assert _list "$1 : $2"
log_failure "path test failure"
fi
}
testRemove() {
_remove $1
assert _list "$2"
if [[ $(_list) = *"$1"* ]]; then
log_failure "remove test failure"
else
log_success "remove test success"
fi
}
testInit

View File

@@ -1,138 +1,194 @@
#!/bin/bash
# assert.sh 1.0 - bash unit testing framework
# Copyright (C) 2009, 2010, 2011, 2012 Robert Lehmann
#
# http://github.com/lehmannro/assert.sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/usr/bin/env bash
export DISCOVERONLY=${DISCOVERONLY:-}
export DEBUG=${DEBUG:-}
export STOP=${STOP:-}
export INVARIANT=${INVARIANT:-}
#####################################################################
##
## title: Assert Extension
##
## description:
## Assert extension of shell (bash, ...)
## with the common assert functions
## Function list based on:
## http://junit.sourceforge.net/javadoc/org/junit/Assert.html
## Log methods : inspired by
## - https://natelandau.com/bash-scripting-utilities/
## author: Mark Torok
##
## date: 07. Dec. 2016
##
## license: MIT
##
#####################################################################
args="$(getopt -n "$0" -l verbose,help,stop,discover,invariant vhxdi $*)" \
|| exit -1
for arg in $args; do
case "$arg" in
-h)
echo "$0 [-vxid] [--verbose] [--stop] [--invariant] [--discover]"
echo "$(sed 's/./ /g' <<< "$0") [-h] [--help]"
exit 0;;
--help)
cat <<EOF
Usage: $0 [options]
Language-agnostic unit tests for subprocesses.
Options:
-v, --verbose generate output for every individual test case
-x, --stop stop running tests after the first failure
-i, --invariant do not measure timings to remain invariant between runs
-d, --discover collect test suites only, do not run any tests
-h show brief usage information and exit
--help show this help message and exit
EOF
exit 0;;
-v|--verbose)
DEBUG=1;;
-x|--stop)
STOP=1;;
-i|--invariant)
INVARIANT=1;;
-d|--discover)
DISCOVERONLY=1;;
esac
done
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
MAGENTA=$(tput setaf 5)
NORMAL=$(tput sgr0)
BOLD=$(tput bold)
printf -v _indent "\n\t" # local format helper
log_header() {
printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2
}
_assert_reset() {
tests_ran=0
tests_failed=0
tests_errors=()
tests_starttime="$(date +%s.%N)" # seconds_since_epoch.nanoseconds
log_success() {
printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2
}
assert_end() {
# assert_end [suite ..]
tests_endtime="$(date +%s.%N)"
tests="$tests_ran ${*:+$* }tests"
[[ -n "$DISCOVERONLY" ]] && echo "collected $tests." && _assert_reset && return
[[ -n "$DEBUG" ]] && echo
[[ -z "$INVARIANT" ]] && report_time=" in $(bc \
<<< "${tests_endtime%.N} - ${tests_starttime%.N}" \
| sed -e 's/\.\([0-9]\{0,3\}\)[0-9]*/.\1/' -e 's/^\./0./')s" \
|| report_time=
if [[ "$tests_failed" -eq 0 ]]; then
echo "all $tests passed$report_time."
else
for error in "${tests_errors[@]}"; do echo "$error"; done
echo "$tests_failed of $tests failed$report_time."
fi
tests_failed_previous=$tests_failed
_assert_reset
return $tests_failed_previous
log_failure() {
printf "${RED}✖ %s${NORMAL}\n" "$@" >&2
}
assert() {
# assert <command> <expected stdout> [stdin]
(( tests_ran++ ))
[[ -n "$DISCOVERONLY" ]] && return
# printf required for formatting
printf -v expected "x${2:-}" # x required to overwrite older results
result="$(eval 2>/dev/null $1 <<< ${3:-})"
# Note: $expected is already decorated
if [[ "x$result" == "$expected" ]]; then
[[ -n "$DEBUG" ]] && echo -n .
return
fi
[[ -n "$DEBUG" ]] && echo -n X
result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")"
[[ -z "$result" ]] && result="nothing" || result="\"$result\""
[[ -z "$2" ]] && expected="nothing" || expected="\"$2\""
failure="expected $expected${_indent}got $result"
report="test #$tests_ran \"$1${3:+ <<< $3}\" failed:${_indent}$failure"
tests_errors[$tests_failed]="$report"
(( tests_failed++ ))
if [[ -n "$STOP" ]]; then
[[ -n "$DEBUG" ]] && echo
echo "$report"
exit 1
fi
assert_eq() {
local expected="$1"
local actual="$2"
local msg
if [ "$#" -ge 3 ]; then
msg="$3"
fi
if [ "$expected" == "$actual" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg" || true
return 1
fi
}
assert_raises() {
# assert_raises <command> <expected code> [stdin]
(( tests_ran++ ))
[[ -n "$DISCOVERONLY" ]] && return
(eval $1 <<< ${3:-}) > /dev/null 2>&1
status=$?
expected=${2:-0}
if [[ "$status" -eq "$expected" ]]; then
[[ -n "$DEBUG" ]] && echo -n .
return
fi
[[ -n "$DEBUG" ]] && echo -n X
failure="program terminated with code $status instead of $expected"
report="test #$tests_ran \"$1${3:+ <<< $3}\" failed:${_indent}$failure"
tests_errors[$tests_failed]="$report"
(( tests_failed++ ))
if [[ -n "$STOP" ]]; then
[[ -n "$DEBUG" ]] && echo
echo "$report"
exit 1
fi
assert_not_eq() {
local expected="$1"
local actual="$2"
local msg
if [ "$#" -ge 3 ]; then
msg="$3"
fi
if [ ! "$expected" == "$actual" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg" || true
return 1
fi
}
_assert_reset
assert_true() {
local actual
local msg
actual="$1"
if [ "$#" -ge 3 ]; then
msg="$3"
fi
assert_eq true "$actual" "$msg"
return "$?"
}
assert_false() {
local actual
local msg
actual="$1"
if [ "$#" -ge 3 ]; then
msg="$3"
fi
assert_eq false "$actual" "$msg"
return "$?"
}
assert_array_eq() {
declare -a expected=("${!1}")
# echo "AAE ${expected[@]}"
declare -a actual=("${!2}")
# echo "AAE ${actual[@]}"
local msg
if [ "$#" -ge 3 ]; then
msg="$3"
fi
local return_code
return_code=0
if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then
return_code=1
fi
local i
for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do
if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then
return_code=1
break
fi
done
if [ "$return_code" == 1 ]; then
[ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" || true
fi
return "$return_code"
}
assert_array_not_eq() {
declare -a expected=("${!1}")
declare -a actual=("${!2}")
local msg
if [ "$#" -ge 3 ]; then
msg="$3"
fi
local return_code
return_code=1
if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then
return_code=0
fi
local i
for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do
if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then
return_code=0
break
fi
done
if [ "$return_code" == 1 ]; then
[ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" || true
fi
return "$return_code"
}
assert_empty() {
local actual
local msg
actual="$1"
if [ "$#" -ge 2 ]; then
msg="$2"
fi
assert_eq "" "$actual" "$msg"
return "$?"
}
assert_not_empty() {
local actual
local msg
actual="$1"
if [ "$#" -ge 2 ]; then
msg="$2"
fi
assert_not_eq "" "$actual" "$msg"
return "$?"
}