Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for chained aliases #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions mdk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def main():
from .command import CommandRunner
from .commands import getCommand, commandsList
from .config import Conf
from .tools import process
from .version import __version__

C = Conf()
Expand Down Expand Up @@ -78,20 +77,49 @@ def main():
# Looking up for an alias
alias = C.get('aliases.%s' % cmd)
if alias != None:
if alias.startswith('!'):
cmd = alias[1:]
i = 0
# Replace $1, $2, ... with passed arguments
for arg in args:
i += 1
cmd = cmd.replace('$%d' % i, arg)
# Remove unknown $[0-9]
cmd = re.sub(r'\$[0-9]', '', cmd)
result = process(cmd, stdout=None, stderr=None)
sys.exit(result[0])
else:
cmd = alias.split(' ')[0]
args = alias.split(' ')[1:] + args
runAlias(alias, args)
else:
runCommand(cmd, args)

def runAlias(alias, args):
import sys
import logging
import re
from .tools import process

if type(alias) is list:
result = 0
for subAlias in alias:
result = runAlias(subAlias, args)
if result and result != 0:
sys.exit(result)
sys.exit(result)

if alias.startswith('!'):
cmd = alias[1:]
i = 0
# Replace $1, $2, ... with passed arguments
for arg in args:
i += 1
cmd = cmd.replace('$%d' % i, arg)
# Remove unknown $[0-9]
cmd = re.sub(r'\$[0-9]', '', cmd)
return process(cmd, stdout=None, stderr=None)
else:
cmd = alias.split(' ')[0]
args = alias.split(' ')[1:] + args

return runCommand(cmd, args)

def runCommand(cmd, args):
import sys
import os
from .command import CommandRunner
from .commands import getCommand, commandsList
from .config import Conf
import logging

C = Conf()

cls = getCommand(cmd)
Cmd = cls(C)
Expand Down