diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-06 20:43:40 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-09 11:13:00 +0100 |
commit | 03b7feebdb56cd014d881faf29de64f681a038a6 (patch) | |
tree | 1937fa75e5458127a3e915ccf63b58aecd3bfb51 | |
parent | 7ed34f04b41f8c575f4ab5a178495a7132c39947 (diff) |
Add CLI for posting text to profile
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | cli/Cargo.toml | 1 | ||||
-rw-r--r-- | cli/src/cli.rs | 38 | ||||
-rw-r--r-- | cli/src/profile.rs | 28 |
3 files changed, 67 insertions, 0 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 749438d..215efe1 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -37,6 +37,7 @@ getset = "0.1" xdg = "2.4" tracing = "0.1" ctrlc = "3.2" +editor-input = "0.1.2" [dependencies.ipfs] git = "https://github.com/rs-ipfs/rust-ipfs/" diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 3f74128..058b649 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -2,6 +2,7 @@ use clap::crate_authors; use clap::crate_version; use clap::App; use clap::Arg; +use clap::ArgGroup; pub fn app<'a>() -> App<'a> { App::new("distrox") @@ -49,6 +50,43 @@ pub fn app<'a>() -> App<'a> { .about("Connect to MULTIADDR as well") ) ) + + .subcommand(App::new("post") + .author(crate_authors!()) + .version(crate_version!()) + .about("Just serve the profile") + + .arg(Arg::new("name") + .long("name") + .required(true) + .takes_value(true) + .value_name("NAME") + .about("Name of the profile to post to") + ) + + .arg(Arg::new("editor") + .long("editor") + .short('e') + .required(false) + .takes_value(false) + .about("Launch the editor for the text to be posted") + .conflicts_with("text") + ) + .arg(Arg::new("text") + .long("text") + .required(true) + .takes_value(true) + .value_name("TEXT") + .about("The text to be posted") + .conflicts_with("editor") + ) + .group(ArgGroup::new("text-or-editor") + .args(&["text", "editor"]) + .required(true) // one must be present + ) + + + ) ) .subcommand(App::new("gui") diff --git a/cli/src/profile.rs b/cli/src/profile.rs index 0eb8b75..4b3fbd6 100644 --- a/cli/src/profile.rs +++ b/cli/src/profile.rs @@ -13,6 +13,7 @@ pub async fn profile(matches: &ArgMatches) -> Result<()> { match matches.subcommand() { Some(("create", m)) => profile_create(m).await, Some(("serve", m)) => profile_serve(m).await, + Some(("post", m)) => profile_post(m).await, _ => unimplemented!(), } } @@ -65,3 +66,30 @@ async fn profile_serve(matches: &ArgMatches) -> Result<()> { log::info!("Shutting down..."); profile.exit().await } + +async fn profile_post(matches: &ArgMatches) -> Result<()> { + let text = match matches.value_of("text") { + Some(text) => String::from(text), + None => if matches.is_present("editor") { + editor_input::input_from_editor("")? + } else { + unreachable!() + } + }; + 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()); + + log::info!("Loading '{}' from {}", name, state_dir.display()); + let mut profile = Profile::load(Config::default(), &name).await?; + log::info!("Profile loaded"); + log::info!("Profile HEAD = {:?}", profile.head()); + + log::info!("Posting text..."); + profile.post_text(text).await?; + log::info!("Posting text finished"); + profile.save().await?; + log::info!("Saving profile state to disk finished"); + profile.exit().await +} + |