diff --git a/workflows/auto_command/action/new_document/_setting_.json b/workflows/auto_command/action/new_document/_setting_.json new file mode 100644 index 0000000..444af33 --- /dev/null +++ b/workflows/auto_command/action/new_document/_setting_.json @@ -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}" + ] +} \ No newline at end of file diff --git a/workflows/auto_command/action/run_shell/_setting_.json b/workflows/auto_command/action/run_shell/_setting_.json new file mode 100644 index 0000000..8b04315 --- /dev/null +++ b/workflows/auto_command/action/run_shell/_setting_.json @@ -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}" + ] +} \ No newline at end of file diff --git a/workflows/auto_command/action/run_shell_file/_setting_.json b/workflows/auto_command/action/run_shell_file/_setting_.json new file mode 100644 index 0000000..4ce7170 --- /dev/null +++ b/workflows/auto_command/action/run_shell_file/_setting_.json @@ -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}" + ] +} \ No newline at end of file diff --git a/workflows/auto_command/action/update_file/_setting_.json b/workflows/auto_command/action/update_file/_setting_.json new file mode 100644 index 0000000..03781a3 --- /dev/null +++ b/workflows/auto_command/action/update_file/_setting_.json @@ -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}"] +} \ No newline at end of file diff --git a/workflows/auto_command/action/update_file/handler.py b/workflows/auto_command/action/update_file/handler.py new file mode 100644 index 0000000..64592fd --- /dev/null +++ b/workflows/auto_command/action/update_file/handler.py @@ -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) \ No newline at end of file diff --git a/workflows/default/command/action/_setting_.json b/workflows/default/command/action/_setting_.json new file mode 100644 index 0000000..2ab6005 --- /dev/null +++ b/workflows/default/command/action/_setting_.json @@ -0,0 +1,7 @@ +{ + "pattern": "auto", + "description": "Generate commands for the bot to execute", + "message": "", + "default": false, + "instructions": ["./chat/action.instr"] +} \ No newline at end of file