Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4376db037d
Fetching contributors…

Cannot retrieve contributors at this time

executable file 171 lines (144 sloc) 3.969 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#!/bin/bash

VERSION_NUMBER=0.2.0
declare -a RESERVED_WORDS=('--help' '-h' '--version' '-v' 'init' 'edit' 'rm' 'ls' 'xfiles' '--use-history' '-uh')
CMD_FILES=.

usage() {
cat << EOF
Usage: xf command [args]

COMMANDS:
--help, -h
Show this message

--version, -v
Show version

init <path>
Initialize cmd project. Uses current working
directory if no path is given.

<commandname> [options]
Run the specified command

edit <commandname> [options]
Edit the specified command
--use-history, -uh
Appends the tail of ~/.bash_history to the
command before editing.

rm <commandname>
Remove the specified command

ls
List available commands

xfiles
Show location of .xfiles
EOF
}

set_cmd_files() {
    while [[ ! -a .xfiles ]]
    do
        cd ..
        # using dev/null because it always? exists at root
        # need to find a better way of identifying root directory
        if [[ -a dev/null ]]
        then
            echo "ERROR: No .xfiles found"
            exit 1 # no cmd files found
        fi
    done
    CMD_FILES=./.xfiles
}

exit_no_cmd_specified() {
    echo "ERROR: No command name specified"
    usage
    exit 1 # can't rm a file that isn't specified
}

exit_reserved_word() {
    echo "ERROR: '$1' is a reserved word and cannot be used as a cmd name"
    exit 1 # can't create the cmd because the cmdname is a reserved word
}

exit_no_such_cmd() {
    echo "ERROR: No such command"
    exit 1 # no command to run
}

show_cmd_version() {
    echo "xf v$VERSION_NUMBER"
}

# handle options

if [[ -z "$1" ]] # if no arguments were given, print usage
then
    show_cmd_version
    usage
    exit 1
elif [[ $1 = "--help" ]] || [[ $1 = "-h" ]]
then
    usage
    exit 0
elif [[ $1 = "--version" ]] || [[ $1 = "-v" ]]
then
    show_cmd_version
    exit 0
elif [[ $1 = "init" ]]
then
    # use specified path if it exists
    if [[ -z "$2" ]]
    then
        PROJECT_PATH=.
    else
        PROJECT_PATH=$2
    fi

    mkdir $PROJECT_PATH/.xfiles
    exit 0
else
    set_cmd_files
    if [[ $1 = "edit" ]]
    then
        # a commandname must be given
        if [[ -z "$2" ]] ; then exit_no_cmd_specified ; fi

        # command name can not be a reserved name
        for reserved_word in ${RESERVED_WORDS[@]}
        do
            if [[ $reserved_word = $2 ]]
            then
                exit_reserved_word $2
            fi
        done

        # the $EDITOR environment variable must be specified
        if [[ -z "$EDITOR" ]]
        then
            echo "ERROR: Set the \$EDITOR environment variable to your preferred text editor"
            exit 1 # can't edit the cmd
        fi

        # if the command doesn't exist create it
        if [[ ! -a $CMD_FILES/$2 ]]
        then
            touch $CMD_FILES/$2
            chmod +x $CMD_FILES/$2
        fi

        # if the tail history flag was set, append the bash
        # history to the end of the command
        if [[ $3 = "--use-history" ]] || [[ $3 = "-uh" ]]
        then
            tail ~/.bash_history >> $CMD_FILES/$2
        fi

        $EDITOR $CMD_FILES/$2
        exit 0
    elif [[ $1 = "rm" ]]
    then
        # a commandname must be given
        if [[ -z "$2" ]] ; then exit_no_cmd_specified ; fi

        # check if command exists
        if [[ ! -a $CMD_FILES/$2 ]] ; then exit_no_such_cmd ; fi

        rm $CMD_FILES/$2
        exit 0
    elif [[ $1 = "ls" ]]
    then
        ls $CMD_FILES
        exit 0
    elif [[ $1 = "xfiles" ]]
    then
        cd $CMD_FILES
        pwd
exit 0
    else
        # check if command exists
        if [[ ! -a $CMD_FILES/$1 ]] ; then exit_no_such_cmd ; fi

        # run command with any extra args passed in
        $CMD_FILES/$1 ${@:2:$#}
exit 0
fi
fi
Something went wrong with that request. Please try again.