提交 4facb7f2 authored 作者: Serhij S's avatar Serhij S

show versions

上级 86eba48f
差异被折叠。
...@@ -32,7 +32,7 @@ pub enum SubCommand { ...@@ -32,7 +32,7 @@ pub enum SubCommand {
#[clap(name = "new", about = "Generate a new project")] #[clap(name = "new", about = "Generate a new project")]
New(NewCommand), New(NewCommand),
#[clap(name = "stat", about = "Get program status")] #[clap(name = "stat", about = "Get program status")]
Stat, Stat(StatCommand),
#[clap(name = "config", about = "Switch remote into CONFIG mode")] #[clap(name = "config", about = "Switch remote into CONFIG mode")]
Config, Config,
#[clap(name = "run", about = "Switch remote into RUN mode")] #[clap(name = "run", about = "Switch remote into RUN mode")]
...@@ -118,6 +118,12 @@ impl LockingPolicy { ...@@ -118,6 +118,12 @@ impl LockingPolicy {
} }
} }
#[derive(Parser)]
pub struct StatCommand {
#[clap(long, help = "Show remote versions (requires RoboPLC Pro)")]
pub show_versions: bool,
}
#[derive(Parser)] #[derive(Parser)]
pub struct FlashCommand { pub struct FlashCommand {
#[clap(long, env = "CARGO", help = "cargo/cross binary path")] #[clap(long, env = "CARGO", help = "cargo/cross binary path")]
......
...@@ -102,8 +102,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { ...@@ -102,8 +102,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
SubCommand::New(_) => { SubCommand::New(_) => {
panic!("BUG"); panic!("BUG");
} }
SubCommand::Stat => { SubCommand::Stat(opts) => {
remote::stat(&url, &key, agent)?; remote::stat(&url, &key, agent, opts.show_versions)?;
} }
SubCommand::Config => { SubCommand::Config => {
remote::set_mode(&url, &key, &agent, Mode::Config, true)?; remote::set_mode(&url, &key, &agent, Mode::Config, true)?;
......
use bma_ts::Timestamp;
use prettytable::{cell, row, Table};
use serde::Deserialize;
use ureq::Agent; use ureq::Agent;
use crate::{ use crate::{
...@@ -6,7 +9,12 @@ use crate::{ ...@@ -6,7 +9,12 @@ use crate::{
API_PREFIX, API_PREFIX,
}; };
pub fn stat(url: &str, key: &str, agent: Agent) -> Result<(), Box<dyn std::error::Error>> { pub fn stat(
url: &str,
key: &str,
agent: Agent,
show_versions: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let resp = agent let resp = agent
.post(&format!("{}{}/query.stats.program", url, API_PREFIX)) .post(&format!("{}{}/query.stats.program", url, API_PREFIX))
.set("x-auth-key", key) .set("x-auth-key", key)
...@@ -14,9 +22,62 @@ pub fn stat(url: &str, key: &str, agent: Agent) -> Result<(), Box<dyn std::error ...@@ -14,9 +22,62 @@ pub fn stat(url: &str, key: &str, agent: Agent) -> Result<(), Box<dyn std::error
.process_error()?; .process_error()?;
let stats: State = resp.into_json()?; let stats: State = resp.into_json()?;
stats.print_std(); stats.print_std();
if show_versions {
println!();
let resp = agent
.post(&format!("{}{}/query.program.meta", url, API_PREFIX))
.set("x-auth-key", key)
.call()
.process_error()?;
let meta: PlcMetadata = resp.into_json()?;
let mut table = Table::new();
table.add_row(row!["Program", "Exists", "Created"]);
table.add_row(row![
"current",
meta.program_current.exists_as_cell(),
meta.program_current.created_iso()?
]);
for (i, program) in meta.program_previous.iter().enumerate() {
table.add_row(row![
format!("prev.{}", i),
program.exists_as_cell(),
program.created_iso()?
]);
}
table.printstd();
}
Ok(()) Ok(())
} }
#[derive(Deserialize)]
struct PlcMetadata {
program_current: ProgramFileMetdata,
#[serde(default)]
program_previous: Vec<ProgramFileMetdata>,
}
#[derive(Deserialize)]
struct ProgramFileMetdata {
exists: bool,
created: Timestamp,
}
impl ProgramFileMetdata {
fn exists_as_cell(&self) -> prettytable::Cell {
if !self.exists {
cell!("YES")
} else {
cell!(Fr->"NO")
}
}
fn created_iso(&self) -> Result<String, Box<dyn std::error::Error>> {
Ok(self
.created
.try_into_datetime_local()?
.to_rfc3339_opts(chrono::SecondsFormat::Secs, true))
}
}
pub fn set_mode( pub fn set_mode(
url: &str, url: &str,
key: &str, key: &str,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论