Skip to content

feat(watch): add hb watch command for auto-push on file change - #42

Merged
hirano00o merged 3 commits into
mainfrom
feature/watch
Mar 23, 2026
Merged

feat(watch): add hb watch command for auto-push on file change#42
hirano00o merged 3 commits into
mainfrom
feature/watch

Conversation

@hirano00o

Copy link
Copy Markdown
Owner

Summary

  • Adds hb watch [<file>] and hb watch --dir <dir> to monitor local Markdown files for changes and automatically push to Hatena Blog
  • Debounce (default 500ms, configurable via --debounce) prevents rapid saves from triggering multiple pushes
  • Graceful shutdown via signal.NotifyContext (SIGINT/SIGTERM); displays "Stopped." on exit
  • pushFileFunc package-level variable enables test injection without real API calls
  • New .md files created inside a watched directory are automatically added to the watch set

Test plan

  • TestWatch_MutuallyExclusive--dir + file argument returns an error
  • TestWatch_NoMDFiles — empty directory prints "No .md files found" and exits cleanly
  • TestWatch_FileChange_TriggersPush — writing to a watched file triggers pushFileFunc with the correct path within the debounce window
  • go test ./... all PASS
  • go vet ./... clean

Adds `hb watch [<file>]` and `hb watch --dir <dir>` to monitor local
Markdown files with fsnotify and push changes to Hatena Blog
automatically after a configurable debounce delay (default 500ms).

Graceful shutdown is handled by signal.NotifyContext (SIGINT/SIGTERM).
pushFileFunc is exposed as a package-level variable for test injection.
- Use cmd.Flags().Changed("dir") instead of dir != "." for mutual
  exclusion check so that explicit --dir . is also detected
- Stop all pending debounce timers before returning on ctx.Done()
  to avoid goroutine leaks after shutdown
- Set cmd context from runWatch so pushFile can propagate cancellation
  to push subcommands
- Guard AfterFunc callback with ctx.Err() check
- Add TestWatch_Debounce to verify rapid saves produce only one push
@hirano00o
hirano00o merged commit 972ffc9 into main Mar 23, 2026
2 checks passed
@hirano00o
hirano00o deleted the feature/watch branch March 23, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant