From 2995769edddcd66ef430884bc760c129f0e4400f Mon Sep 17 00:00:00 2001 From: gram Date: Sat, 5 Oct 2024 09:19:08 +0200 Subject: [PATCH] Add commands to work with the device name --- src/args.rs | 30 ++++++++++++++++++++++++++++++ src/cli.rs | 7 ++++--- src/commands/inspect.rs | 1 + src/commands/mod.rs | 2 ++ src/commands/name.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/vfs.rs | 2 +- 7 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/commands/name.rs diff --git a/src/args.rs b/src/args.rs index 611ff80..62091fd 100644 --- a/src/args.rs +++ b/src/args.rs @@ -42,6 +42,10 @@ pub enum Commands { #[clap(alias("keys"))] Key(KeyCommands), + /// Commands to set, get, and generate device name. + #[command(subcommand)] + Name(NameCommands), + /// Commands to interact with catalog.fireflyzero.com. #[command(subcommand)] Catalog(CatalogCommands), @@ -70,6 +74,21 @@ pub enum KeyCommands { Rm(KeyArgs), } +#[derive(Subcommand, Debug)] +pub enum NameCommands { + /// Show the current device name. + #[clap(alias("show"), alias("echo"))] + Get(NameGetArgs), + + /// Set a new device name. + #[clap(alias("change"))] + Set(NameSetArgs), + + /// Set a new device name. + #[clap(alias("gen"), alias("new"))] + Generate(NameGenerateArgs), +} + #[derive(Subcommand, Debug)] pub enum CatalogCommands { /// List all games available in the catalog. @@ -95,6 +114,17 @@ pub struct KeyExportArgs { pub output: Option, } +#[derive(Debug, Parser)] +pub struct NameGetArgs {} + +#[derive(Debug, Parser)] +pub struct NameSetArgs { + pub name: String, +} + +#[derive(Debug, Parser)] +pub struct NameGenerateArgs {} + #[derive(Debug, Parser)] pub struct BuildArgs { /// Path to the project root. diff --git a/src/cli.rs b/src/cli.rs index 5b558d4..826ebe3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,4 @@ -use crate::args::CatalogCommands; -use crate::args::{Commands, KeyCommands}; -#[allow(clippy::wildcard_imports)] +use crate::args::*; use crate::commands::*; use std::fmt::Display; use std::path::PathBuf; @@ -21,6 +19,9 @@ pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> { Commands::Key(KeyCommands::Rm(args)) => cmd_key_rm(&vfs, args), Commands::Catalog(CatalogCommands::List(args)) => cmd_catalog_list(args), Commands::Catalog(CatalogCommands::Show(args)) => cmd_catalog_show(args), + Commands::Name(NameCommands::Get(args)) => cmd_name_get(&vfs, args), + Commands::Name(NameCommands::Set(args)) => cmd_name_set(&vfs, args), + Commands::Name(NameCommands::Generate(args)) => cmd_name_generate(&vfs, args), Commands::Vfs => cmd_vfs(), } } diff --git a/src/commands/inspect.rs b/src/commands/inspect.rs index 157c944..5593454 100644 --- a/src/commands/inspect.rs +++ b/src/commands/inspect.rs @@ -151,6 +151,7 @@ fn print_wasm_stats(stats: &WasmStats) { } println!(" {}: {}", "exports".cyan(), stats.exports.len()); for export in &stats.exports { + // TODO: when we stabilize the list of callbacks, highlight unknown exports. println!(" {export}"); } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 01ce715..1d600ff 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,6 +6,7 @@ mod import; mod inspect; mod keys; mod monitor; +mod name; mod repl; mod vfs; @@ -17,5 +18,6 @@ pub use import::cmd_import; pub use inspect::cmd_inspect; pub use keys::{cmd_key_add, cmd_key_new, cmd_key_priv, cmd_key_pub, cmd_key_rm}; pub use monitor::cmd_monitor; +pub use name::{cmd_name_generate, cmd_name_get, cmd_name_set}; pub use repl::cmd_repl; pub use vfs::cmd_vfs; diff --git a/src/commands/name.rs b/src/commands/name.rs new file mode 100644 index 0000000..c6be231 --- /dev/null +++ b/src/commands/name.rs @@ -0,0 +1,33 @@ +use crate::{args::*, vfs::generate_valid_name}; +use anyhow::{bail, Result}; +use std::{fs, path::Path}; + +pub fn cmd_name_get(vfs: &Path, _args: &NameGetArgs) -> Result<()> { + let name_path = vfs.join("sys").join("name"); + let name = fs::read_to_string(name_path)?; + if let Err(err) = firefly_types::validate_id(&name) { + println!("⚠️ the name is not valid: {err}"); + } + println!("{name}"); + Ok(()) +} + +pub fn cmd_name_set(vfs: &Path, args: &NameSetArgs) -> Result<()> { + let name_path = vfs.join("sys").join("name"); + let old_name = fs::read_to_string(&name_path)?; + println!("old name: {old_name}"); + if let Err(err) = firefly_types::validate_id(&args.name) { + bail!("validate new name: {err}"); + } + fs::write(name_path, &args.name)?; + println!("new name: {}", &args.name); + Ok(()) +} + +pub fn cmd_name_generate(vfs: &Path, _args: &NameGenerateArgs) -> Result<()> { + let name_path = vfs.join("sys").join("name"); + let name = generate_valid_name(); + fs::write(name_path, &name)?; + println!("new name: {name}"); + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 1564098..552a7d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![allow(clippy::module_name_repetitions)] #![allow(clippy::option_if_let_else)] #![allow(clippy::enum_glob_use)] +#![allow(clippy::wildcard_imports)] mod args; mod cli; diff --git a/src/vfs.rs b/src/vfs.rs index 0f36664..e440200 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -42,7 +42,7 @@ pub fn init_vfs(path: &Path) -> anyhow::Result<()> { } /// Generate a random valid device name. -fn generate_valid_name() -> String { +pub fn generate_valid_name() -> String { loop { let name = generate_name(); if firefly_types::validate_id(&name).is_ok() {