Skip to content

Commit

Permalink
Add CLI command to print the app config
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed Apr 7, 2024
1 parent f7b23b2 commit 1828ba4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use sea_orm_migration::MigratorTrait;
use sidekiq::{periodic, Processor};
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
// `debug` isn't used with some feature configurations
#[allow(unused_imports)]
use tracing::{debug, error, info, instrument};

use crate::app_context::AppContext;
Expand Down Expand Up @@ -61,12 +63,12 @@ where
// combine them with the roadster CLI attributes.
let cli = A::Cli::augment_args(cli);
let cli = if let Some((a, b)) = about.zip(cli.get_about().cloned()) {
cli.about(format!("roadster: {a}, app: {b}"))
cli.about(format!("{a}\n\n{b}"))
} else {
cli
};
let cli = if let Some((a, b)) = long_about.zip(cli.get_long_about().cloned()) {
cli.long_about(format!("roadster: {a}, app: {b}"))
cli.long_about(format!("{a}\n\n{b}"))
} else {
cli
};
Expand Down Expand Up @@ -97,8 +99,6 @@ where

A::init_tracing(&config)?;

debug!("{config:?}");

#[cfg(feature = "db-sql")]
let db = Database::connect(A::db_connection_options(&config)?).await?;

Expand Down
6 changes: 6 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::cli::list_routes::ListRoutesArgs;
use crate::cli::migrate::MigrateArgs;
#[cfg(feature = "open-api")]
use crate::cli::open_api_schema::OpenApiArgs;
use crate::cli::print_config::PrintConfigArgs;
use crate::config::environment::Environment;

#[cfg(feature = "open-api")]
Expand All @@ -17,6 +18,7 @@ pub mod list_routes;
pub mod migrate;
#[cfg(feature = "open-api")]
pub mod open_api_schema;
pub mod print_config;

/// Implement to enable Roadster to run your custom CLI commands.
#[async_trait]
Expand Down Expand Up @@ -139,6 +141,9 @@ pub enum RoadsterSubCommand {
#[cfg(feature = "db-sql")]
#[clap(visible_aliases = ["m", "migration"])]
Migrate(MigrateArgs),

/// Print the AppConfig
PrintConfig(PrintConfigArgs),
}

#[async_trait]
Expand All @@ -154,6 +159,7 @@ where
RoadsterSubCommand::OpenApi(args) => args.run(app, cli, context).await,
#[cfg(feature = "db-sql")]
RoadsterSubCommand::Migrate(args) => args.run(app, cli, context).await,
RoadsterSubCommand::PrintConfig(args) => args.run(app, cli, context).await,
}
}
}
62 changes: 62 additions & 0 deletions src/cli/print_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use async_trait::async_trait;
use clap::Parser;
use serde_derive::{Deserialize, Serialize};
use strum_macros::{EnumString, IntoStaticStr};
use tracing::info;

use crate::app::App;
use crate::app_context::AppContext;
use crate::cli::{RoadsterCli, RunRoadsterCommand};

#[derive(Debug, Parser)]
pub struct PrintConfigArgs {
/// Print the config with the specified format.
#[clap(short, long, default_value = "debug")]
pub format: Format,
}

#[derive(
Debug, Clone, Eq, PartialEq, Serialize, Deserialize, EnumString, IntoStaticStr, clap::ValueEnum,
)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Format {
Debug,
Json,
JsonPretty,
Toml,
TomlPretty,
}

#[async_trait]
impl<A> RunRoadsterCommand<A> for PrintConfigArgs
where
A: App,
{
async fn run(
&self,
_app: &A,
_cli: &RoadsterCli,
context: &AppContext,
) -> anyhow::Result<bool> {
match self.format {
Format::Debug => {
info!("{:?}", context.config)
}
Format::Json => {
info!("{}", serde_json::to_string(&context.config)?)
}
Format::JsonPretty => {
info!("{}", serde_json::to_string_pretty(&context.config)?)
}
Format::Toml => {
info!("{}", toml::to_string(&context.config)?)
}
Format::TomlPretty => {
info!("{}", toml::to_string_pretty(&context.config)?)
}
}

Ok(true)
}
}
5 changes: 4 additions & 1 deletion src/config/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use std::env;
use std::str::FromStr;

use anyhow::anyhow;
#[cfg(feature = "cli")]
use clap::ValueEnum;
use const_format::concatcp;
use serde_derive::{Deserialize, Serialize};
use strum_macros::{EnumString, IntoStaticStr};

use crate::config::app_config::{ENV_VAR_PREFIX, ENV_VAR_SEPARATOR};

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, EnumString, IntoStaticStr)]
#[cfg_attr(feature = "cli", derive(ValueEnum))]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Environment {
Expand All @@ -30,7 +33,7 @@ impl Environment {
// Get the stage, and validate it by parsing to the Environment enum
let environment = env::var(ENV_VAR_WITH_PREFIX)
.map_err(|_| anyhow!("Env var `{ENV_VAR_WITH_PREFIX}` not defined."))?;
let environment = Environment::from_str(&environment).map_err(|err| {
let environment = <Environment as FromStr>::from_str(&environment).map_err(|err| {
anyhow!(
"Unable to parse `{ENV_VAR_WITH_PREFIX}` env var with value `{environment}`: {err}"
)
Expand Down

0 comments on commit 1828ba4

Please sign in to comment.