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

Add monitor command #18

Merged
merged 14 commits into from
Jul 28, 2024
146 changes: 142 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ anyhow = "1.0.81"
clap = { version = "4.5.4", features = ["derive"] }
# ANSI color codes for terminal output
colored = "2.1.0"
# TUI for the "monitor" command
crossterm = "0.27.0"
# Convert binary hash into hex
data-encoding = "2.6.0"
# Find the best place to sotre the VFS
directories = "5.0.1"
# Serialize app config into meta file in the ROM
firefly-meta = "0.1.2"
firefly-types = { version = "0.2.0" }
# Parse PNG images
image = { version = "0.25.1", default-features = false, features = ["png"] }
# Random device name generation
Expand Down
6 changes: 6 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub enum Commands {
/// Show the full path to the virtual filesystem.
Vfs,

/// Show runtime stats for a running device (or emulator).
Monitor(MonitorArgs),

/// Commands to manage signing keys.
#[command(subcommand)]
#[clap(alias("keys"))]
Expand Down Expand Up @@ -130,3 +133,6 @@ pub struct ImportArgs {
#[arg()]
pub path: String,
}

#[derive(Debug, Parser)]
pub struct MonitorArgs {}
6 changes: 3 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn cmd_build(vfs: PathBuf, args: &BuildArgs) -> anyhow::Result<()> {

/// Serialize and write the ROM meta information.
fn write_meta(config: &Config) -> anyhow::Result<()> {
use firefly_meta::{validate_id, validate_name, Meta};
use firefly_types::{validate_id, validate_name, Meta};
if let Err(err) = validate_id(&config.app_id) {
bail!("validate app_id: {err}");
}
Expand Down Expand Up @@ -116,7 +116,7 @@ fn write_meta(config: &Config) -> anyhow::Result<()> {

/// Write the latest installed app name into internal DB.
fn write_installed(config: &Config) -> anyhow::Result<()> {
let short_meta = firefly_meta::ShortMeta {
let short_meta = firefly_types::ShortMeta {
app_id: &config.app_id,
author_id: &config.author_id,
};
Expand Down Expand Up @@ -210,7 +210,7 @@ fn write_hash(rom_path: &Path) -> anyhow::Result<()> {
let hash = hash_dir(rom_path)?;
let hash_path = rom_path.join(HASH);
let mut hash_file = fs::File::create(hash_path).context("create file")?;
hash_file.write_all(&hash).context("write file")
hash_file.write_all(&hash[..]).context("write file")
}

/// Sign the ROM hash.
Expand Down
8 changes: 4 additions & 4 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ mod tests {
let dir = make_tmp_dir();
assert_eq!(dir.read_dir().unwrap().count(), 0);
std::fs::write(dir.join("somefile"), "hello").unwrap();
let hash1: &[u8] = &hash_dir(&dir).unwrap();
let hash2: &[u8] = &hash_dir(&dir).unwrap();
let hash1: &[u8] = &hash_dir(&dir).unwrap()[..];
let hash2: &[u8] = &hash_dir(&dir).unwrap()[..];
assert_eq!(hash1, hash2, "not idempotent");

std::fs::write(dir.join("somefile"), "hell").unwrap();
let hash3: &[u8] = &hash_dir(&dir).unwrap();
let hash3: &[u8] = &hash_dir(&dir).unwrap()[..];
assert!(hash2 != hash3, "doesn't change if file changed");

std::fs::write(dir.join("somefile2"), "hell").unwrap();
let hash4: &[u8] = &hash_dir(&dir).unwrap();
let hash4: &[u8] = &hash_dir(&dir).unwrap()[..];
assert!(hash3 != hash4, "doesn't change if fiels added");
}
}
6 changes: 3 additions & 3 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::file_names::{HASH, KEY, META, SIG};
use crate::vfs::init_vfs;
use anyhow::{bail, Context, Result};
use data_encoding::HEXLOWER;
use firefly_meta::Meta;
use firefly_types::Meta;
use rsa::pkcs1::DecodeRsaPublicKey;
use rsa::pkcs1v15::{Signature, VerifyingKey};
use rsa::signature::hazmat::PrehashVerifier;
Expand Down Expand Up @@ -97,7 +97,7 @@ fn read_meta_raw(archive: &mut ZipArchive<File>) -> Result<Vec<u8>> {

/// Write the latest installed app name into internal DB.
fn write_installed(meta: &Meta, vfs_path: &Path) -> anyhow::Result<()> {
let short_meta = firefly_meta::ShortMeta {
let short_meta = firefly_types::ShortMeta {
app_id: meta.app_id,
author_id: meta.author_id,
};
Expand All @@ -116,7 +116,7 @@ fn write_installed(meta: &Meta, vfs_path: &Path) -> anyhow::Result<()> {
fn verify(rom_path: &Path) -> anyhow::Result<()> {
let hash_path = rom_path.join(HASH);
let hash_expected: &[u8] = &fs::read(hash_path).context("read hash file")?;
let hash_actual: &[u8] = &hash_dir(rom_path).context("calculate hash")?;
let hash_actual: &[u8] = &hash_dir(rom_path).context("calculate hash")?[..];
if hash_actual != hash_expected {
let exp = HEXLOWER.encode(hash_expected);
let act = HEXLOWER.encode(hash_actual);
Expand Down
6 changes: 3 additions & 3 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const BIT_SIZE: usize = 2048;
pub fn cmd_key_new(vfs: &Path, args: &KeyArgs) -> anyhow::Result<()> {
init_vfs(vfs).context("init vfs")?;
let author = &args.author_id;
if let Err(err) = firefly_meta::validate_id(author) {
if let Err(err) = firefly_types::validate_id(author) {
bail!("invalid author ID: {err}")
}

Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn export_key(vfs: &Path, args: &KeyExportArgs, public: bool) -> anyhow::Res

pub fn cmd_key_rm(vfs: &Path, args: &KeyArgs) -> anyhow::Result<()> {
let author = &args.author_id;
if let Err(err) = firefly_meta::validate_id(author) {
if let Err(err) = firefly_types::validate_id(author) {
bail!("invalid author ID: {err}")
}

Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn cmd_key_add(vfs: &Path, args: &KeyArgs) -> anyhow::Result<()> {
let author = author.to_string();
let key_raw = fs::read(&key_path)?;
(author, key_raw)
} else if firefly_meta::validate_id(key_path).is_ok() {
} else if firefly_types::validate_id(key_path).is_ok() {
println!("⏳️ downloading the key from catalog...");
let url = format!("https://catalog.fireflyzero.com/keys/{key_path}.der");
download_key(&url)?
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(clippy::all, clippy::pedantic, clippy::nursery)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::option_if_let_else)]
#![allow(clippy::enum_glob_use)]

mod args;
mod build;
Expand All @@ -12,6 +13,7 @@ mod images;
mod import;
mod keys;
mod langs;
mod monitor;
mod vfs;
mod wasm;

Expand All @@ -23,6 +25,7 @@ use crate::build::cmd_build;
use crate::export::cmd_export;
use crate::import::cmd_import;
use crate::keys::{cmd_key_add, cmd_key_new, cmd_key_priv, cmd_key_pub, cmd_key_rm};
use crate::monitor::cmd_monitor;
use crate::vfs::{cmd_vfs, get_vfs_path};
use clap::Parser;
use colored::Colorize;
Expand All @@ -35,6 +38,7 @@ fn main() {
Commands::Build(args) => cmd_build(vfs, args),
Commands::Export(args) => cmd_export(&vfs, args),
Commands::Import(args) => cmd_import(&vfs, args),
Commands::Monitor(args) => cmd_monitor(&vfs, args),
Commands::Key(KeyCommands::New(args)) => cmd_key_new(&vfs, args),
Commands::Key(KeyCommands::Add(args)) => cmd_key_add(&vfs, args),
Commands::Key(KeyCommands::Pub(args)) => cmd_key_pub(&vfs, args),
Expand Down
Loading
Loading