libshpool/
list.rs

1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{io, path::PathBuf, time};
16
17use anyhow::Context;
18use shpool_protocol::{ConnectHeader, ListReply};
19
20use crate::{protocol, protocol::ClientResult};
21
22pub fn run(socket: PathBuf) -> anyhow::Result<()> {
23    let mut client = match protocol::Client::new(socket) {
24        Ok(ClientResult::JustClient(c)) => c,
25        Ok(ClientResult::VersionMismatch { warning, client }) => {
26            eprintln!("warning: {warning}, try restarting your daemon");
27            client
28        }
29        Err(err) => {
30            let io_err = err.downcast::<io::Error>()?;
31            if io_err.kind() == io::ErrorKind::NotFound {
32                eprintln!("could not connect to daemon");
33            }
34            return Err(io_err).context("connecting to daemon");
35        }
36    };
37
38    client.write_connect_header(ConnectHeader::List).context("sending list connect header")?;
39    let reply: ListReply = client.read_reply().context("reading reply")?;
40
41    println!("NAME\tSTARTED_AT\tSTATUS");
42    for session in reply.sessions.iter() {
43        let started_at =
44            time::UNIX_EPOCH + time::Duration::from_millis(session.started_at_unix_ms as u64);
45        let started_at = chrono::DateTime::<chrono::Utc>::from(started_at);
46        println!("{}\t{}\t{}", session.name, started_at.to_rfc3339(), session.status);
47    }
48
49    Ok(())
50}