Skip to content

worklog init: starter config writer #28

Description

@Saber5656

Title

worklog init: starter config writer

Summary

Implement worklog init: write the commented starter config
(config.Starter(), issue 05) to the default (or --config) path with
safe-write semantics, refusing to overwrite without --force.

Context

This is the second and last legitimate write path (invariant I3). It reuses
the atomic-write mechanics but targets the config location and must create
~/.config/worklog/ correctly on first run.

Scope

  • internal/cli/init.go (replace stub) + tests.

Detailed Requirements

  1. Target: --config value if given, else config.DefaultPath().
  2. Behavior: existing file + no --force → message
    config already exists: <path> (use --force to overwrite) to stderr,
    exit 1. Otherwise: create parent dirs 0700, atomic temp+rename write,
    file mode 0600 (config may later hold llm.api_key), then print
    wrote <path> to stderr and, to stdout, the 3-line quickstart:
    Edit the config, then run: / worklog doctor / worklog daily.
  3. Symlink final component → refuse (same rule as 22; reuse
    output-package internals via a small exported helper
    output.WriteFileSafe(path string, content []byte, force bool) error
    — refactor 22's core into this helper in THIS issue, keeping 22's tests
    green).
  4. The written file must round-trip: config.Load(path) → zero warnings,
    zero validation errors. This holds because config.Starter() (issue 05)
    ships [llm] enabled = false with the guidance comment — identical to
    the built-in default (DESIGN §5), so no starter-vs-default divergence
    exists.
  5. init ignores --date/--lang etc. (not registered on its FlagSet, 07
    already scoped flags).

Acceptance Criteria

  • Fresh HOME: creates dirs+file with exact Starter() bytes, 0600/0700
    modes, correct stdout/stderr split, exit 0.
  • Existing config without --force → exit 1, file untouched
    (hash-asserted); with --force → replaced atomically.
  • Symlinked config path → refused, exit 1.
  • Round-trip: Load on the written file → no warnings, no validation
    errors.
  • XDG_CONFIG_HOME respected when set (test both with and without).
  • 22's test suite still green after the WriteFileSafe refactor.

Validation

go test -race -cover ./internal/cli/ ./internal/output/ ./internal/config/
in PR (three packages touched).

Dependencies

05, 07, 22 (hard dependency: this issue refactors 22's write core into the
shared output.WriteFileSafe helper).

Non-goals

Interactive prompts/wizards, config migration between versions (post-v1),
editing existing configs.

Design References

  • docs/DESIGN.md §5, §10, §1.2 (I3)

Source of truth: docs/issues/27-cmd-init.md (PR #1, branch docs/v1-design). If this issue and the repo docs disagree, the docs win. Execution order and dependencies: docs/ISSUE_PLAN.md (this is issue 27 of 33).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions