Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auto completion shell support for poststation-cli #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions tools/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/poststation-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
anyhow = "1.0.89"
clap = { version = "4.5.19", features = ["derive"] }
clap_complete = "4.5.44"
directories = "5.0.1"
rand = "0.8.5"
serde_json = "1.0.128"
Expand Down
28 changes: 13 additions & 15 deletions tools/poststation-cli/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ pub enum DeviceCommands {
/// View all topics handled by a given device
TopicsIn,
/// View the most recent logs from a given device
Logs { count: Option<u32> },
Logs {
#[arg(default_value = "8")]
count: u32,
},
/// View the selected range of logs from a given device
LogsRange {
/// Number of logs to print
count: Option<u32>,
/// The UUID of the log to start from
start: String,
/// Direction to print from ('before' or 'after')
direction: String,
/// Number of logs to print
#[arg(default_value = "8")]
count: u32,
},
/// Proxy message to device endpoint
Proxy {
Expand Down Expand Up @@ -153,20 +157,15 @@ async fn device_types(serial: u64, schema: &SchemaReport) -> anyhow::Result<()>
Ok(())
}

async fn device_logs(
client: PoststationClient,
serial: u64,
count: &Option<u32>,
) -> anyhow::Result<()> {
let count = count.unwrap_or(8);
async fn device_logs(client: PoststationClient, serial: u64, count: &u32) -> anyhow::Result<()> {
let logs = client
.get_device_logs(serial, count)
.get_device_logs(serial, *count)
.await
.expect("expected to be able to get logs for device")
.expect("expected device to have known logs");

println!();
println!("Logs (last {} messages):", count.min(logs.len() as u32));
println!("Logs (last {} messages):", count.min(&(logs.len() as u32)));
println!();
for log in logs {
// println!("* {} => {}", log.uuidv7.id_to_time().time(), log.msg);
Expand All @@ -185,11 +184,10 @@ async fn device_logs(
async fn device_logs_range(
client: PoststationClient,
serial: u64,
count: &Option<u32>,
count: &u32,
start: &str,
direction: &str,
) -> anyhow::Result<()> {
let count = count.unwrap_or(8);
let start = start.parse::<Uuid>()?;
let dir = match direction.to_lowercase().as_str() {
"after" => Direction::After,
Expand All @@ -200,7 +198,7 @@ async fn device_logs_range(
let logs = client
.get_device_logs_range(
serial,
count,
*count,
dir,
poststation_api_icd::postsock::Anchor::Uuid(start.into()),
)
Expand All @@ -209,7 +207,7 @@ async fn device_logs_range(
.expect("expected device to have known logs");

println!();
println!("Logs (last {} messages):", count.min(logs.len() as u32));
println!("Logs (last {} messages):", count.min(&(logs.len() as u32)));
println!();
for log in logs {
// println!("* {} => {}", log.uuidv7.id_to_time().time(), log.msg);
Expand Down
31 changes: 28 additions & 3 deletions tools/poststation-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::{net::SocketAddr, path::{Path, PathBuf}, time::Instant};
use std::{
net::SocketAddr,
path::{Path, PathBuf},
time::Instant,
};

use anyhow::bail;
use clap::{command, Parser, Subcommand};
use clap::{arg, command, CommandFactory, Parser, Subcommand};
use device::{device_cmds, Device};
use directories::ProjectDirs;
use postcard_rpc::host_client::{EndpointReport, TopicReport};
use poststation_sdk::{
connect, connect_insecure, connect_with_ca_pem, schema::schema::owned::OwnedDataModelType, ConnectError, PoststationClient
connect, connect_insecure, connect_with_ca_pem, schema::schema::owned::OwnedDataModelType,
ConnectError, PoststationClient,
};

mod device;
Expand Down Expand Up @@ -46,6 +51,12 @@ enum Commands {

/// Interact with a specific device
Device(Device),

/// Generate auto-completion for given shell
Completion {
#[arg(value_enum)]
shell: clap_complete::Shell,
},
}

#[tokio::main]
Expand All @@ -61,6 +72,19 @@ async fn main() -> anyhow::Result<()> {
}

async fn inner_main(cli: Cli) -> anyhow::Result<()> {
// Handle completion command first, before any server connection
if let Commands::Completion { shell } = cli.command {
println!("Start generating completion script for {shell} shell");
clap_complete::generate(
shell,
&mut Cli::command(),
env!("CARGO_PKG_NAME"),
&mut std::io::stdout().lock(),
);
println!("Successfully generated completion script.");
return Ok(());
}

let server = cli
.server
.unwrap_or_else(|| "127.0.0.1:51837".parse().unwrap());
Expand Down Expand Up @@ -136,6 +160,7 @@ async fn inner_main(cli: Cli) -> anyhow::Result<()> {
println!();
Ok(())
}
Commands::Completion { .. } => Ok(()),
}
}

Expand Down