Skip to content

Commit

Permalink
Add performance tracing to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 23, 2024
1 parent 0b9bb13 commit 292c11a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
66 changes: 44 additions & 22 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use aftman::storage::Home;
use anyhow::{Context, Result};
use clap::Parser;
use tokio::time::Instant;

mod debug_system_info;
mod debug_trusted_tools;
Expand All @@ -16,14 +17,46 @@ use self::untrust::UntrustSubcommand;

#[derive(Debug, Parser)]
#[clap(author, version, about)]
pub struct Args {
pub struct Cli {
#[clap(subcommand)]
pub subcommand: Subcommand,
}

impl Args {
impl Cli {
pub async fn run(self) -> Result<()> {
self.subcommand.run().await
// 1. Load aftman data structures
let start_home = Instant::now();
let home = Home::load_from_env().await.context(
"Failed to load Aftman home!\
\nYour installation or environment may be corrupted.",
)?;
tracing::trace!(
elapsed = ?start_home.elapsed(),
"Aftman loaded"
);

// 2. Run the subcommand and capture the result
let start_command = Instant::now();
let result = self.subcommand.run(&home).await;
tracing::trace!(
elapsed = ?start_command.elapsed(),
success = result.is_ok(),
"Aftman ran",
);

// 3. Save aftman data structures to disk
let start_save = Instant::now();
home.save().await.context(
"Failed to save Aftman data!\
\nChanges to trust, tools, and more may have been lost.",
)?;
tracing::trace!(
elapsed = ?start_save.elapsed(),
"Aftman saved"
);

// 4. Return the result of the subcommand
result
}
}

Expand All @@ -41,26 +74,15 @@ pub enum Subcommand {
}

impl Subcommand {
pub async fn run(self) -> Result<()> {
let home = Home::load_from_env()
.await
.context("Failed to load Aftman home!")?;

let result = match self {
pub async fn run(self, home: &Home) -> Result<()> {
match self {
// Hidden subcommands
Self::DebugSystemInfo(cmd) => cmd.run(&home).await,
Self::DebugTrustedTools(cmd) => cmd.run(&home).await,
Self::DebugSystemInfo(cmd) => cmd.run(home).await,
Self::DebugTrustedTools(cmd) => cmd.run(home).await,
// Public subcommands
Self::List(cmd) => cmd.run(&home).await,
Self::Trust(cmd) => cmd.run(&home).await,
Self::Untrust(cmd) => cmd.run(&home).await,
};

home.save().await.context(
"Failed to save Aftman data!\
\nChanges to trust, tools, and more may have been lost.",
)?;

result
Self::List(cmd) => cmd.run(home).await,
Self::Trust(cmd) => cmd.run(home).await,
Self::Untrust(cmd) => cmd.run(home).await,
}
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::{error, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;

mod cli;
use cli::Args;
use cli::Cli;

#[tokio::main]
async fn main() {
Expand All @@ -25,7 +25,7 @@ async fn main() {
.without_time()
.init();

if let Err(e) = Args::parse().run().await {
if let Err(e) = Cli::parse().run().await {
// NOTE: We use tracing for errors here for consistent
// output between returned errors, and errors that
// may be logged while the program is running.
Expand Down

0 comments on commit 292c11a

Please sign in to comment.