Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#66: Make :sh task a ExtendableTask
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Jul 14, 2021
1 parent 41cf090 commit 9d5db92
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/core/rkd/core/standardlib/shell.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@

from argparse import ArgumentParser
from subprocess import CalledProcessError
from typing import Callable
from typing import Callable, List
from ..api.syntax import TaskDeclaration
from ..api.contract import TaskInterface
from ..api.contract import TaskInterface, ExtendableTaskInterface
from ..api.contract import ExecutionContext


# <sphinx=shell-command>
class ShellCommandTask(TaskInterface):
"""Executes shell scripts"""
class ShellCommandTask(ExtendableTaskInterface):
"""Executes shell commands and scripts"""

# to be overridden in compile()
is_cmd_required: bool

def __init__(self):
self.is_cmd_required = True

def get_name(self) -> str:
return ':sh'

def get_group_name(self) -> str:
return ''

def get_configuration_attributes(self) -> List[str]:
return ['is_cmd_required']

def configure_argparse(self, parser: ArgumentParser):
parser.add_argument('--cmd', '-c', help='Shell command', required=True)
parser.add_argument('--cmd', '-c', help='Shell command', required=self.is_cmd_required)

def execute(self, context: ExecutionContext) -> bool:
# self.sh() and self.io() are part of TaskUtilities via TaskInterface
cmd = ''

if context.get_input():
cmd = context.get_input().read()

if context.get_arg('cmd'):
cmd = context.get_arg('cmd')

try:
self.sh(context.get_arg('cmd'), capture=False)
# self.sh() and self.io() are part of the base class
if cmd:
self.sh(cmd, capture=False)
self.inner_execute(context)

except CalledProcessError as e:
self.io().error_msg(str(e))
return False
Expand Down

0 comments on commit 9d5db92

Please sign in to comment.