From 1f0b94c5cc023fb1b259b92fde572ec173571f0b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 8 Dec 2021 18:40:17 +0100 Subject: Restructure module layout Signed-off-by: Matthias Beyer --- cli/src/commands/mod.rs | 2 -- cli/src/commands/profile.rs | 67 --------------------------------------------- cli/src/main.rs | 4 +-- cli/src/profile.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 71 deletions(-) delete mode 100644 cli/src/commands/mod.rs delete mode 100644 cli/src/commands/profile.rs create mode 100644 cli/src/profile.rs diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs deleted file mode 100644 index 5569bcb..0000000 --- a/cli/src/commands/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod profile; -pub use profile::profile; diff --git a/cli/src/commands/profile.rs b/cli/src/commands/profile.rs deleted file mode 100644 index 0eb8b75..0000000 --- a/cli/src/commands/profile.rs +++ /dev/null @@ -1,67 +0,0 @@ -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering; -use std::sync::Arc; - -use anyhow::Context; -use anyhow::Result; -use clap::ArgMatches; - -use distrox_lib::config::Config; -use distrox_lib::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!(), - } -} - -async fn profile_create(matches: &ArgMatches) -> Result<()> { - let name = matches.value_of("name").map(String::from).unwrap(); // required - let state_dir = Profile::state_dir_path(&name)?; - log::info!("Creating '{}' in {}", name, state_dir.display()); - - let profile = Profile::create(&state_dir, &name, Config::default()).await?; - log::info!("Saving..."); - profile.save().await?; - - log::info!("Shutting down..."); - profile.exit().await -} - -async fn profile_serve(matches: &ArgMatches) -> Result<()> { - use ipfs::MultiaddrWithPeerId; - - let name = matches.value_of("name").map(String::from).unwrap(); // required - let connect_peer = matches.value_of("connect").map(|s| { - s.parse::() - .map_err(anyhow::Error::from) - }).transpose()?; - - 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"); - log::info!("Profile HEAD = {:?}", profile.head()); - - if let Some(connect_to) = connect_peer { - log::info!("Connecting to {:?}", connect_to); - profile.connect(connect_to).await?; - } - - 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 -} diff --git a/cli/src/main.rs b/cli/src/main.rs index 99ac4d1..c4050ac 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; mod cli; -mod commands; +mod profile; #[tokio::main] async fn main() -> Result<()> { @@ -9,7 +9,7 @@ async fn main() -> Result<()> { let matches = crate::cli::app().get_matches(); match matches.subcommand() { - Some(("profile", matches)) => crate::commands::profile(matches).await, + Some(("profile", matches)) => crate::profile::profile(matches).await, Some(("gui", _)) => { unimplemented!() }, diff --git a/cli/src/profile.rs b/cli/src/profile.rs new file mode 100644 index 0000000..0eb8b75 --- /dev/null +++ b/cli/src/profile.rs @@ -0,0 +1,67 @@ +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::sync::Arc; + +use anyhow::Context; +use anyhow::Result; +use clap::ArgMatches; + +use distrox_lib::config::Config; +use distrox_lib::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!(), + } +} + +async fn profile_create(matches: &ArgMatches) -> Result<()> { + let name = matches.value_of("name").map(String::from).unwrap(); // required + let state_dir = Profile::state_dir_path(&name)?; + log::info!("Creating '{}' in {}", name, state_dir.display()); + + let profile = Profile::create(&state_dir, &name, Config::default()).await?; + log::info!("Saving..."); + profile.save().await?; + + log::info!("Shutting down..."); + profile.exit().await +} + +async fn profile_serve(matches: &ArgMatches) -> Result<()> { + use ipfs::MultiaddrWithPeerId; + + let name = matches.value_of("name").map(String::from).unwrap(); // required + let connect_peer = matches.value_of("connect").map(|s| { + s.parse::() + .map_err(anyhow::Error::from) + }).transpose()?; + + 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"); + log::info!("Profile HEAD = {:?}", profile.head()); + + if let Some(connect_to) = connect_peer { + log::info!("Connecting to {:?}", connect_to); + profile.connect(connect_to).await?; + } + + 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 +} -- cgit v1.2.3