diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-05 13:17:11 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-06 16:29:01 +0100 |
commit | 13a59045f8b692575ea7625d9d3f13895830ac5a (patch) | |
tree | 7a811a5cc8a045c1519b73d61c7dddc1368bd5a8 /src | |
parent | 397805100d2fc48937652b4c54a8870501966301 (diff) |
Add CLI for creating profile
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 28 | ||||
-rw-r--r-- | src/commands/mod.rs | 2 | ||||
-rw-r--r-- | src/commands/profile.rs | 22 | ||||
-rw-r--r-- | src/config.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/profile.rs | 2 |
6 files changed, 62 insertions, 4 deletions
@@ -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<PathBuf> { + pub fn state_dir_path(name: &str) -> Result<PathBuf> { xdg::BaseDirectories::with_prefix("distrox") .map_err(anyhow::Error::from) .and_then(|dirs| { |