-
-
Save guillermo/80f7a1484d7294c77df03cb91b6437af to your computer and use it in GitHub Desktop.
A shell script to run commands through ai
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Check if mods is installed | |
| if ! command -v mods &> /dev/null; then | |
| echo "Error: 'mods' command not found. Please install it first." | |
| exit 1 | |
| fi | |
| # Parse arguments | |
| SHOW_ONLY=false | |
| VERBOSE=false | |
| CONFIRM=false | |
| USER_REQUEST="" | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) | |
| echo "Usage: $0 [--show|-s] [--verbose|-v] [--confirm|-c] <your request>" | |
| echo "Example: $0 'list all files in the current directory sorted by size'" | |
| echo "Options:" | |
| echo " -s, --show Show the command without executing it" | |
| echo " -v, --verbose Show the command before executing it" | |
| echo " -c, --confirm Confirm before executing the command" | |
| echo " -h, --help Show this help message and" | |
| exit 0 | |
| ;; | |
| -s|--show) | |
| SHOW_ONLY=true | |
| shift | |
| ;; | |
| -v|--verbose) | |
| VERBOSE=true | |
| shift | |
| ;; | |
| -e|--explain) | |
| EXPLAIN=true | |
| shift | |
| ;; | |
| -c|--confirm) | |
| CONFIRM=true | |
| VERBOSE=true # Confirm implies verbose | |
| shift | |
| ;; | |
| *) | |
| if [[ -z "$USER_REQUEST" ]]; then | |
| USER_REQUEST="$1" | |
| else | |
| USER_REQUEST="$USER_REQUEST $1" | |
| fi | |
| shift | |
| ;; | |
| esac | |
| done | |
| # Check if user provided a request | |
| if [[ -z "$USER_REQUEST" ]]; then | |
| echo "Error: No request provided." | |
| echo "Usage: $0 [--show|-s] [--verbose|-v] [--confirm|-c] <your request>" | |
| echo "Example: $0 'list all files in the current directory sorted by size'" | |
| exit 1 | |
| fi | |
| # Create the prompt for mods | |
| PROMPT="You need to create a command line that does ${USER_REQUEST}. Assume the requirements to run the command are met. In case of doubt, give the best approximate. Use echo command relevant information to the usert.The shell is bash, and you should only output the command line ready to be executed. Do not include any other text or comments. Do not put the code as a block with backticks, just return the command" | |
| if [[ "$EXPLAIN" = true ]]; then | |
| PROMPT="$PROMPT. Before writting the command, echo and exaplanation of the command. If there are parameters, explain them." | |
| fi | |
| # Call mods with the prompt | |
| COMMAND=$(mods --quiet --raw "$PROMPT") | |
| # Check if mods returned a command | |
| if [[ -z "$COMMAND" ]]; then | |
| echo "Error: Failed to generate a command." | |
| exit 1 | |
| fi | |
| # Either show or execute the command | |
| if [[ "$SHOW_ONLY" = true ]]; then | |
| echo "$COMMAND" | |
| elif [[ "$CONFIRM" = true ]]; then | |
| echo "Command to execute: $COMMAND" | |
| read -p "Do you want to execute this command? (y/n): " CONFIRM_RESPONSE | |
| if [[ "$CONFIRM_RESPONSE" =~ ^[Yy]$ ]]; then | |
| eval "$COMMAND" | |
| else | |
| echo "Command execution cancelled." | |
| fi | |
| else | |
| if [[ "$VERBOSE" = true ]]; then | |
| echo "Executing: $COMMAND" | |
| fi | |
| eval "$COMMAND" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment