cargo_move/commands.rs
1//! CargoMove Subcommands
2//!
3//! This is where you specify the subcommands of your application.
4//!
5//! The default application comes with two subcommands:
6//!
7//! - `start`: launches the application
8//! - `version`: print application version
9//!
10//! See the `impl Configurable` below for how to specify the path to the
11//! application's configuration file.
12
13mod start;
14mod version;
15
16use self::{start::StartCmd, version::VersionCmd};
17use crate::config::CargoMoveConfig;
18use abscissa_core::{
19 config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable,
20};
21use std::path::PathBuf;
22
23/// CargoMove Configuration Filename
24pub const CONFIG_FILE: &str = "cargo_move.toml";
25
26/// CargoMove Subcommands
27#[derive(Command, Debug, Options, Runnable)]
28pub enum CargoMoveCmd {
29 /// The `help` subcommand
30 #[options(help = "get usage information")]
31 Help(Help<Self>),
32
33 /// The `start` subcommand
34 #[options(help = "start the application")]
35 Start(StartCmd),
36
37 /// The `version` subcommand
38 #[options(help = "display version information")]
39 Version(VersionCmd),
40}
41
42/// This trait allows you to define how application configuration is loaded.
43impl Configurable<CargoMoveConfig> for CargoMoveCmd {
44 /// Location of the configuration file
45 fn config_path(&self) -> Option<PathBuf> {
46 // Check if the config file exists, and if it does not, ignore it.
47 // If you'd like for a missing configuration file to be a hard error
48 // instead, always return `Some(CONFIG_FILE)` here.
49 let filename = PathBuf::from(CONFIG_FILE);
50
51 if filename.exists() {
52 Some(filename)
53 } else {
54 None
55 }
56 }
57
58 /// Apply changes to the config after it's been loaded, e.g. overriding
59 /// values in a config file using command-line options.
60 ///
61 /// This can be safely deleted if you don't want to override config
62 /// settings from command-line options.
63 fn process_config(
64 &self,
65 config: CargoMoveConfig,
66 ) -> Result<CargoMoveConfig, FrameworkError> {
67 match self {
68 CargoMoveCmd::Start(cmd) => cmd.override_config(config),
69 _ => Ok(config),
70 }
71 }
72}