refactor: improve bots/todo-* (#41)

This commit is contained in:
sigoden
2024-06-10 19:57:37 +08:00
committed by GitHub
parent 8f3385ca9e
commit 7225ee331a
4 changed files with 8 additions and 21 deletions
+2 -7
View File
@@ -34,7 +34,7 @@ exports.del_todo = function delTodo(args) {
fs.writeFileSync(todosFile, JSON.stringify(newData)); fs.writeFileSync(todosFile, JSON.stringify(newData));
console.log(`Successfully deleted todo id=${args.id}`); console.log(`Successfully deleted todo id=${args.id}`);
} else { } else {
_die('Empty todo list'); console.log('Empty todo list');
} }
} }
@@ -46,7 +46,7 @@ exports.list_todos = function listTodos() {
if (fs.existsSync(todosFile)) { if (fs.existsSync(todosFile)) {
console.log(fs.readFileSync(todosFile, "utf8")); console.log(fs.readFileSync(todosFile, "utf8"));
} else { } else {
_die('Empty todo list'); console.log("[]");
} }
} }
@@ -66,8 +66,3 @@ function _getTodosFile() {
} }
return path.join(cacheDir, 'todos.json'); return path.join(cacheDir, 'todos.json');
} }
function _die(message) {
console.error(message);
process.exit(1);
}
+3 -6
View File
@@ -32,7 +32,8 @@ def del_todo(id: int):
with open(todos_file, "r") as f: with open(todos_file, "r") as f:
data = json.load(f) data = json.load(f)
except (FileNotFoundError, JSONDecodeError): except (FileNotFoundError, JSONDecodeError):
_die("Empty todo list") print("Empty todo list")
return
data = [item for item in data if item["id"] != id] data = [item for item in data if item["id"] != id]
with open(todos_file, "w") as f: with open(todos_file, "w") as f:
json.dump(data, f) json.dump(data, f)
@@ -46,7 +47,7 @@ def list_todos():
with open(todos_file, "r") as f: with open(todos_file, "r") as f:
print(f.read()) print(f.read())
except FileNotFoundError: except FileNotFoundError:
_die("Empty todo list") print("[]")
def clear_todos(): def clear_todos():
@@ -59,7 +60,3 @@ def _get_todos_file() -> str:
if not os.path.exists(cache_dir): if not os.path.exists(cache_dir):
os.makedirs(cache_dir, exist_ok=True) os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, "todos.json") return os.path.join(cache_dir, "todos.json")
def _die(msg: str):
print(msg, file=sys.stderr)
exit(1)
+1 -1
View File
@@ -1,4 +1,4 @@
name: Todo List name: TodoBot
description: Your name is TodoBot and you are a helpful chatbot that manages a todo list. description: Your name is TodoBot and you are a helpful chatbot that manages a todo list.
instructions: | instructions: |
You will be provided with a list of todos. You will be provided with a list of todos.
+2 -7
View File
@@ -31,7 +31,7 @@ del_todo() {
> "$todos_file" > "$todos_file"
echo "Successfully deleted todo id=$argc_id" echo "Successfully deleted todo id=$argc_id"
else else
_die "Empty todo list" echo "Empty todo list"
fi fi
} }
@@ -41,7 +41,7 @@ list_todos() {
if [[ -f "$todos_file" ]]; then if [[ -f "$todos_file" ]]; then
cat "$todos_file" cat "$todos_file"
else else
_die "Empty todo list" echo '[]'
fi fi
} }
@@ -63,10 +63,5 @@ _get_todos_file() {
echo "${LLM_BOT_CACHE_DIR:-/tmp}/todos.json" echo "${LLM_BOT_CACHE_DIR:-/tmp}/todos.json"
} }
_die() {
echo "$*" >&2
exit 1
}
# See more details at https://github.com/sigoden/argc # See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")" eval "$(argc --argc-eval "$0" "$@")"