From eca3b296c32a3af4f6b05a3479e9fece84f216a1 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 11 Jun 2024 12:46:12 +0200 Subject: [PATCH] lib: Move out COB modules --- {src => bin}/cob.rs | 2 + {src => bin}/cob/inbox.rs | 0 {src => bin}/cob/issue.rs | 2 + {src => bin}/cob/patch.rs | 1 + bin/commands/inbox.rs | 4 +- bin/commands/inbox/select.rs | 3 +- bin/commands/issue.rs | 3 +- bin/commands/issue/select.rs | 3 +- bin/commands/patch.rs | 11 ++- bin/commands/patch/select.rs | 3 +- bin/main.rs | 1 + src/context.rs | 184 ----------------------------------- src/lib.rs | 2 - 13 files changed, 23 insertions(+), 196 deletions(-) rename {src => bin}/cob.rs (95%) rename {src => bin}/cob/inbox.rs (100%) rename {src => bin}/cob/issue.rs (98%) rename {src => bin}/cob/patch.rs (99%) delete mode 100644 src/context.rs diff --git a/src/cob.rs b/bin/cob.rs similarity index 95% rename from src/cob.rs rename to bin/cob.rs index 0ce05f56..5d47ea3c 100644 --- a/src/cob.rs +++ b/bin/cob.rs @@ -9,6 +9,7 @@ pub mod inbox; pub mod issue; pub mod patch; +#[allow(dead_code)] pub fn parse_labels(input: String) -> Result> { let mut labels = vec![]; if !input.is_empty() { @@ -23,6 +24,7 @@ pub fn parse_labels(input: String) -> Result> { Ok(labels) } +#[allow(dead_code)] pub fn parse_assignees(input: String) -> Result> { let mut assignees = vec![]; if !input.is_empty() { diff --git a/src/cob/inbox.rs b/bin/cob/inbox.rs similarity index 100% rename from src/cob/inbox.rs rename to bin/cob/inbox.rs diff --git a/src/cob/issue.rs b/bin/cob/issue.rs similarity index 98% rename from src/cob/issue.rs rename to bin/cob/issue.rs index b59b3cf5..0323697f 100644 --- a/src/cob/issue.rs +++ b/bin/cob/issue.rs @@ -80,11 +80,13 @@ pub fn all(profile: &Profile, repository: &Repository) -> Result Result> { let cache = profile.issues(repository)?; Ok(cache.get(id)?) } +#[allow(dead_code)] pub fn create( profile: &Profile, repository: &Repository, diff --git a/src/cob/patch.rs b/bin/cob/patch.rs similarity index 99% rename from src/cob/patch.rs rename to bin/cob/patch.rs index e8b3324d..e7bd1437 100644 --- a/src/cob/patch.rs +++ b/bin/cob/patch.rs @@ -79,6 +79,7 @@ pub fn all(profile: &Profile, repository: &Repository) -> Result Result> { let cache = profile.patches(repository)?; Ok(cache.get(id)?) diff --git a/bin/commands/inbox.rs b/bin/commands/inbox.rs index 5dac8c57..6b6bc359 100644 --- a/bin/commands/inbox.rs +++ b/bin/commands/inbox.rs @@ -9,13 +9,13 @@ use anyhow::anyhow; use radicle_tui as tui; -use tui::cob::inbox::{self}; - use radicle_cli::terminal; use radicle_cli::terminal::{Args, Error, Help}; use self::common::{Mode, RepositoryMode, SelectionMode}; +use crate::cob::inbox; + pub const HELP: Help = Help { name: "inbox", description: "Terminal interfaces for notifications", diff --git a/bin/commands/inbox/select.rs b/bin/commands/inbox/select.rs index 3c614477..3bec7cc0 100644 --- a/bin/commands/inbox/select.rs +++ b/bin/commands/inbox/select.rs @@ -22,7 +22,6 @@ use radicle::Profile; use radicle_tui as tui; -use tui::cob::inbox; use tui::store; use tui::store::StateValue; use tui::ui::items::{Filter, NotificationItem, NotificationItemFilter}; @@ -33,6 +32,8 @@ use tui::ui::widget::window::{Page, PageProps, Shortcuts, ShortcutsProps, Window use tui::ui::widget::{ToWidget, Widget}; use tui::{BoxedAny, Channel, Exit, PageStack}; +use crate::cob::inbox; + use self::ui::Browser; use self::ui::BrowserProps; diff --git a/bin/commands/issue.rs b/bin/commands/issue.rs index 14374f3f..438e439e 100644 --- a/bin/commands/issue.rs +++ b/bin/commands/issue.rs @@ -15,9 +15,10 @@ use radicle_cli::terminal::{Args, Error, Help}; use radicle_tui as tui; -use tui::cob; use tui::log; +use crate::cob; + pub const HELP: Help = Help { name: "issue", description: "Terminal interfaces for issues", diff --git a/bin/commands/issue/select.rs b/bin/commands/issue/select.rs index c45ce553..fafa282f 100644 --- a/bin/commands/issue/select.rs +++ b/bin/commands/issue/select.rs @@ -17,7 +17,6 @@ use radicle::Profile; use radicle_tui as tui; -use tui::cob::issue; use tui::store; use tui::store::StateValue; use tui::ui::items::{Filter, IssueItem, IssueItemFilter}; @@ -29,6 +28,8 @@ use tui::ui::widget::{ToWidget, Widget}; use tui::{BoxedAny, Channel, Exit, PageStack}; +use crate::cob::issue; + use self::ui::{Browser, BrowserProps}; use super::common::Mode; diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 76ab609e..a446dbdc 100644 --- a/bin/commands/patch.rs +++ b/bin/commands/patch.rs @@ -9,14 +9,17 @@ use anyhow::anyhow; use radicle::identity::RepoId; use radicle::patch::Status; -use radicle_tui as tui; - -use tui::cob::patch::{self, Filter}; -use tui::log; use radicle_cli::terminal; use radicle_cli::terminal::args::{Args, Error, Help}; +use radicle_tui as tui; + +use tui::log; + +use crate::cob::patch; +use crate::cob::patch::Filter; + pub const HELP: Help = Help { name: "patch", description: "Terminal interfaces for patches", diff --git a/bin/commands/patch/select.rs b/bin/commands/patch/select.rs index 49c92856..7f0138fa 100644 --- a/bin/commands/patch/select.rs +++ b/bin/commands/patch/select.rs @@ -16,7 +16,6 @@ use ratatui::style::Stylize; use ratatui::text::{Line, Span, Text}; use termion::event::Key; -use tui::cob::patch; use tui::store; use tui::ui::items::{Filter, PatchItem, PatchItemFilter}; use tui::ui::span; @@ -31,6 +30,8 @@ use self::ui::{Browser, BrowserProps}; use super::common::Mode; +use crate::cob::patch; + type Selection = tui::Selection; pub struct Context { diff --git a/bin/main.rs b/bin/main.rs index 8073784d..c32d2d36 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -1,3 +1,4 @@ +mod cob; mod commands; use std::ffi::OsString; diff --git a/src/context.rs b/src/context.rs deleted file mode 100644 index e0943fba..00000000 --- a/src/context.rs +++ /dev/null @@ -1,184 +0,0 @@ -use std::fmt::Display; - -use radicle::cob::issue::{Issue, IssueId}; -use radicle::cob::patch::{Patch, PatchId}; -use radicle::crypto::ssh::keystore::MemorySigner; -use radicle::crypto::Signer; -use radicle::identity::{Project, RepoId}; -use radicle::node::notifications::Notification; -use radicle::profile::env::RAD_PASSPHRASE; -use radicle::storage::git::Repository; -use radicle::storage::{ReadRepository, ReadStorage}; -use radicle::Profile; - -use radicle_cli::terminal::io::PassphraseValidator; -use radicle_term as term; -use term::{passphrase, spinner}; - -use super::cob::inbox; - -/// Git revision parameter. Supports extended SHA-1 syntax. -#[derive(Debug, Clone, PartialEq, Eq)] - -pub struct Rev(String); - -impl From for Rev { - fn from(value: String) -> Self { - Rev(value) - } -} - -impl Display for Rev { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -/// Application context that holds all the project data that are -/// needed to render it. -pub struct Context { - profile: Profile, - rid: RepoId, - project: Project, - repository: Repository, - issues: Option>, - patches: Option>, - notifications: Vec, - signer: Option>, -} - -impl Context { - pub fn new(profile: Profile, rid: RepoId) -> Result { - let repository = profile.storage.repository(rid).unwrap(); - let project = repository.identity_doc()?.project()?; - let notifications = inbox::all(&repository, &profile)?; - - let issues = None; - let patches = None; - let signer = None; - - Ok(Self { - profile, - rid, - project, - repository, - issues, - patches, - notifications, - signer, - }) - } - - pub fn with_issues(mut self) -> Self { - use super::cob::issue; - self.issues = Some(issue::all(&self.profile, &self.repository).unwrap_or_default()); - self - } - - pub fn with_patches(mut self) -> Self { - use super::cob::patch; - self.patches = Some(patch::all(&self.profile, &self.repository).unwrap_or_default()); - self - } - - pub fn with_signer(mut self) -> Self { - self.signer = signer(&self.profile).ok(); - self - } - - pub fn profile(&self) -> &Profile { - &self.profile - } - - pub fn rid(&self) -> &RepoId { - &self.rid - } - - pub fn project(&self) -> &Project { - &self.project - } - - pub fn repository(&self) -> &Repository { - &self.repository - } - - pub fn issues(&self) -> &Option> { - &self.issues - } - - pub fn patches(&self) -> &Option> { - &self.patches - } - - pub fn notifications(&self) -> &Vec { - &self.notifications - } - - #[allow(clippy::borrowed_box)] - pub fn signer(&self) -> &Option> { - &self.signer - } - - pub fn reload_patches(&mut self) { - use super::cob::patch; - self.patches = Some(patch::all(&self.profile, &self.repository).unwrap_or_default()); - } - - pub fn reload_issues(&mut self) { - use super::cob::issue; - self.issues = Some(issue::all(&self.profile, &self.repository).unwrap_or_default()); - } -} - -// /// Validates secret key passphrases. -// #[derive(Clone)] -// pub struct PassphraseValidator { -// keystore: Keystore, -// } - -// impl PassphraseValidator { -// /// Create a new validator. -// pub fn new(keystore: Keystore) -> Self { -// Self { keystore } -// } -// } - -// impl inquire::validator::StringValidator for PassphraseValidator { -// fn validate( -// &self, -// input: &str, -// ) -> Result { -// let passphrase = Passphrase::from(input.to_owned()); -// if self.keystore.is_valid_passphrase(&passphrase)? { -// Ok(validator::Validation::Valid) -// } else { -// Ok(validator::Validation::Invalid( -// validator::ErrorMessage::from("Invalid passphrase, please try again"), -// )) -// } -// } -// } - -/// Get the signer. First we try getting it from ssh-agent, otherwise we prompt the user, -/// if we're connected to a TTY. -pub fn signer(profile: &Profile) -> anyhow::Result> { - if let Ok(signer) = profile.signer() { - return Ok(signer); - } - let validator = PassphraseValidator::new(profile.keystore.clone()); - let passphrase = match passphrase(validator) { - Ok(p) => p, - Err(inquire::InquireError::NotTTY) => { - return Err(anyhow::anyhow!( - "running in non-interactive mode, please set `{RAD_PASSPHRASE}` to unseal your key", - )); - } - Err(e) => return Err(e.into()), - }; - let spinner = spinner("Unsealing key..."); - let signer = MemorySigner::load(&profile.keystore, Some(passphrase))?; - - spinner.finish(); - - Ok(signer.boxed()) -} diff --git a/src/lib.rs b/src/lib.rs index a6f0cc94..85ea5360 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -pub mod cob; -pub mod context; pub mod event; pub mod git; pub mod log;