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
39 changes: 39 additions & 0 deletions linkup-cli/src/commands/sessions/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use colored::Colorize;

use crate::{
Result,
services::local_server,
session::{list_session_rows, print_sessions_table},
};

#[derive(clap::Args)]
pub struct Args {
#[arg(long)]
pub json: bool,
}

pub async fn run(args: &Args) -> Result<()> {
if !local_server::is_reachable().await {
println!(
"{}",
"Seems like your local Linkup server is not running. Please run 'linkup start' first."
.yellow()
);

return Ok(());
}

let sessions_rows = list_session_rows().await;

if args.json {
println!(
"{}",
serde_json::to_string_pretty(&sessions_rows)
.expect("Failed to serialize sessions rows")
);
} else {
print_sessions_table(&sessions_rows, None);
}

Ok(())
}
5 changes: 5 additions & 0 deletions linkup-cli/src/commands/sessions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod create_isolated;
mod create_preview;
mod delete;
mod list;

use clap::Subcommand;

Expand All @@ -22,12 +23,16 @@ enum Command {

#[clap(about = "Delete session")]
Delete(delete::Args),

#[clap(about = "List sessions", aliases = ["ls"])]
List(list::Args),
}

pub async fn sessions(args: &Args, config: &Option<String>) -> Result<()> {
match &args.command {
Command::CreatePreview(args) => create_preview::run(args, config).await,
Command::CreateIsolated(args) => create_isolated::run(args, config).await,
Command::Delete(args) => delete::run(args).await,
Command::List(args) => list::run(args).await,
}
}
30 changes: 2 additions & 28 deletions linkup-cli/src/commands/status/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use url::Url;
use crate::{
commands,
services::{self, local_server},
session::{SessionRow, format_state_domains, print_sessions_table},
session::{SessionRow, list_session_rows, print_sessions_table},
state::{State, get_config},
};

Expand Down Expand Up @@ -55,7 +55,7 @@ pub async fn status(args: &Args) -> anyhow::Result<()> {
.unwrap_or(&state.linkup.session_name)
.to_string();

let all_sessions = fetch_sessions().await;
let all_sessions = list_session_rows().await;

if args.session.is_some()
&& !all_sessions
Expand Down Expand Up @@ -199,32 +199,6 @@ struct Output {
services: Vec<ServiceStatus>,
}

async fn fetch_sessions() -> Vec<SessionRow> {
let client = LocalServerClient::new(&services::local_server::url());

match client.list_sessions().await {
Ok(response) => {
let mut entries: Vec<SessionRow> = response
.sessions
.into_iter()
.map(|(name, session)| {
let domains = format_state_domains(&name, &session.domains);

SessionRow {
name,
kind: session.kind,
domains,
}
})
.collect();

entries.sort_by(|a, b| a.kind.cmp(&b.kind).then(a.name.cmp(&b.name)));
entries
}
Err(_) => vec![],
}
}

async fn fetch_session_detail(session_name: &str) -> Option<SessionDetailResponse> {
let client = LocalServerClient::new(&services::local_server::url());
client.get_session(session_name).await.ok()
Expand Down
29 changes: 28 additions & 1 deletion linkup-cli/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use colored::Colorize;
use linkup::{Domain, SessionKind};
use linkup_clients::LocalServerClient;
use serde::{Deserialize, Serialize};

use crate::state::State;
use crate::{services::local_server, state::State};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SessionRow {
Expand All @@ -21,6 +22,32 @@ impl SessionRow {
}
}

pub async fn list_session_rows() -> Vec<SessionRow> {
let client = LocalServerClient::new(&local_server::url());

match client.list_sessions().await {
Ok(response) => {
let mut entries: Vec<SessionRow> = response
.sessions
.into_iter()
.map(|(name, session)| {
let domains = format_state_domains(&name, &session.domains);

SessionRow {
name,
kind: session.kind,
domains,
}
})
.collect();

entries.sort_by(|a, b| a.kind.cmp(&b.kind).then(a.name.cmp(&b.name)));
entries
}
Err(_) => vec![],
}
}

pub fn print_sessions_table(sessions: &[SessionRow], current: Option<&str>) {
if sessions.is_empty() {
return;
Expand Down
Loading