diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-05 17:44:43 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-06 16:29:01 +0100 |
commit | 52a81581816da9533d93211b9b0e63ff221926d2 (patch) | |
tree | 9fc01bb0f16eceeef2b9472c2925bd9268cbc855 /src | |
parent | 080a97a314926ae00a4153aab21a21766c58f521 (diff) |
Implement profile serving
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 8 | ||||
-rw-r--r-- | src/commands/profile.rs | 29 |
2 files changed, 37 insertions, 0 deletions
@@ -33,6 +33,14 @@ pub fn app<'a>() -> App<'a> { .author(crate_authors!()) .version(crate_version!()) .about("Just serve the profile") + + .arg(Arg::with_name("name") + .long("name") + .required(true) + .takes_value(true) + .value_name("NAME") + .about("Name of the profile") + ) ) ) } diff --git a/src/commands/profile.rs b/src/commands/profile.rs index 951d4b0..93753d7 100644 --- a/src/commands/profile.rs +++ b/src/commands/profile.rs @@ -1,3 +1,8 @@ +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::sync::Arc; + +use anyhow::Context; use anyhow::Result; use clap::ArgMatches; @@ -7,6 +12,7 @@ use crate::profile::Profile; pub async fn profile(matches: &ArgMatches) -> Result<()> { match matches.subcommand() { Some(("create", m)) => profile_create(m).await, + Some(("serve", m)) => profile_serve(m).await, _ => unimplemented!(), } } @@ -23,3 +29,26 @@ async fn profile_create(matches: &ArgMatches) -> Result<()> { log::info!("Shutting down..."); profile.exit().await } + +async fn profile_serve(matches: &ArgMatches) -> Result<()> { + let name = matches.value_of("name").map(String::from).unwrap(); // required + let state_dir = Profile::state_dir_path(&name)?; + + log::info!("Loading '{}' from {}", name, state_dir.display()); + let profile = Profile::load(Config::default(), &name).await?; + log::info!("Profile loaded"); + + let running = Arc::new(AtomicBool::new(true)); + let r = running.clone(); + + ctrlc::set_handler(move || { + r.store(false, Ordering::SeqCst); + }).context("Error setting Ctrl-C handler")?; + + log::info!("Serving..."); + while running.load(Ordering::SeqCst) { + tokio::time::sleep(std::time::Duration::from_millis(500)).await // sleep not so busy + } + log::info!("Shutting down..."); + profile.exit().await +} |