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:
@ -2,13 +2,11 @@ pub(crate) mod cli;
|
||||
pub(crate) mod manifest;
|
||||
|
||||
use clap::Parser;
|
||||
use cli::{Cli, Commands};
|
||||
use cli::{Cli, Commands, Target};
|
||||
use comfy_table::Table;
|
||||
use manifest::Manifest;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::str::FromStr;
|
||||
use thiserror::Error as TeError;
|
||||
use wfassoc::{Program, Scope, Token, View};
|
||||
|
||||
@ -46,10 +44,7 @@ impl Composition {
|
||||
// Open file and read manifest TOML file
|
||||
let mf = Manifest::from_file(&cli.config_file)?;
|
||||
// Create instance
|
||||
let mut program = Program::new(
|
||||
&mf.identifier,
|
||||
PathBuf::from_str(&cli.config_file).unwrap().as_path(),
|
||||
)?;
|
||||
let mut program = Program::new(&mf.identifier, &cli.config_file)?;
|
||||
// Setup manner
|
||||
let mut manner_token_map: HashMap<&str, Token> = HashMap::new();
|
||||
for (k, v) in mf.manners.iter() {
|
||||
@ -82,25 +77,40 @@ impl Composition {
|
||||
&self.program
|
||||
}
|
||||
|
||||
pub fn get_mut_program(&mut self) -> &mut Program {
|
||||
&mut self.program
|
||||
}
|
||||
|
||||
pub fn iter_ext_tokens(&self) -> impl Iterator<Item = Token> {
|
||||
self.ext_tokens.iter().copied()
|
||||
}
|
||||
}
|
||||
|
||||
fn run_register(cli: &Cli) -> Result<()> {
|
||||
// let program = Program::new();
|
||||
// let kind: RegisterKind = cli.for_which.into();
|
||||
// program.register(kind)?;
|
||||
fn run_register(cli: &Cli, target: &Target) -> Result<()> {
|
||||
let composition = Composition::new(cli)?;
|
||||
let scope = target.clone().into();
|
||||
|
||||
// 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(())
|
||||
}
|
||||
|
||||
fn run_unregister(cli: &Cli) -> Result<()> {
|
||||
// let program = Program::new();
|
||||
// program.unregister()?;
|
||||
fn run_unregister(cli: &Cli, target: &Target) -> Result<()> {
|
||||
let composition = Composition::new(cli)?;
|
||||
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(())
|
||||
}
|
||||
|
||||
@ -140,8 +150,8 @@ fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let rv = match &cli.command {
|
||||
Commands::Register => run_register(&cli),
|
||||
Commands::Unregister => run_unregister(&cli),
|
||||
Commands::Register { target } => run_register(&cli, target),
|
||||
Commands::Unregister { target } => run_unregister(&cli, target),
|
||||
Commands::Query => run_query(&cli),
|
||||
};
|
||||
rv.unwrap_or_else(|e| {
|
||||
|
||||
Reference in New Issue
Block a user