~pi/aka

3df4dafd9586314521f49539af6b4a2c2cdfbaf9 — Paul Ivanov 1 year, 1 month ago 9f74304
implement AKA_FILE
3 files changed, 16 insertions(+), 16 deletions(-)

M README
M aka
M aka_tiny
M README => README +8 -6
@@ 55,12 55,13 @@ Everything that is not aliased will get executed as a regular command.

## How it works

aka gets executed via /bin/sh, sources $AKA_FILE and evals the command, thus
applying aliases and whatever other parameters were stored in $AKA_FILE.
aka gets executed via /bin/sh, sources AKA_FILE and evals the positional
parameters, thus applying aliases and whatever other shell script shenanigans
stored in AKA_FILE.

This means that you can, for example put something like
For example, this means that you can put something like

    alias my='AKA_FILE=~/.my_aliases aka'
    alias my='AKA_FILE=~/.my_rc aka'

in your shell's startup files, and thereafter use the `my` command as an `aka`
invocation that always sources ~/.my_aliases file in your home directory,


@@ 75,7 76,8 @@ establishing their behavior based on the `argv[0]` name they were called by

## Ideas

[ ] AKA_FILE environment variable set to the directory containing the desired
    .aka file, otherwise we look for that file in the current directory.
[x] AKA_FILE environment variable set to the file containing the desired
    shellscript file to source. Otherwise we look for the `.aka` file in the
    current directory.

[ ] `aka unalias` command?

M aka => aka +4 -5
@@ 14,6 14,7 @@ echo "       aka NAME [...]          run CMD, optionally with more arguments"
    esac
fi

aliasFile=${AKA_FILE:-./.aka}
# Is this an alias creation? If so, we should have at least 3 arguments,
# such as:
#


@@ 28,15 29,13 @@ if [ $# -ge 3 ]  && [ $1 = alias ] ; then
    # make 'more' the new $1
    shift 2
    # remove previous aliases for 'pager'
    [ -e .aka ] && grep -v "alias $cmd=" .aka  > .aka~
    echo "alias $cmd='$*'" >> .aka~ && mv .aka~ .aka
    [ -e "$aliasFile" ] && grep -v "alias $cmd=" "$aliasFile"  > "$aliasFile"~
    echo "alias $cmd='$*'" >> "$aliasFile"~ && mv "$aliasFile"~ "$aliasFile"
    exit 0
fi

# Load up aliases...
if [ -e .aka ] ; then
    . ./.aka
fi
[ -e "$aliasFile" ] && . "$aliasFile"

# ...and execute command
eval $*

M aka_tiny => aka_tiny +4 -5
@@ 1,12 1,11 @@
#!/bin/sh
aliasFile=${AKA_FILE:-./.aka}
if [ $# -ge 3 ]  && [ $1 = alias ] ; then
    cmd=$2
    shift 2
    cat .aka | grep -v "alias $cmd=" > .aka
    echo "alias $cmd='$*'" >> .aka
    [ -e "$aliasFile" ] && grep -v "alias $cmd=" "$aliasFile"  > "$aliasFile"~
    echo "alias $cmd='$*'" >> "$aliasFile"~ && mv "$aliasFile"~ "$aliasFile"
    exit 0
fi
if [ -e .aka ] ; then
    . ./.aka
fi
[ -e "$aliasFile" ] && . "$aliasFile"
eval $*