iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👻

Building my own 'todo' CLI command

に公開

I wanted to manage my todo list from the CLI, so I decided to create my own todo command.
You can find the finished product here:
https://github.com/watarukura/todo

Summarizing the Ideas

  • todo list
    • Display a list of todos
    • Select a todo with fzf and open the file
  • todo cd
    • Move to the todo management directory
  • todo $title
    • Add a todo
    • todo $title $project
      • Since I sometimes work on multiple projects in parallel, I want to be able to set a project as a label
  • todo done
    • Select with fzf, insert today's date into finished_at, and move the file under the done directory
  • Each todo is represented by one file
  • Metadata is managed using front matter
    • title: Title
    • created_at: Date the todo was started
    • finished_at: Date the todo was completed
    • project: Project name
  • No data store other than the todo files is used

Creating a Prototype with Shell Scripts

Front Matter | yq
It seems the yq command can read front matter, so I'll use that.
You can extract from or update front matter like this:

❯ cat <<EOF > sample.md
---
title: hoge
---
body
EOF

❯ yq --front-matter=extract '.title' sample.md
hoge

❯ yq --front-matter=process '. + {"created_at": now} ' sample.md
---
title: hoge
created_at: 2026-02-09T09:02:38+09:00
---
body

Here is the result, which I write into .zshrc.

function todo() {
  local todo_dir="$HOME/Documents/todo"
  local today=$(date +%Y-%m-%d)
  local escaped_filename=$(echo $1 | sed -e 's;/;_;g' -e 's;\x0;;g')

  if [[ $# -eq 1 ]]; then
    if [[ "$1" == "list" ]]; then
      ls "$todo_dir/"*.md |
        xargs basename |
        while read -r todo_file; do
          todo_file="$todo_file" \
          yq --front-matter=extract \
            '[.title, .created_at, .project|"", strenv(todo_file)] |@tsv' \
            "$todo_dir/$todo_file" |
            fzf | cut -f4 | xargs -I{} $EDITOR "$todo_dir/{}"
        done
      return
    fi
    if [[ "$1" == "cd" ]]; then
      cd $todo_dir
      return
    fi
    if [[ "$1" == "done" ]]; then
      donefile=$(ls "$todo_dir/"*.md | xargs -I{} basename {} | fzf)
      today="$today" \
        yq --front-matter=process '. + {"finished_at": strenv(today)}' "$todo_dir/$donefile" -i
      mkdir -p "$todo_dir/done/$today/"
      mv "$todo_dir/$donefile" "$todo_dir/done/$today/"
      return
    fi
    if [[ -e $todo_dir/$1.md ]]; then
      $EDITOR "$todo_dir/$1".md
      return
    fi

    printf -- "---\n{}\n---\n" >"$todo_dir/$escaped_filename.md"
    title="$1" today="$today" \
      yq --front-matter=process \
      '. + {"title": strenv(title), "created_at": strenv(today)}' \
      "$todo_dir/$escaped_filename.md" -i
    $EDITOR "$todo_dir/$escaped_filename".md
  elif [[ $# -eq 2 ]]; then
    printf -- "---\n{}\n---\n" >"$todo_dir/$escaped_filename.md"
    title="$1" today="$today" project="$2" \
      yq --front-matter=process \
      '. + {"title": strenv(title), "created_at": strenv(today), "project": strenv(project)}' \
      "$todo_dir/$escaped_filename.md" -i
    $EDITOR "$todo_dir/$escaped_filename".md
  else
    cd $todo_dir
    printf -- "---\n{}\n---\n" | $EDITOR
    cd -
  fi
}

With this, I have a working implementation.

Letting an AI Agent Read and Translate it to Another Language

It is difficult to write tests for shell scripts, and without tests, the functionality often breaks whenever I add new features.
(Well, it's not that I can't write tests at all, but...)
So, I am rewriting it in Go.
I applied for the preview of Junie-CLI and was able to use it, so I decided to give it a try.

As of 2026-02-08, only English is supported in TUI mode, so I wrote "I want to translate this bash script to go." and pasted the script above.
I also instructed it to write unit tests by saying, "Write down unit test."

Feature Additions and Fixes

The todo cd command, which worked fine with cd $todo_dir in the shell script, does not work when executed from Go.

This case "cd" is the process for the cd subcommand. Since a CLI executed from Go runs in a separate process, calling cd there does not change the working directory of the parent shell. In the original bash script, since it was a "function", cd $todo_dir worked directly, but in Go, that is not possible, so instead, I display the todoDir and return.

https://github.com/twpayne/chezmoi/blob/43ba0344d8465d916a006022a697e215204a90a9/internal/cmd/cdcmd.go
Since this feature was inspired by chezmoi cd, I had Junie read the chezmoi code and explain it to me.

The mechanism of chezmoi cd is not "changing the cwd of the parent shell" but "launching a subshell with the specified directory as the cwd". The flow of the code is as follows:
• In runCDCmd, set CHEZMOI_SUBSHELL=1
• If there is no argument, use the source directory; if there is one, resolve it and determine the directory
• Confirm that the directory exists and is a directory
• Launch a shell with c.run(dir, cdCommand, cdArgs) (launching with the directory as the cwd)

cdCommand / cdArgs can be overridden in settings, but if not set, it launches using the current user shell (shell.CurrentUserShell()). In other words:
• When executing chezmoi cd, a new shell opens in the source directory
• Exiting that shell returns you to the original location

That is the behavior. Since the cwd of the parent shell does not change, the cd-like experience is achieved because "a subshell is opened".

I see...
Indeed, when you chezmoi cd and then exit, you return to the original directory.
Since this behavior doesn't seem inconvenient at all, I will implement the same behavior.

func runSubshell(dir string) {
    // Spawn the user's shell as a subshell in the target directory.
    shell := os.Getenv("SHELL")
    if shell == "" {
    shell = "sh"
    }
    cmd := exec.Command(shell)
    cmd.Dir = dir
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    _ = cmd.Run()
}

Summary

It is great to maintain motivation when the lead time from having an idea to having something usable is short.
For now, I plan to use it for a while.

Discussion