Add new actions and settings for auto_command workflow

- Add new_file action for creating or replacing files.
- Add run_shell action for running shell scripts.
- Add run_shell_file action for running shell script files.
- Add update_file action for replacing file content.
- Add default command action settings for auto pattern.
This commit is contained in:
bobo.yang 2023-07-24 00:11:56 +08:00
parent 63ea89387b
commit 2be4e52a0a
6 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,28 @@
{
"name": "new_file",
"description": "create new file or replace file with specified content",
"type": [
"*"
],
"args": [
{
"name": "fileName",
"description": "target file name to create",
"type": "string",
"from": "content.fileName"
},
{
"name": "content",
"description": "content to write to file",
"type": "string",
"as": "file",
"from": "content.content"
}
],
"action": "new_file",
"handler": [
"cp",
"${content}",
"${fileName}"
]
}

View File

@ -0,0 +1,19 @@
{
"name": "run_shell",
"description": "run shell script",
"type": [
"shell-singleline"
],
"args": [
{
"name": "script",
"description": "shell command to run, for example: [\"ls\", \"/tmp\"]",
"type": "string",
"from": "content.content"
}
],
"action": "run_shell",
"handler": [
"${script}"
]
}

View File

@ -0,0 +1,21 @@
{
"name": "run_shell",
"description": "run shell script",
"type": [
"shell"
],
"args": [
{
"name": "script",
"description": "shell script file to run",
"type": "string",
"as": "file",
"from": "content.content"
}
],
"action": "run_shell",
"handler": [
"bash",
"${script}"
]
}

View File

@ -0,0 +1,23 @@
{
"name": "update_file",
"description": "replace selections of file with specified content",
"type": ["update"],
"args": [{
"name": "fileName",
"description": "target file name to update",
"type": "string",
"from": "content.fileName"
}, {
"name": "old_content",
"description": "old content to replaced",
"type": "string",
"from": "content.content.old"
}, {
"name": "content",
"description": "content to write to file",
"type": "string",
"from": "content.content.new"
}],
"action": "update_file",
"handler": ["python", "${CurDir}/handler.py", "${fileName}", "${old_content}", "${content}"]
}

View File

@ -0,0 +1,40 @@
import sys
def replace_text_in_file(fileName: str, old_content: str, newContent: str) -> None:
"""
Replace text in a file within the specified range with new content.
:param fileName: The name of the file to modify.
:param startPos: The starting position of the range to replace.
:param endPos: The ending position of the range to replace.
:param newContent: The new content to replace the specified range with.
"""
with open(fileName, 'r') as file:
content = file.read()
# how many times old_content occurs in content
count = content.count(old_content)
# if count is not 1, then we can't replace the text
if count != 1:
# output error message to stderr and exit
print(f"Error: {old_content} occurs {count} times in {fileName}.", file=sys.stderr)
exit(-1)
# replace old_content with new_content
modified_content = content.replace(old_content, new_content)
with open(fileName, 'w') as file:
file.write(modified_content)
if __name__ == "__main__":
try:
file_name = sys.argv[1]
old_content = sys.argv[2]
new_content = sys.argv[3]
replace_text_in_file(file_name, old_content, new_content)
except Exception as e:
print(e, file=sys.stderr)
exit(-1)
exit(0)

View File

@ -0,0 +1,7 @@
{
"pattern": "auto",
"description": "Generate commands for the bot to execute",
"message": "",
"default": false,
"instructions": ["./chat/action.instr"]
}