Skip to content

Comments

Refactor zainod CLI: add generate-config command and XDG config support#854

Merged
fluidvanadium merged 12 commits intodevfrom
refactor/minimal-main
Feb 20, 2026
Merged

Refactor zainod CLI: add generate-config command and XDG config support#854
fluidvanadium merged 12 commits intodevfrom
refactor/minimal-main

Conversation

@nachog00
Copy link
Contributor

@nachog00 nachog00 commented Feb 18, 2026

This update refactors the zainod CLI to utilize explicit subcommands, introduces a generate-config utility, and adopts the XDG Base Directory specification for improved configuration management.


Breaking Changes

The CLI now requires a specific subcommand to operate.

  • Previous usage: zainod --config path.toml
  • New usage: zainod start or zainod start -c path.toml

Important

Running zainod without a subcommand will now print the help menu and exit. All automated integrations or scripts must be updated to include the start command.


Core Benefits

  • Effortless Setup: The generate-config command allows users to create a valid, up-to-date configuration file instantly.
  • Reduced Maintenance: Removed zindexer.toml. Configuration is now generated directly from the codebase, ensuring the "example" never drifts from actual default values.
  • Cleaner Filesystems: By following XDG standards, the daemon keeps user home directories organized and compliant with modern Linux standards.
  • Better Docs: Configuration options are now documented via doc comments on internal types, making them easily accessible through cargo docs.

Detailed Changes

CLI & Configuration

  • Subcommand Restructure: Migrated from flags to subcommands. The start command now explicitly represents the daemon's behavior.
  • XDG Compliance: Default config path is now $XDG_CONFIG_HOME/zaino/zainod.toml (falling back to ~/.config/zaino/zainod.toml).
  • New Utility: Added zainod generate-config, which writes the default setup to the XDG path or a custom location via the -o flag.

Technical Fixes

  • TOML Serialization: Resolved several serde issues:
  • 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

  • Migrated all configuration descriptions from zindexer.toml into code-level doc comments.
  • Updated use_cases.md to 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.

- 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.
@nachog00 nachog00 added documentation Improvements or additions to documentation Configs Related to overall configuration of zaino zainod problems that concern zainod (binary) labels Feb 18, 2026
This was linked to issues Feb 18, 2026
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().
@nachog00
Copy link
Contributor Author

Additional work addressing #807:

The last three commits address issues 1 and 2 from #807:

  • 8d1e0b4: run() now returns Result<(), IndexerError> instead of handling exits internally. The binary decides exit behavior, and config loading errors are reported gracefully rather than panicking.

  • 01d363b: Introduces zaino-common::xdg module with centralized XDG Base Directory utilities. Path resolution follows the policy: $XDG_VAR -> $HOME/.{subdir} -> /tmp/zaino/.{subdir}. This eliminates panics when HOME is unset.

  • a37b96a: Updates DatabaseConfig::default() to use resolve_path_with_xdg_cache_defaults("zaino") instead of the hardcoded relative path ./zaino_cache.

Issue 3 from #807 (documentation referencing non-existent no_db option) requires further investigation and will be addressed in a separate issue.

@nachog00
Copy link
Contributor Author

nachog00 commented Feb 19, 2026

>>> Running tests using:
>>> -- IMAGE             = zingodevops/zaino-ci
>>> -- TAG               = RUST_1.92-ZCASH_6.3.0-ZEBRA_3.1.0-DOCKER_9229da2adb2857

Summary [1557.166s] 330 tests run: 330 passed (54 slow), 32 skipped

@nachog00 nachog00 mentioned this pull request Feb 20, 2026
Copy link
Contributor

@fluidvanadium fluidvanadium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-reviewed.

Command-tested.

@fluidvanadium fluidvanadium merged commit 039ca21 into dev Feb 20, 2026
5 of 10 checks passed
Comment on lines +42 to +50
// /// `XDG_DATA_HOME` - User data files.
// ///
// /// Default: `$HOME/.local/share`
// Data,

// /// `XDG_STATE_HOME` - Persistent state (logs, history).
// ///
// /// Default: `$HOME/.local/state`
// State,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be very careful about using these: https://bsky.app/profile/str4d.xyz/post/3lsjbnpsbh22i

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Configs Related to overall configuration of zaino documentation Improvements or additions to documentation zainod problems that concern zainod (binary)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Config improvements: error handling and documentation updates zainod config example is outdated generate-config command

3 participants