From a167314ada6abe879b57de18c835ea84d34732d7 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 12 Oct 2012 19:32:32 +0800 Subject: [PATCH] Command to create aliases of MDK commands --- README.md | 11 +++++++++- moodle | 62 ++++++++++++++++++++++++++++++++++++++------------------- moodle-alias.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 22 deletions(-) create mode 100755 moodle-alias.py diff --git a/README.md b/README.md index 1bbdc3b..7973dae 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,16 @@ If you already have instances installed, and your settings are correct, try the Command list ------------ +### - alias + +Set up aliases of your Moodle commands. + +**Example** + +This line defines the alias 'upall', for 'moodle update --all' + + moodle alias add upall "update --all" + ### - backport Backport a branch to another instance of Moodle. @@ -77,7 +87,6 @@ Backports the branch MDL-12345-23 from the instance stable_23 to the instance st moodle backport stable_23 --branch MDL-12345-23 --version 22 --push - ### - backup Backup a whole instance so that it can be restored later. diff --git a/moodle b/moodle index 5dff624..250ff5c 100755 --- a/moodle +++ b/moodle @@ -18,37 +18,57 @@ # # 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 $? diff --git a/moodle-alias.py b/moodle-alias.py new file mode 100755 index 0000000..d2a5050 --- /dev/null +++ b/moodle-alias.py @@ -0,0 +1,62 @@ +#!/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://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) -- 2.11.0