From b8b3b17392d6ed087d1fdcb81a00722f95fbe32f Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 27 May 2024 11:23:36 +0800 Subject: [PATCH] Refactor log.py to handle file insertions in CLI This commit refactors the log.py file in the _cli directory to handle file insertions in the command-line interface (CLI). It introduces a new functionality where if the `insert` parameter is a file path, the content of the file is read and assigned to the `insert` variable. Additionally, the file is deleted after its content is read. --- devchat/_cli/log.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/devchat/_cli/log.py b/devchat/_cli/log.py index 70277d5a..5a956ad4 100755 --- a/devchat/_cli/log.py +++ b/devchat/_cli/log.py @@ -1,4 +1,5 @@ import json +import os import sys import time from dataclasses import dataclass, field @@ -41,6 +42,17 @@ def log(skip, max_count, topic_root, insert, delete): logger = get_logger(__name__) + # handler insert + # if insert is a file path, then read file content as "insert" value, then delete that file + try: + if os.path.isfile(insert): + insert_file = insert + with open(insert_file, "r", encoding="utf-8") as f: + insert = f.read() + os.remove(insert_file) + except Exception: + pass + if (insert or delete) and (skip != 0 or max_count != 1 or topic_root is not None): print( "Error: The --insert or --delete option cannot be used with other options.",