Skip to content

Commit

Permalink
Merge pull request #169 from sambhavnoobcoder/Shell-Bash-Scripting
Browse files Browse the repository at this point in the history
Command to execute a snippet #35
  • Loading branch information
bishoy-at-pieces authored Aug 21, 2024
2 parents 1a24f3c + c0d8d13 commit 4def71a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pieces/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def add_subparsers(self):
run_parser = self.command_parser.add_parser('run', help='Runs CLI in a loop')
run_parser.set_defaults(func=loop)

# Subparser for the 'execute' command
execute_parser = self.command_parser.add_parser('execute', help='Execute shell or bash assets')
execute_parser.add_argument('max_assets', nargs='?', type=int, default=10, help='Max number of assets to display')
execute_parser.set_defaults(func=ExecuteCommand.execute_command)

# Subparser for the 'edit' command
edit_parser = self.command_parser.add_parser('edit', help='Edit an existing asset')
edit_parser.add_argument('--name',"-n",dest='name', help='New name for the asset', required=False)
Expand Down
78 changes: 78 additions & 0 deletions src/pieces/commands/run_assets_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from pieces.settings import Settings
from pieces.assets import check_assets_existence, AssetsCommandsApi
from typing import List, Tuple, Callable
import subprocess
from enum import Enum
from list_command import PiecesSelectMenu
from pieces.assets.assets_command import AssetsCommands

class AssetClassification(Enum):
SHELL = "sh"
BASH = "bat" # Added BASH classification

class ExecuteCommand:
@classmethod
@check_assets_existence
def execute_command(cls, max_assets: int = 10):
if max_assets < 1:
print("Max assets must be greater than 0")
max_assets = 10
assets_snapshot = AssetsCommandsApi().assets_snapshot
assets = []

for i, uuid in enumerate(list(assets_snapshot.keys())[:max_assets], start=1):
asset = AssetsCommandsApi.get_asset_snapshot(uuid)
classification = cls.get_asset_classification(asset)
if classification in [AssetClassification.SHELL.value, AssetClassification.BASH.value]:
assets.append((f"{i}: {asset.name}", {"ITEM_INDEX": i, "UUID": uuid, "CLASSIFICATION": classification}))

if not assets:
print("No shell or bash assets found")
return

def open_and_execute_asset(**kwargs):
AssetsCommands.open_asset(**kwargs)
cls.execute_asset(**kwargs)

select_menu = PiecesSelectMenu(assets, open_and_execute_asset)
select_menu.run()

@staticmethod
def get_asset_classification(asset):
try:
return asset.original.reference.classification.specific.value
except AttributeError:
return "unknown"

@classmethod
def execute_asset(cls, **kwargs):
uuid = kwargs.get("UUID")
classification = kwargs.get("CLASSIFICATION")
asset = AssetsCommandsApi.get_asset_snapshot(uuid)
asset_dict = AssetsCommandsApi.extract_asset_info(asset)

cls.execute_command_in_subprocess(asset_dict["raw"], classification)

@staticmethod
def execute_command_in_subprocess(command: str, classification: str):
try:
if classification == AssetClassification.BASH.value:
result = subprocess.run(['bash', '-c', command], capture_output=True, text=True)
elif classification == AssetClassification.SHELL.value:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
else:
print(f"Unsupported asset classification: {classification}")
return

print(f"\nExecuting {classification} command:")
print(result.stdout)
if result.stderr:
print("Errors:")
print(result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
except Exception as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
ExecuteCommand.execute_command()

0 comments on commit 4def71a

Please sign in to comment.