added enarx version no. when enarx info and enarx info --json is run, used fmt::Display to print the version no.
use crate::backend::BACKENDS;
use crate::cli::{Result, StructOpt};
+use crate::Backend;
+use core::fmt::Formatter;
+use serde::Serialize;
+use std::fmt;
use std::ops::Deref;
-
/// Show details about backend support on this system
#[derive(StructOpt, Debug)]
pub struct Options {
@@ -12,18 +15,19 @@ pub struct Options {
json: bool,
}
-impl Options {
- /// Display nicely-formatted info about each backend
- pub fn display(self) -> Result<()> {
- use colorful::*;
-
- let backends = BACKENDS.deref();
+#[derive(Serialize)]
+struct Info<'a> {
+ version: &'static str,
+ backends: &'a Vec<Box<dyn Backend>>,
+}
- if self.json {
- println!("{}", serde_json::to_string_pretty(&backends)?);
- } else {
+impl fmt::Display for Info<'_> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ use colorful::*;
+ let backends = self.backends;
+ writeln!(f, "Version {} starting up", self.version)?;
for backend in backends {
- println!("Backend: {}", backend.name());
+ writeln!(f, "Backend: {}", backend.name())?;
let data = backend.data();
@@ -34,18 +38,35 @@ impl Options {
};
if let Some(info) = datum.info.as_ref() {
- println!(" {} {}: {}", icon, datum.name, info);
+ writeln!(f, " {} {}: {}", icon, datum.name, info)?;
} else {
- println!(" {} {}", icon, datum.name);
+ writeln!(f, " {} {}", icon, datum.name)?;
}
}
for datum in &data {
if let Some(mesg) = datum.mesg.as_ref() {
- println!("\n{}\n", mesg);
+ writeln!(f, "\n{}\n", mesg)?;
}
}
}
+ Ok(())
+ }
+}
+
+impl Options {
+ /// Display nicely-formatted info about each backend
+ pub fn display(self) -> Result<()> {
+ let backends = BACKENDS.deref();
+
+ let info = Info {
+ version: env!("CARGO_PKG_VERSION"),
+ backends,
+ };
+ if self.json {
+ println!("{}", serde_json::to_string_pretty(&info)?);
+ } else {
+ println!("{}", info);
}
Ok(())
