diff --git a/src/cli/cmd.rs b/src/cli/cmd.rs index 94ba800..8a4468e 100644 --- a/src/cli/cmd.rs +++ b/src/cli/cmd.rs @@ -11,6 +11,7 @@ pub mod version; use super::sink::{Error as SinkError, Sink}; use crate::cli::opts::CommonOpts; use crate::data::renku_url::RenkuUrl; +use crate::httpclient::data::ProjectDetails; use crate::httpclient::{self, Client}; use serde::Serialize; use snafu::{ResultExt, Snafu}; @@ -44,6 +45,18 @@ impl Context { let fmt = self.opts.format; Sink::write_err(&fmt, value) } + + pub async fn resolve_project_context( + &self, + ) -> Result, httpclient::Error> { + match self.opts.get_project_context() { + Ok(Some(id)) => self.client.get_project(&id).await, + Ok(None) => Ok(None), + Err(err) => Err(httpclient::Error::ProjectUrlParse { + reason: format!("{}", err), + }), + } + } } #[derive(Debug, Snafu)] diff --git a/src/cli/cmd/job/list.rs b/src/cli/cmd/job/list.rs index c3a8cb8..dd1a500 100644 --- a/src/cli/cmd/job/list.rs +++ b/src/cli/cmd/job/list.rs @@ -25,12 +25,16 @@ pub enum Error { impl Input { pub async fn exec(&self, ctx: Context) -> Result<(), Error> { - let result = ctx + let mut result = ctx .client .list_sessions(Some(SessionMode::NonInteractive)) .await .context(HttpClientSnafu)?; + if let Ok(Some(project)) = ctx.resolve_project_context().await { + result.retain(|v| v.project_id == project.id); + } + ctx.write_result(&result).await.context(WriteResultSnafu) } } diff --git a/src/httpclient/data.rs b/src/httpclient/data.rs index 5fe18e8..4ead0df 100644 --- a/src/httpclient/data.rs +++ b/src/httpclient/data.rs @@ -84,6 +84,22 @@ impl fmt::Display for SessionStartRequest { #[derive(Debug, Serialize, Deserialize)] pub struct SessionList(pub Vec); +impl SessionList { + pub fn filter(self, f: F) -> SessionList + where + F: Fn(&SessionStartResponse) -> bool, + { + let l: Vec = self.0.into_iter().filter(|v| f(v)).collect(); + SessionList(l) + } + + pub fn retain(&mut self, f: F) + where + F: Fn(&SessionStartResponse) -> bool, + { + self.0.retain(|v| f(v)); + } +} fn create_session_table<'a, I>(data: I) -> Table where @@ -93,8 +109,8 @@ where for r in data { let sub_id = r.submission_id.as_deref().unwrap_or("-"); let started = r.started.format(); - let status = r.status.state.to_string(); - let data = vec![&r.name, sub_id, &r.project_id, &status, &started]; + let status = r.status.state.to_str(); + let data = vec![&r.name, sub_id, &r.project_id, status, &started]; builder.push_record(data); } builder.insert_record( @@ -132,13 +148,23 @@ pub enum SessionState { } impl SessionState { + pub fn is_running(&self) -> bool { + match self { + SessionState::Running => true, + SessionState::Starting => true, + SessionState::Stopping => true, + SessionState::Failed => false, + SessionState::Hibernated => false, + SessionState::Succeeded => false, + } + } pub fn to_str(&self) -> &'static str { match self { + SessionState::Failed => "Failed", + SessionState::Hibernated => "Hibernated", SessionState::Running => "Running", SessionState::Starting => "Starting", SessionState::Stopping => "Stopping", - SessionState::Failed => "Failed", - SessionState::Hibernated => "Hibernated", SessionState::Succeeded => "Succeeded", } } @@ -146,21 +172,7 @@ impl SessionState { impl fmt::Display for SessionState { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let name = self.to_str(); - f.write_str(name) - } -} - -impl SessionState { - pub fn is_running(&self) -> bool { - match self { - SessionState::Running => true, - SessionState::Starting => true, - SessionState::Stopping => true, - SessionState::Failed => false, - SessionState::Hibernated => false, - SessionState::Succeeded => false, - } + f.write_str(self.to_str()) } }