Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -44,6 +45,18 @@ impl Context {
let fmt = self.opts.format;
Sink::write_err(&fmt, value)
}

pub async fn resolve_project_context(
&self,
) -> Result<Option<ProjectDetails>, 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)]
Expand Down
6 changes: 5 additions & 1 deletion src/cli/cmd/job/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
50 changes: 31 additions & 19 deletions src/httpclient/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ impl fmt::Display for SessionStartRequest {

#[derive(Debug, Serialize, Deserialize)]
pub struct SessionList(pub Vec<SessionStartResponse>);
impl SessionList {
pub fn filter<F>(self, f: F) -> SessionList
where
F: Fn(&SessionStartResponse) -> bool,
{
let l: Vec<SessionStartResponse> = self.0.into_iter().filter(|v| f(v)).collect();
SessionList(l)
}

pub fn retain<F>(&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
Expand All @@ -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(
Expand Down Expand Up @@ -132,35 +148,31 @@ 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",
}
}
}

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())
}
}

Expand Down
Loading