Refactor zainod CLI: add generate-config command and XDG config support#854
Refactor zainod CLI: add generate-config command and XDG config support#854fluidvanadium merged 12 commits intodevfrom
Conversation
- Extract CLI parsing to dedicated cli.rs module with Command enum - Move indexer loop and logging init from main.rs to lib.rs run() - main.rs now only parses CLI and dispatches to command handlers - Add generate-config subcommand for creating default config files - Require explicit subcommand: `zainod run` or `zainod generate-config`
- Add GENERATED_CONFIG_HEADER const to lib.rs - Move generate_default_config() function to lib.rs for testability - cli.rs now delegates to lib.rs for config generation logic - DEFAULT_CONFIG_PATH stays in cli.rs as it's a binary concern
- DatabaseSize: simplify from enum to newtype struct with #[serde(transparent)] - Network: add serde into/from for consistent string serialization - ZainodConfig: reorder fields (values before tables) per TOML spec Enables `zainod generate-config` to produce valid TOML output.
Ensures serialize → deserialize → serialize produces stable output. Catches regressions in field ordering and custom serde impls.
Relocates GENERATED_CONFIG_HEADER, generate_default_config(), and related tests from lib.rs to config.rs where they belong.
Default config location is now $XDG_CONFIG_HOME/zaino/zainod.toml, falling back to ~/.config/zaino/zainod.toml if XDG_CONFIG_HOME is unset.
- Renamed 'run' subcommand to 'start' (better conveys daemon behavior) - generate-config now writes to default XDG config path when -o is omitted - Creates parent directories automatically if needed
Reflects 'zainod start' subcommand and generate-config usage.
Moved useful documentation from zindexer.toml into doc comments on config structs (ValidatorConfig, BackendType, CacheConfig, ZainodConfig). Removed zindexer.toml - users should use 'zainod generate-config' instead. This avoids maintenance burden of keeping an example file in sync with code changes. Config documentation now lives in cargo docs.
Move error handling responsibility to the binary. The run() function now returns Result<(), IndexerError> instead of handling exits internally. This allows the binary to decide how to handle errors (exit code, logging). Addresses issue #807 point 1: config failures now exit gracefully with clear error messages instead of panicking.
Add centralized XDG path resolution policy in zaino-common/src/xdg.rs.
This provides consistent default path handling across Zaino:
- XdgDir enum with Config and Cache variants
- resolve_path_with_xdg_{config,cache}_defaults() convenience functions
- Fallback chain: $XDG_VAR -> $HOME/.{subdir} -> /tmp/zaino/.{subdir}
Update zainod to use these utilities:
- cli.rs: default_config_path() now uses XDG config defaults
- config.rs: default_zaino_db_path() and default_zebra_db_path() use XDG cache defaults
Fixes issue #807 point 2: zebra_db_path no longer panics on missing HOME.
Instead of failing at config construction, invalid paths are detected at
runtime when actually accessed, providing clearer error messages.
Update DatabaseConfig::default() in zaino-common to use
resolve_path_with_xdg_cache_defaults("zaino") instead of
the hardcoded relative path "./zaino_cache".
This ensures consistent XDG-compliant paths across all storage defaults.
Remove now-redundant default_zaino_db_path() from zainod config and
simplify ZainodConfig::default() to use StorageConfig::default().
|
Additional work addressing #807: The last three commits address issues 1 and 2 from #807:
Issue 3 from #807 (documentation referencing non-existent |
|
fluidvanadium
left a comment
There was a problem hiding this comment.
Code-reviewed.
Command-tested.
| // /// `XDG_DATA_HOME` - User data files. | ||
| // /// | ||
| // /// Default: `$HOME/.local/share` | ||
| // Data, | ||
|
|
||
| // /// `XDG_STATE_HOME` - Persistent state (logs, history). | ||
| // /// | ||
| // /// Default: `$HOME/.local/state` | ||
| // State, |
There was a problem hiding this comment.
Be very careful about using these: https://bsky.app/profile/str4d.xyz/post/3lsjbnpsbh22i
There was a problem hiding this comment.
I don't have an account on that platform. Could you elaborate on your concerns? I understand they revolve around the XDG_DATA_HOME specifically.
This update refactors the
zainodCLI to utilize explicit subcommands, introduces agenerate-configutility, and adopts the XDG Base Directory specification for improved configuration management.Breaking Changes
The CLI now requires a specific subcommand to operate.
zainod --config path.tomlzainod startorzainod start -c path.tomlImportant
Running
zainodwithout a subcommand will now print the help menu and exit. All automated integrations or scripts must be updated to include thestartcommand.Core Benefits
generate-configcommand allows users to create a valid, up-to-date configuration file instantly.zindexer.toml. Configuration is now generated directly from the codebase, ensuring the "example" never drifts from actual default values.cargo docs.Detailed Changes
CLI & Configuration
startcommand now explicitly represents the daemon's behavior.$XDG_CONFIG_HOME/zaino/zainod.toml(falling back to~/.config/zaino/zainod.toml).zainod generate-config, which writes the default setup to the XDG path or a custom location via the-oflag.Technical Fixes
serdeissues:DatabaseSize: Simplified to a newtype struct.Network: Added a bidirectional serde helper.ZainodConfig: Reordered fields to ensure values appear before tables, satisfying TOML requirements.Documentation
zindexer.tomlinto code-level doc comments.use_cases.mdto reflect the new CLI syntax.Testing
To ensure stability, the following tests were implemented:
test_generate_default_config_produces_valid_toml: Validates the output of the generation tool.test_config_roundtrip_serialize_deserialize: Prevents regressions in field ordering and serialization logic.