The cmd module serves as the command-line interface (CLI) orchestration layer. It manages the lifecycle of project documentation generation by coordinating user input, configuration persistence, git repository validation, and the execution of the underlying documentation engine.
- Input Acquisition: CLI arguments are parsed via
RootCmd. User-provided flags (--model-id,--num-ctx) and interactive input (os.Stdin) are captured. - Environment Validation: The module verifies the presence of a git repository and determines the current working directory via
os.Getwd. - State Assessment: The module checks for existing configuration files and determines if the project is in an initialized state to validate the requested operation (
initvs.update). - Configuration Resolution: Configuration is loaded from disk, merged with defaults, and optionally updated via an interactive setup flow.
- Execution Dispatch: The validated configuration and mode are passed to the
enginepackage to perform documentation generation or incremental updates. - Output Stream: Results, status updates, and errors are streamed to
stdoutandstderr.
The central logic for the CLI lifecycle is contained within this file.
RootCmd(*cobra.Command): The entry point for the CLI application.executeCommand(mode engine.Mode) error: The primary controller. It performs the following sequence:- Resolves the working directory.
- Verifies the git repository status.
- Triggers
checkAndRunSetupif configuration is missing andos.Stdinis a TTY. - Resolves the final configuration.
- Validates that the current project state matches the requested
engine.Mode. - Dispatches execution to
runEngine.
checkAndRunSetup(repoRoot string) error: Evaluates ifRunSetupFlowis required based on the existence of configuration files and terminal availability.checkInitStatus(repoRoot, docsDir string, mode engine.Mode) error: Enforces state transition rules:ModeInitis prohibited if the project is already initialized.ModeUpdateis prohibited if the project is not yet initialized.
runEngine(repoRoot string, cfg *config.Config, mode engine.Mode) error: Orchestrates engine execution with signal handling foros.Interruptandsyscall.SIGTERM.
These files act as thin wrappers that map CLI subcommands to specific engine modes.
initsubcommand: Dispatchesengine.ModeInitto trigger the initial scanning of the repository and generation of wiki markdown files.updatesubcommand: Dispatchesengine.ModeUpdateto perform incremental updates by comparing current file states against the last documented git commit.
Handles the interactive provisioning of project settings.
RunSetupFlow(repoRoot string) error: Manages a stateful, interactive session to collect:- LLM Model ID.
- Ollama Base URL.
- Ollama Context Size.
- File/directory ignore patterns.
- Documentation directory paths.
- Persistence: Finalized settings are consolidated into a
config.Configobject and written to disk viaconfig.SaveConfig.
- Global Flags:
modelIDFlagandnumCtxFlagare modified during theinit()phase and persist for the lifetime of the process. - Command State:
RootCmdstate is modified via.SetArgs()and.Execute()during testing.
- Filesystem:
- Directory/file creation via
os.MkdirAllandos.WriteFile. - Configuration persistence via
config.SaveConfigandconfig.LoadConfig. - Git verification via
tools.VerifyGitRepo. - State check via
engine.IsInitialized.
- Directory/file creation via
- Standard Streams:
os.Stdin: Used for interactive parameter collection.os.Stdout: Used for status messages and engine events.os.Stderr: Used for error reporting.
- Propagation: Errors are passed up the call stack via return values.
- Wrapping:
os.Getwdandrunner.Runerrors are wrapped with context using%w. - Swallowed Errors:
config.LoadConfigerrors are swallowed inRunSetupFlow(defaults are returned instead).strconv.Atoierrors inpromptContextSizeare swallowed (default values are returned instead).RootCmd.Execute()return values are swallowed incmd_test.go.