From 13a59045f8b692575ea7625d9d3f13895830ac5a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 5 Dec 2021 13:17:11 +0100 Subject: Add CLI for creating profile Signed-off-by: Matthias Beyer --- src/cli.rs | 28 ++++++++++++++++++++++++++++ src/commands/mod.rs | 2 ++ src/commands/profile.rs | 22 ++++++++++++++++++++++ src/config.rs | 1 - src/main.rs | 11 +++++++++-- src/profile.rs | 2 +- 6 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/commands/mod.rs create mode 100644 src/commands/profile.rs diff --git a/src/cli.rs b/src/cli.rs index 4e01cbb..640c57c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,10 +1,38 @@ use clap::crate_authors; use clap::crate_version; use clap::App; +use clap::Arg; pub fn app<'a>() -> App<'a> { App::new("distrox") .author(crate_authors!()) .version(crate_version!()) .about("Distributed social network") + + + .subcommand(App::new("profile") + .author(crate_authors!()) + .version(crate_version!()) + .about("Profile actions") + + .subcommand(App::new("create") + .author(crate_authors!()) + .version(crate_version!()) + .about("Create profile") + + .arg(Arg::with_name("name") + .long("name") + .required(true) + .takes_value(true) + .value_name("NAME") + .about("Name of the profile") + ) + ) + + .subcommand(App::new("serve") + .author(crate_authors!()) + .version(crate_version!()) + .about("Just serve the profile") + ) + ) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..5569bcb --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,2 @@ +mod profile; +pub use profile::profile; diff --git a/src/commands/profile.rs b/src/commands/profile.rs new file mode 100644 index 0000000..98fbc08 --- /dev/null +++ b/src/commands/profile.rs @@ -0,0 +1,22 @@ +use anyhow::Result; +use clap::ArgMatches; + +use crate::config::Config; +use crate::profile::Profile; + +pub async fn profile(matches: &ArgMatches) -> Result<()> { + match matches.subcommand() { + Some(("create", m)) => profile_create(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 +} diff --git a/src/config.rs b/src/config.rs index d2f0aba..0d25e78 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,6 @@ pub struct Config { } -#[cfg(test)] impl Default for Config { fn default() -> Self { Config { } diff --git a/src/main.rs b/src/main.rs index ef413c6..257847c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use anyhow::Result; pub mod cli; pub mod client; +mod commands; pub mod config; pub mod consts; pub mod ipfs_client; @@ -11,7 +12,13 @@ pub mod types; #[tokio::main] async fn main() -> Result<()> { let _ = env_logger::try_init()?; - let _ = crate::cli::app(); - Ok(()) + let matches = crate::cli::app().get_matches(); + + match matches.subcommand() { + Some(("profile", matches)) => crate::commands::profile(matches).await, + _ => unimplemented!() + } + + } diff --git a/src/profile.rs b/src/profile.rs index 1b5f81c..abbb92d 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -86,7 +86,7 @@ impl Profile { }) } - fn state_dir_path(name: &str) -> Result { + pub fn state_dir_path(name: &str) -> Result { xdg::BaseDirectories::with_prefix("distrox") .map_err(anyhow::Error::from) .and_then(|dirs| { -- cgit v1.2.3