#
# http://github.com/FMCorz/mdk
-COMMAND="moodle-$1.py"
-BIN=`which $COMMAND`
+COMMAND=$1
+ARGS=${@:2}
-if [ -z "$BIN" ]
-then
- if [ -n "${BASH_SOURCE[0]}" ]
+# Resolve a command
+function resolve {
+ BIN=`which moodle-$1.py`
+ if [ -z "$BIN" ]
then
- SOURCE="${BASH_SOURCE[0]}"
- DIR="$( dirname "$SOURCE" )"
- while [ -h "$SOURCE" ]
- do
- SOURCE="$(readlink "$SOURCE")"
- [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
- DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
- done
- DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
- BIN="$DIR/$COMMAND"
+ if [ -n "${BASH_SOURCE[0]}" ]
+ then
+ SOURCE="${BASH_SOURCE[0]}"
+ DIR="$( dirname "$SOURCE" )"
+ while [ -h "$SOURCE" ]
+ do
+ SOURCE="$(readlink "$SOURCE")"
+ [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
+ DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+ done
+ DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+ BIN="$DIR/moodle-$1.py"
+ fi
fi
-fi
+ if [ -z "$BIN" ] || [ ! -f "$BIN" ]
+ then
+ exit 1
+ fi
+ echo "$BIN"
+}
-if [ -z "$BIN" ] || [ ! -f "$BIN" ]
+# Checking if an alias exist for that command.
+BIN=`resolve "alias"`
+ALIAS=`$BIN show $COMMAND`
+
+if [ -n "$ALIAS" ]
then
- echo "Unknown Moodle command..."
- exit 1
+ COMMAND=`echo $ALIAS | cut -d ' ' -f 1`
+ ALIASARGS=`echo $ALIAS | cut -d ' ' -f 2-`
+ ARGS="$ALIASARGS $ARGS"
fi
-if [ ! -x "$BIN" ]
+BIN=`resolve $COMMAND`
+
+if [ $? == 1 ]
+then
+ echo "Unknown Moodle command $1..."
+ exit 1
+elif [ ! -x "$BIN" ]
then
echo "Permission denied. $BIN is not executable."
exit 1
fi
-$BIN ${@:2}
+$BIN $ARGS
exit $?
--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Moodle Development Kit
+
+Copyright (c) 2012 Frédéric Massart - FMCorz.net
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+http://github.com/FMCorz/mdk
+"""
+
+import sys
+import argparse
+from lib.tools import debug
+from lib.config import C
+
+# Arguments
+parser = argparse.ArgumentParser(description='Manage your aliases')
+parser.add_argument('command', metavar='command', choices=['list', 'show', 'add', 'remove'], help='the action to perform')
+parser.add_argument('arguments', type=str, metavar='arguments', default=None, nargs=argparse.REMAINDER, help='arguments for the command')
+args = parser.parse_args()
+
+if args.command == 'list':
+ aliases = C.get('aliases')
+ for alias, command in aliases.items():
+ print '{0:<20}: {1}'.format(alias, command)
+
+elif args.command == 'show':
+ if len(args.arguments) != 1:
+ debug('Too few/many arguments. One needed: moodle alias show aliasName')
+ sys.exit(1)
+ alias = C.get('aliases.%s' % args.arguments[0])
+ if alias != None:
+ debug(alias)
+
+elif args.command == 'add':
+ if len(args.arguments) < 2:
+ debug('Too few/many arguments. Two needed: moodle alias add aliasName Command To Perform')
+ sys.exit(1)
+ alias = args.arguments[0]
+ command = ' '.join(args.arguments[1:])
+ C.add('aliases.%s' % alias, command)
+
+elif args.command == 'remove':
+ if len(args.arguments) != 1:
+ debug('Too few/many arguments. One needed: moodle alias remove aliasName')
+ sys.exit(1)
+ alias = args.arguments[0]
+ C.remove('aliases.%s' % alias)