diff options
| author | sigoden <sigoden@gmail.com> | 2024-06-08 20:46:27 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-08 20:46:27 +0800 |
| commit | 213e949fc8c2362046d197554fda98c87a7906df (patch) | |
| tree | 595a883c6b1fd1154a916569c3d0255634c6e193 /bots/todo-sh/tools.sh | |
| parent | 82d7a7de8a76e56cff306b0da7f4f14fdb57cbf1 (diff) | |
| download | llm-functions-docker-213e949fc8c2362046d197554fda98c87a7906df.tar.gz | |
feat: support bots (#39)
Diffstat (limited to 'bots/todo-sh/tools.sh')
| -rwxr-xr-x | bots/todo-sh/tools.sh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/bots/todo-sh/tools.sh b/bots/todo-sh/tools.sh new file mode 100755 index 0000000..1b1758a --- /dev/null +++ b/bots/todo-sh/tools.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +set -e + +# @cmd Add a new todo item +# @option --desc! The task description +add_todo() { + todos_file="$(_get_todos_file)" + if [[ -f "$todos_file" ]]; then + num="$(cat "$todos_file" | jq '[.[].id] | max + 1')" + data="$(cat "$todos_file")" + else + num=1 + data="[]" + fi + echo "$data" | \ + jq --arg new_id $num \ + --arg new_desc "$argc_desc" \ + '. += [{"id": $new_id | tonumber, "desc": $new_desc}]' \ + > "$todos_file" + echo "Successfully added todo id=$num" +} + +# @cmd Delete an existing todo item +# @option --id! <INT> The task id +del_todo() { + todos_file="$(_get_todos_file)" + if [[ -f "$todos_file" ]]; then + data="$(cat "$todos_file")" + echo "$data" | \ + jq --arg id $argc_id '[.[] | select(.id != ($id | tonumber))]' \ + > "$todos_file" + echo "Successfully deleted todo id=$argc_id" + else + _die "Empty todo list" + fi +} + +# @cmd Display the current todo list in json format. +list_todos() { + todos_file="$(_get_todos_file)" + if [[ -f "$todos_file" ]]; then + cat "$todos_file" + else + _die "Empty todo list" + fi +} + +# @cmd Delete the entire todo list. +clear_todos() { + todos_file="$(_get_todos_file)" + if [[ -f "$todos_file" ]]; then + rm -rf "$todos_file" + fi + echo "Successfully deleted entry todo list" +} + +_argc_before() { + todos_file="$(_get_todos_file)" + mkdir -p "$(dirname "$todos_file")" +} + +_get_todos_file() { + echo "${LLM_BOT_CACHE_DIR:-/tmp}/todos.json" +} + +_die() { + echo "$*" >&2 + exit 1 +} + +# See more details at https://github.com/sigoden/argc +eval "$(argc --argc-eval "$0" "$@")" |
