Command to create aliases of MDK commands
authorFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 11:32:32 +0000 (19:32 +0800)
committerFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 11:34:04 +0000 (19:34 +0800)
README.md
moodle
moodle-alias.py [new file with mode: 0755]

index 1bbdc3b..7973dae 100644 (file)
--- 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 (executable)
--- a/moodle
+++ b/moodle
 #
 # 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 (executable)
index 0000000..d2a5050
--- /dev/null
@@ -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://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)