diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5bcfe6f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: CI + +on: + push: + branches: [ master, main ] + pull_request: + branches: [ master, main ] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install ShellCheck + run: sudo apt-get install -y shellcheck + + - name: Run ShellCheck + run: shellcheck aliasme.sh test/*.sh + + - name: Run Tests + run: make test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f5ae071..0000000 --- a/.travis.yml +++ /dev/null @@ -1,2 +0,0 @@ -language: bash -script: make test diff --git a/README.md b/README.md index b540aa3..3f1dbbd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # aliasme [![Codacy Badge](https://api.codacy.com/project/badge/Grade/dc29953069bf43438f4abac2629e4b5a)](https://app.codacy.com/app/Jintin/aliasme?utm_source=github.com&utm_medium=referral&utm_content=Jintin/aliasme&utm_campaign=badger) -[![Build Status](https://travis-ci.org/Jintin/aliasme.svg?branch=master)](https://travis-ci.org/Jintin/aliasme) +[![CI](https://github.com/Jintin/aliasme/actions/workflows/ci.yml/badge.svg)](https://github.com/Jintin/aliasme/actions/workflows/ci.yml) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/alebcay/awesome-shell) A shell script to organize your alias in command line. diff --git a/aliasme.sh b/aliasme.sh index ec465c6..17b1959 100644 --- a/aliasme.sh +++ b/aliasme.sh @@ -2,30 +2,30 @@ _list() { - if [ -s ~/.aliasme/cmd ];then - while read name + if [ -s "$HOME/.aliasme/cmd" ];then + while read -r name do - read value + if ! read -r value; then break; fi echo "$name : $value" - done < ~/.aliasme/cmd + done < "$HOME/.aliasme/cmd" fi } _add() { #read name name=$1 - if [ -z $1 ]; then - read -ep "Input name to add:" name + if [ -z "$1" ]; then + read -rep "Input name to add:" name fi #read path cmd="$2" if [ -z "$2" ]; then - read -ep "Input cmd to add:" cmd + read -rep "Input cmd to add:" cmd fi - echo $name >> ~/.aliasme/cmd - echo $cmd >> ~/.aliasme/cmd + echo "$name" >> "$HOME/.aliasme/cmd" + echo "$cmd" >> "$HOME/.aliasme/cmd" echo "add: $name -> $cmd" _autocomplete @@ -34,36 +34,36 @@ _add() { _remove() { #read name name=$1 - if [ -z $1 ]; then + if [ -z "$1" ]; then read -pr "Input name to remove:" name fi # read and replace file - if [ -s ~/.aliasme/cmd ];then - touch ~/.aliasme/cmdtemp - while read line + if [ -s "$HOME/.aliasme/cmd" ];then + touch "$HOME/.aliasme/cmdtemp" + while read -r line do if [ "$line" = "$name" ]; then - read line #skip one more line + read -r _ #skip one more line echo "remove $name" else - echo $line >> ~/.aliasme/cmdtemp + echo "$line" >> "$HOME/.aliasme/cmdtemp" fi - done < ~/.aliasme/cmd - mv ~/.aliasme/cmdtemp ~/.aliasme/cmd + done < "$HOME/.aliasme/cmd" + mv "$HOME/.aliasme/cmdtemp" "$HOME/.aliasme/cmd" fi _autocomplete } _excute() { - if [ -s ~/.aliasme/cmd ];then - while read -u9 line; do + if [ -s "$HOME/.aliasme/cmd" ];then + while read -u9 -r line; do if [ "$1" = "$line" ]; then - read -u9 line - eval $line + read -u9 -r line + eval "$line" return 0 fi - done 9< ~/.aliasme/cmd + done 9< "$HOME/.aliasme/cmd" fi return 1 } @@ -75,29 +75,31 @@ _bashauto() cur="${COMP_WORDS[COMP_CWORD]}" opts="" - if [ -s ~/.aliasme/cmd ];then - while read line + if [ -s "$HOME/.aliasme/cmd" ];then + while read -r line do opts+=" $line" - read line - done < ~/.aliasme/cmd + read -r _ + done < "$HOME/.aliasme/cmd" fi - COMPREPLY=( $(compgen -W "${opts}" ${cur}) ) + # shellcheck disable=SC2207 + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 } _autocomplete() { - if [ $ZSH_VERSION ]; then + if [ -n "$ZSH_VERSION" ]; then # zsh opts="" - if [ -s ~/.aliasme/cmd ];then - while read line + if [ -s "$HOME/.aliasme/cmd" ];then + while read -r line do opts+="$line " - read line - done < ~/.aliasme/cmd + read -r _ + done < "$HOME/.aliasme/cmd" fi + # shellcheck disable=SC2154 compctl -k "($opts)" al else # bash @@ -108,14 +110,14 @@ _autocomplete() _autocomplete al(){ - if [ ! -z $1 ]; then - if [ $1 = "ls" ]; then + if [ -n "$1" ]; then + if [ "$1" = "ls" ]; then _list - elif [ $1 = "add" ]; then - _add $2 "$3" - elif [ $1 = "rm" ]; then - _remove $2 - elif [ $1 = "-h" ]; then + elif [ "$1" = "add" ]; then + _add "$2" "$3" + elif [ "$1" = "rm" ]; then + _remove "$2" + elif [ "$1" = "-h" ]; then echo "Usage:" echo "al add [name] [command] # add alias command with name" echo "al rm [name] # remove alias by name" @@ -123,11 +125,11 @@ al(){ echo "al [name] # execute alias associate with [name]" echo "al -v # version information" echo "al -h # help" - elif [ $1 = "-v" ]; then + elif [ "$1" = "-v" ]; then echo "aliasme 3.0.0" echo "visit https://github.com/Jintin/aliasme for more information" else - if ! _excute $1 ; then + if ! _excute "$1" ; then echo "not found" fi fi diff --git a/test/aliastest.sh b/test/aliastest.sh index 496eabe..abd081e 100755 --- a/test/aliastest.sh +++ b/test/aliastest.sh @@ -11,22 +11,20 @@ testInit() { testAlias() { - path_alias=$(pwd) - name1=testaaa cmd1=cmdaaa - testAdd $name1 $cmd1 + testAdd "$name1" "$cmd1" name2=testbbb cmd2=cmdbbb - testAdd $name2 $cmd2 + testAdd "$name2" "$cmd2" - testRemove $name1 - testRemove $name2 + testRemove "$name1" + testRemove "$name2" } testAdd() { - _add $1 $2 + _add "$1" "$2" if [[ $(_list) = *"$1 : $2"* ]]; then log_success "path test success" else @@ -35,7 +33,7 @@ testAdd() { } testRemove() { - _remove $1 + _remove "$1" if [[ $(_list) = *"$1"* ]]; then log_failure "remove test failure" else diff --git a/test/assert.sh b/test/assert.sh index b16484a..6be33c4 100755 --- a/test/assert.sh +++ b/test/assert.sh @@ -50,7 +50,9 @@ assert_eq() { if [ "$expected" == "$actual" ]; then return 0 else - [ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg" || true + if [ "${#msg}" -gt 0 ]; then + log_failure "$expected == $actual :: $msg" + fi return 1 fi } @@ -67,7 +69,9 @@ assert_not_eq() { if [ ! "$expected" == "$actual" ]; then return 0 else - [ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg" || true + if [ "${#msg}" -gt 0 ]; then + log_failure "$expected != $actual :: $msg" + fi return 1 fi } @@ -128,7 +132,9 @@ assert_array_eq() { done if [ "$return_code" == 1 ]; then - [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" || true + if [ "${#msg}" -gt 0 ]; then + log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" + fi fi return "$return_code" @@ -159,7 +165,9 @@ assert_array_not_eq() { done if [ "$return_code" == 1 ]; then - [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" || true + if [ "${#msg}" -gt 0 ]; then + log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" + fi fi return "$return_code"