1
0

feat(cli): implement register and unregister commands with scope support

- Add Target enum to handle user/system scope selection
- Move scope argument from global CLI to register/unregister subcommands
- Implement actual registration logic including ProgId subkey creation
- Update Program::new to accept string path instead of Path
- Add proper error handling and success messages
This commit is contained in:
2025-10-19 17:37:51 +08:00
parent f42c50ce10
commit d493285900
3 changed files with 93 additions and 48 deletions

View File

@@ -13,7 +13,7 @@ use assoc::{Ext, ProgId};
use indexmap::{IndexMap, IndexSet};
use regex::Regex;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::LazyLock;
use thiserror::Error as TeError;
use winreg::RegKey;
@@ -157,16 +157,19 @@ impl Program {
/// More preciously, `identifier` will be used as the vendor part of ProgId.
///
/// `full_path` is the fully qualified path to the application.
pub fn new(identifier: &str, full_path: &Path) -> Result<Self> {
pub fn new(identifier: &str, full_path: &str) -> Result<Self> {
// Check identifier
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9]*$").unwrap());
if !RE.is_match(identifier) {
return Err(Error::BadIdentifier(identifier.to_string()));
}
// Everything is okey, build self.
Ok(Self {
identifier: identifier.to_string(),
full_path: full_path.to_path_buf(),
// The error type of PathBuf FromStr trait is Infallible,
// so it must be okey and we can use unwrap safely.
full_path: full_path.parse().unwrap(),
manners: IndexSet::new(),
exts: IndexMap::new(),
})
@@ -260,6 +263,20 @@ impl Program {
}
}
// Create ProgId subkeys
debug_println!("Adding ProgId subkey...");
let subkey_parent = hk.open_subkey_with_flags(Self::CLASSES, KEY_READ)?;
for (ext, manner_token) in self.exts.iter() {
let manner = self.manners.get_index(*manner_token).ok_or(Error::InvalidMannerToken)?;
let prog_id = self.build_prog_id(ext);
debug_println!("Adding ProgId \"{0}\" subkey...", prog_id.to_string());
let (subkey, _) = subkey_parent.create_subkey_with_flags(prog_id.to_string(), KEY_READ)?;
let (subkey_verb, _) = subkey.create_subkey_with_flags("open", KEY_READ)?;
let (subkey_command, _) = subkey_verb.create_subkey_with_flags("command", KEY_WRITE)?;
subkey_command.set_value("", manner)?;
}
// Okey
utilities::notify_assoc_changed();
Ok(())
@@ -289,6 +306,16 @@ impl Program {
let subkey_parent = hk.open_subkey_with_flags(Self::APPLICATIONS, KEY_READ)?;
subkey_parent.delete_subkey_all(file_name)?;
// Remove ProgId subkeys
debug_println!("Removing ProgId subkey...");
let subkey_parent = hk.open_subkey_with_flags(Self::CLASSES, KEY_READ)?;
for ext in self.exts.keys() {
let prog_id = self.build_prog_id(ext);
debug_println!("Removing ProgId \"{0}\" subkey...", prog_id.to_string());
subkey_parent.delete_subkey_all(prog_id.to_string())?;
}
// Okey
utilities::notify_assoc_changed();
Ok(())