diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-09 11:37:57 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-09 11:37:57 +0100 |
commit | 2851ca7aa80da00e8c2178a5c60c97b006b36f7f (patch) | |
tree | 07c9a8d82f741e72b389fa6def21f1b3d5d4b303 | |
parent | d2db69cfc4e239a73aa24543b781031b5ff63611 (diff) | |
parent | 03b7feebdb56cd014d881faf29de64f681a038a6 (diff) |
Merge branch 'profile-post-text'
-rw-r--r-- | cli/Cargo.toml | 1 | ||||
-rw-r--r-- | cli/src/cli.rs | 38 | ||||
-rw-r--r-- | cli/src/profile.rs | 28 | ||||
-rw-r--r-- | lib/src/profile/mod.rs | 13 | ||||
-rw-r--r-- | lib/src/profile/state.rs | 5 |
5 files changed, 85 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 412f143..ca59c2b 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!(), } } @@ -67,3 +68,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 +} + diff --git a/lib/src/profile/mod.rs b/lib/src/profile/mod.rs index db3c593..050d53d 100644 --- a/lib/src/profile/mod.rs +++ b/lib/src/profile/mod.rs @@ -66,6 +66,19 @@ impl Profile { self.client.connect(peer).await } + pub async fn post_text(&mut self, text: String) -> Result<cid::Cid> { + let parent = self.state + .profile_head() + .as_ref() + .map(cid::Cid::clone) + .into_iter() + .collect::<Vec<cid::Cid>>(); + + let new_cid = self.client.post_text_node(parent, text).await?; + self.state.update_head(new_cid.clone())?; + Ok(new_cid) + } + async fn ipfs_path(state_dir: &StateDir) -> Result<PathBuf> { let path = state_dir.ipfs(); tokio::fs::create_dir_all(&path).await?; diff --git a/lib/src/profile/state.rs b/lib/src/profile/state.rs index 4075b52..33f0bd6 100644 --- a/lib/src/profile/state.rs +++ b/lib/src/profile/state.rs @@ -49,6 +49,11 @@ impl ProfileState { keypair } } + + pub(super) fn update_head(&mut self, cid: cid::Cid) -> Result<()> { + self.profile_head = Some(cid); + Ok(()) // reserved for future use + } } impl std::fmt::Debug for ProfileState { |