Skip to content

Commit

Permalink
added command chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost-ng committed Jan 1, 2024
1 parent b7f13a3 commit 95f6373
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ pip install slinger-_._._.tar.gz
### Service Control
- sc modify
### MISC
- command chaining
- run a slinger script file (from a script folder)
## Contributing
### Creating Your Own Plugin for Slinger
Expand Down
62 changes: 29 additions & 33 deletions src/slinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from slingerpkg.utils.printlib import *
from slingerpkg.lib.slingerclient import SlingerClient
from slingerpkg.utils.common import get_config_value, set_config_value, run_local_command, show_config
from slingerpkg.utils.cli import setup_cli_parser, get_prompt, CommandCompleter, setup_completer, merge_parsers, force_help
from slingerpkg.utils.cli import setup_cli_parser, get_prompt, CommandCompleter, setup_completer, merge_parsers, force_help, file_to_slinger_script
from slingerpkg.lib.plugin_base import load_plugins
from slingerpkg.var.config import version
import shlex, argparse, sys, os, pty, termios
Expand Down Expand Up @@ -101,14 +101,18 @@ def main():
print_debug('',sys.exc_info())
sys.exit()


slingerQueue = []
while True:
try:
prompt_text = get_prompt(slingerClient, prgm_args.nojoy)

try:
#formatted_text(ANSI(prompt_text),end='')
user_input = session.prompt(to_formatted_text(ANSI(prompt_text)))
if slingerQueue:
user_input = slingerQueue.pop(0)
else:
user_input = session.prompt(to_formatted_text(ANSI(prompt_text)))

logwriter.info(user_input)
split = shlex.split(user_input)
#user_input = prompt('', history=history)
Expand Down Expand Up @@ -188,37 +192,29 @@ def main():
elif args.command == "exit":
slingerClient.exit()
break


elif args.command == "run":

if args.file:
file_path = os.path.expanduser(args.file)
if not os.path.exists(file_path):
print_warning(f"File {args.file} does not exist")
continue
lines = file_to_slinger_script(file_path)

for line in lines:
slingerQueue.append(line)

elif args.cmd_chain:
if ";" in args.cmd_chain:
lines = args.cmd_chain.split(";")

for line in lines:
slingerQueue.append(line)
else:
print_warning("Invalid command sequence. Use ';' to separate commands")

# elif args.command== "upload" or args.command == "put":
# remote_path = ""
# if slingerClient.check_if_connected():
# if args.remote_path == "." or args.remote_path == "" or args.remote_path is None:
# remote_path = os.path.basename(args.local_path)
# else:
# remote_path = args.remote_path
# if os.path.exists(args.local_path):
# print_info(f"Uploading: {args.local_path} --> {slingerClient.share}\\{remote_path}")
# slingerClient.upload(args.local_path, remote_path)
# else:
# print_warning(f"Local path {args.local_path} does not exist.")

# elif args.command == "download" or args.command == "get":
# # handle if remote_path is a file name (relative to current path)
# remote_path = os.path.normpath(os.path.join(slingerClient.relative_path, args.remote_path))
# local_path = ""
# if slingerClient.check_if_connected():
# if not slingerClient.file_exists(args.remote_path):
# print_warning(f"Remote file {args.remote_path} does not exist.")
# continue
# if args.local_path == "." or args.local_path == "" or args.local_path is None:
# local_path = os.path.join(os.getcwd(), os.path.basename(args.remote_path))
# else:
# local_path = args.local_path
# if os.path.isdir(os.path.dirname(local_path)):
# print_info(f"Downloading: {slingerClient.share}\\{remote_path} --> {local_path}")
# slingerClient.download(remote_path, local_path)
# else:
# print_warning(f"Local path {args.local_path} does not exist.")
else:
pass
except Exception as e:
Expand Down
28 changes: 27 additions & 1 deletion src/slingerpkg/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def force_help(parser, command):
else:
print(f"No command named '{command}' found")

class InvalidParsing(Exception):
pass

class CustomArgumentParser(argparse.ArgumentParser):

Expand Down Expand Up @@ -69,9 +71,13 @@ def parse_args(self, args=None, namespace=None):
msg = 'unrecognized arguments: %s'
self.error(msg % ' '.join(argv))
return args



def error(self, message):
if 'invalid choice' in message:
print_log('Invalid command entered. Type help for a list of commands.')
raise InvalidParsing('Invalid command entered. Type help for a list of commands.')
#super().error(message)

def show_command_help(parser, command):
Expand All @@ -92,7 +98,7 @@ def show_command_help(parser, command):

def setup_cli_parser(slingerClient):
parser = CustomArgumentParser(prog=program_name, description='In App Commands')
parser.add_argument('-v', '--version', action='version', version='%(prog)s '+version, help='Show the version number and exit')
parser.add_argument('--version', action='version', version='%(prog)s '+version, help='Show the version number and exit')

subparsers = parser.add_subparsers(dest='command')

Expand Down Expand Up @@ -342,8 +348,28 @@ def setup_cli_parser(slingerClient):

parser_setvar = subparsers.add_parser('config', help='Show the current config', description='Show the current config', epilog='Example Usage: config')

parser_run = subparsers.add_parser('run', help='Run a slinger script or command sequence', description='Run a slinger script or command sequence', epilog='Example Usage: run -c|-f [script]')
#parser_run.add_argument('-v', '--validate', help='Validate the script or command sequence without running it', action='store_true')
parser_rungroup = parser_run.add_mutually_exclusive_group(required=True)
parser_rungroup.add_argument('-c', '--cmd_chain', help='Specify a command sequence to run')
parser_rungroup.add_argument('-f', '--file', help='Specify a script file to run')

return parser

# def validate_args(parser, arg_list):
# try:
# args = parser.parse_args(arg_list)
# except InvalidParsing:
# return False
# pass


def file_to_slinger_script(file_path):
script = ""
with open(file_path, 'r') as file:
script = file.read().splitlines()
return script

def get_subparser_aliases(command, parser):
for action in parser._actions:
if isinstance(action, argparse._SubParsersAction):
Expand Down

0 comments on commit 95f6373

Please sign in to comment.