Skip to content

Commit

Permalink
add --json command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Nov 29, 2024
1 parent af20944 commit e725d5e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

60 changes: 60 additions & 0 deletions crates/rugpi-common/src/system/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use super::System;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SystemInfo {
boot_flow: String,
slots: IndexMap<String, SlotInfo>,
boot_groups: IndexMap<String, BootGroupInfo>,
default_boot_group: Option<String>,
active_boot_group: Option<String>,
}

impl SystemInfo {
pub fn from(system: &System) -> Self {
let boot_flow = system.boot_flow().name().to_owned();
let slots = system
.slots()
.iter()
.map(|(_, slot)| {
(
slot.name().to_owned(),
SlotInfo {
active: slot.active(),
},
)
})
.collect();
let active_boot_group = system
.active_boot_entry()
.map(|idx| system.boot_entries()[idx].name().to_owned());
let default_boot_group = Some(
system.boot_entries()[system.boot_flow().get_default(system).unwrap()]
.name()
.to_owned(),
);
let boot_groups = system
.boot_entries()
.iter()
.map(|(_, group)| (group.name().to_owned(), BootGroupInfo {}))
.collect();
Self {
boot_flow,
slots,
boot_groups,
active_boot_group,
default_boot_group,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlotInfo {
active: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BootGroupInfo {}
1 change: 1 addition & 0 deletions crates/rugpi-common/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{disk::blkdev::BlockDevice, Anyhow};
pub mod boot_entries;
pub mod boot_flows;
pub mod config;
pub mod info;
pub mod partitions;
pub mod paths;
pub mod root;
Expand Down
1 change: 1 addition & 0 deletions crates/rugpi-ctrl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sha2 = "0.10.8"
hex = "0.4.3"
tracing.workspace = true
indexmap = "2.5.0"
serde_json = "1.0.133"

[lints]
workspace = true
34 changes: 22 additions & 12 deletions crates/rugpi-ctrl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rugpi_common::{
stream_hasher::StreamHasher,
system::{
boot_entries::{BootEntry, BootEntryIdx},
info::SystemInfo,
slots::SlotKind,
System,
},
Expand Down Expand Up @@ -142,17 +143,22 @@ pub fn main() -> Anyhow<()> {
}
}
Command::System(sys_cmd) => match sys_cmd {
SystemCommand::Info => {
println!("Boot Flow: {}", system.boot_flow().name());
let hot = system.active_boot_entry().unwrap();
let default = system.boot_flow().get_default(&system)?;
let spare = system.spare_entry()?.unwrap().0;
let cold = if hot == default { spare } else { default };
let entries = system.boot_entries();
println!("Hot: {}", entries[hot].name());
println!("Cold: {}", entries[cold].name());
println!("Default: {}", entries[default].name());
println!("Spare: {}", entries[spare].name());
SystemCommand::Info { json } => {
if *json {
let info = SystemInfo::from(&system);
serde_json::to_writer_pretty(std::io::stdout(), &info)?;
} else {
println!("Boot Flow: {}", system.boot_flow().name());
let hot = system.active_boot_entry().unwrap();
let default = system.boot_flow().get_default(&system)?;
let spare = system.spare_entry()?.unwrap().0;
let cold = if hot == default { spare } else { default };
let entries = system.boot_entries();
println!("Hot: {}", entries[hot].name());
println!("Cold: {}", entries[cold].name());
println!("Default: {}", entries[default].name());
println!("Spare: {}", entries[spare].name());
}
}
SystemCommand::Commit => {
if system.needs_commit()? {
Expand Down Expand Up @@ -397,7 +403,11 @@ pub enum UpdateRebootType {

#[derive(Debug, Parser)]
pub enum SystemCommand {
Info,
Info {
/// Output system information as JSON.
#[clap(long)]
json: bool,
},
/// Make the hot system the default.
Commit,
/// Reboot the system.
Expand Down

0 comments on commit e725d5e

Please sign in to comment.