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 commands to work with the device name #24

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand All @@ -95,6 +114,17 @@ pub struct KeyExportArgs {
pub output: Option<PathBuf>,
}

#[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.
Expand Down
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod import;
mod inspect;
mod keys;
mod monitor;
mod name;
mod repl;
mod vfs;

Expand All @@ -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;
33 changes: 33 additions & 0 deletions src/commands/name.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading