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:
@ -13,7 +13,7 @@ use assoc::{Ext, ProgId};
|
|||||||
use indexmap::{IndexMap, IndexSet};
|
use indexmap::{IndexMap, IndexSet};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
use thiserror::Error as TeError;
|
use thiserror::Error as TeError;
|
||||||
use winreg::RegKey;
|
use winreg::RegKey;
|
||||||
@ -157,16 +157,19 @@ impl Program {
|
|||||||
/// More preciously, `identifier` will be used as the vendor part of ProgId.
|
/// More preciously, `identifier` will be used as the vendor part of ProgId.
|
||||||
///
|
///
|
||||||
/// `full_path` is the fully qualified path to the application.
|
/// `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
|
// Check identifier
|
||||||
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9]*$").unwrap());
|
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9]*$").unwrap());
|
||||||
if !RE.is_match(identifier) {
|
if !RE.is_match(identifier) {
|
||||||
return Err(Error::BadIdentifier(identifier.to_string()));
|
return Err(Error::BadIdentifier(identifier.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Everything is okey, build self.
|
// Everything is okey, build self.
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
identifier: identifier.to_string(),
|
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(),
|
manners: IndexSet::new(),
|
||||||
exts: IndexMap::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
|
// Okey
|
||||||
utilities::notify_assoc_changed();
|
utilities::notify_assoc_changed();
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -289,6 +306,16 @@ impl Program {
|
|||||||
let subkey_parent = hk.open_subkey_with_flags(Self::APPLICATIONS, KEY_READ)?;
|
let subkey_parent = hk.open_subkey_with_flags(Self::APPLICATIONS, KEY_READ)?;
|
||||||
subkey_parent.delete_subkey_all(file_name)?;
|
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
|
// Okey
|
||||||
utilities::notify_assoc_changed();
|
utilities::notify_assoc_changed();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@ -1,10 +1,28 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
|
use wfassoc::Scope;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, ValueEnum)]
|
||||||
|
pub enum Target {
|
||||||
|
#[value(name = "user")]
|
||||||
|
User,
|
||||||
|
#[value(name = "system")]
|
||||||
|
System,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Target> for Scope {
|
||||||
|
fn from(target: Target) -> Self {
|
||||||
|
match target {
|
||||||
|
Target::User => Scope::User,
|
||||||
|
Target::System => Scope::System,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Simple program to manage Windows file associations
|
/// Simple program to manage Windows file associations
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "Windows File Association Operator", version, about)]
|
#[command(name = "Windows File Association Operator", version, about)]
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
/// The toml file introducing the complete program
|
/// The TOML file representing the complete program
|
||||||
#[arg(
|
#[arg(
|
||||||
short = 'c',
|
short = 'c',
|
||||||
long = "config",
|
long = "config",
|
||||||
@ -13,40 +31,30 @@ pub struct Cli {
|
|||||||
)]
|
)]
|
||||||
pub(crate) config_file: String,
|
pub(crate) config_file: String,
|
||||||
|
|
||||||
/// The scope where wfassoc operate
|
|
||||||
#[arg(short = 'f', long = "for", value_name = "TARGET", value_enum, default_value_t = ForTarget::User)]
|
|
||||||
pub(crate) for_which: ForTarget,
|
|
||||||
|
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub(crate) command: Commands,
|
pub(crate) command: Commands,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::ValueEnum, Clone)]
|
|
||||||
pub enum ForTarget {
|
|
||||||
#[value(name = "user")]
|
|
||||||
User,
|
|
||||||
#[value(name = "system")]
|
|
||||||
System,
|
|
||||||
}
|
|
||||||
|
|
||||||
// impl From<ForTarget> for RegisterKind {
|
|
||||||
// fn from(target: ForTarget) -> Self {
|
|
||||||
// match target {
|
|
||||||
// ForTarget::User => RegisterKind::User,
|
|
||||||
// ForTarget::System => RegisterKind::System,
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum Commands {
|
pub enum Commands {
|
||||||
/// Register the program
|
/// Register the program
|
||||||
#[command(name = "register")]
|
#[command(name = "register")]
|
||||||
Register,
|
#[command(about = "Register application with given manifest and scope.")]
|
||||||
|
Register {
|
||||||
|
/// The scope where wfassoc operate
|
||||||
|
#[arg(short = 't', long = "target", value_name = "TARGET", value_enum, default_value_t = Target::User)]
|
||||||
|
target: Target,
|
||||||
|
},
|
||||||
/// Unregister the program
|
/// Unregister the program
|
||||||
#[command(name = "unregister")]
|
#[command(name = "unregister")]
|
||||||
Unregister,
|
#[command(about = "Unregister application with given manifest and scope.")]
|
||||||
|
Unregister {
|
||||||
|
/// The scope where wfassoc operate
|
||||||
|
#[arg(short = 't', long = "target", value_name = "TARGET", value_enum, default_value_t = Target::User)]
|
||||||
|
target: Target,
|
||||||
|
},
|
||||||
/// Query file associations
|
/// Query file associations
|
||||||
#[command(name = "query")]
|
#[command(name = "query")]
|
||||||
|
#[command(about = "Query file extensions association infos according toh given manifest represented application.")]
|
||||||
Query,
|
Query,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,13 +2,11 @@ pub(crate) mod cli;
|
|||||||
pub(crate) mod manifest;
|
pub(crate) mod manifest;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cli::{Cli, Commands};
|
use cli::{Cli, Commands, Target};
|
||||||
use comfy_table::Table;
|
use comfy_table::Table;
|
||||||
use manifest::Manifest;
|
use manifest::Manifest;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::str::FromStr;
|
|
||||||
use thiserror::Error as TeError;
|
use thiserror::Error as TeError;
|
||||||
use wfassoc::{Program, Scope, Token, View};
|
use wfassoc::{Program, Scope, Token, View};
|
||||||
|
|
||||||
@ -46,10 +44,7 @@ impl Composition {
|
|||||||
// Open file and read manifest TOML file
|
// Open file and read manifest TOML file
|
||||||
let mf = Manifest::from_file(&cli.config_file)?;
|
let mf = Manifest::from_file(&cli.config_file)?;
|
||||||
// Create instance
|
// Create instance
|
||||||
let mut program = Program::new(
|
let mut program = Program::new(&mf.identifier, &cli.config_file)?;
|
||||||
&mf.identifier,
|
|
||||||
PathBuf::from_str(&cli.config_file).unwrap().as_path(),
|
|
||||||
)?;
|
|
||||||
// Setup manner
|
// Setup manner
|
||||||
let mut manner_token_map: HashMap<&str, Token> = HashMap::new();
|
let mut manner_token_map: HashMap<&str, Token> = HashMap::new();
|
||||||
for (k, v) in mf.manners.iter() {
|
for (k, v) in mf.manners.iter() {
|
||||||
@ -82,25 +77,40 @@ impl Composition {
|
|||||||
&self.program
|
&self.program
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut_program(&mut self) -> &mut Program {
|
|
||||||
&mut self.program
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iter_ext_tokens(&self) -> impl Iterator<Item = Token> {
|
pub fn iter_ext_tokens(&self) -> impl Iterator<Item = Token> {
|
||||||
self.ext_tokens.iter().copied()
|
self.ext_tokens.iter().copied()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_register(cli: &Cli) -> Result<()> {
|
fn run_register(cli: &Cli, target: &Target) -> Result<()> {
|
||||||
// let program = Program::new();
|
let composition = Composition::new(cli)?;
|
||||||
// let kind: RegisterKind = cli.for_which.into();
|
let scope = target.clone().into();
|
||||||
// program.register(kind)?;
|
|
||||||
|
// Register program
|
||||||
|
let program = composition.get_program();
|
||||||
|
program.register(scope)?;
|
||||||
|
// Link all file extensions
|
||||||
|
for token in composition.iter_ext_tokens() {
|
||||||
|
program.link_ext(token, scope)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Register OK.");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_unregister(cli: &Cli) -> Result<()> {
|
fn run_unregister(cli: &Cli, target: &Target) -> Result<()> {
|
||||||
// let program = Program::new();
|
let composition = Composition::new(cli)?;
|
||||||
// program.unregister()?;
|
let scope = target.clone().into();
|
||||||
|
|
||||||
|
// Unlink all file extensions
|
||||||
|
let program = composition.get_program();
|
||||||
|
for token in composition.iter_ext_tokens() {
|
||||||
|
program.unlink_ext(token, scope)?;
|
||||||
|
}
|
||||||
|
// Unregister prorgam
|
||||||
|
program.unregister(scope)?;
|
||||||
|
|
||||||
|
println!("Unregister OK.");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,8 +150,8 @@ fn main() {
|
|||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
let rv = match &cli.command {
|
let rv = match &cli.command {
|
||||||
Commands::Register => run_register(&cli),
|
Commands::Register { target } => run_register(&cli, target),
|
||||||
Commands::Unregister => run_unregister(&cli),
|
Commands::Unregister { target } => run_unregister(&cli, target),
|
||||||
Commands::Query => run_query(&cli),
|
Commands::Query => run_query(&cli),
|
||||||
};
|
};
|
||||||
rv.unwrap_or_else(|e| {
|
rv.unwrap_or_else(|e| {
|
||||||
|
|||||||
Reference in New Issue
Block a user