feat: arguments with clip

This commit is contained in:
Strix 2023-10-14 22:39:42 +02:00
parent 09250c59cb
commit 1d3895bd43
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
6 changed files with 85 additions and 46 deletions

View file

@ -7,7 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
fern = "0.6.2" fern = "0.6.2"
getopts = "0.2.21"
log = "0.4.19" log = "0.4.19"
regex = "1.9.1" regex = "1.9.1"
manifest = { path = "../manifest" } manifest = { path = "../manifest" }
clap = { version = "4.3.12", features = ["derive"] }

View file

@ -1,9 +1,63 @@
use crate::package::identifier::PackageIdentifier; use crate::package::identifier::PackageIdentifier;
use crate::prelude::Arguments; use clap::{Parser, Subcommand};
use log::error;
pub enum Command { #[derive(Parser, Debug)]
Install(Arguments, PackageIdentifier), // install a package from a remote source #[clap(name = "pkgr", version = "0.1.0", author = "")]
Remove(PackageIdentifier), // remove a package from the local source pub struct Cli {
List, // list all packages in the local source #[command(subcommand)]
Update, pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Install a package
Install {
#[arg(short, long, default_value_t = false)]
build: bool,
package_identifier: PackageIdentifier,
},
/// Remove a package from the system
Remove {
package_identifier: PackageIdentifier,
},
/// List packages
List {
/// List installed packages
#[arg(short, long, default_value_t = true)]
installed: bool,
},
/// Update packages on the system
Update,
#[command(hide = true)]
None,
}
impl Command {
pub fn execute(&self) {
match self {
Command::Install {
build,
package_identifier,
} => {
if *build {
error!("Build is not yet implemented.");
} else {
error!("Install is not yet implemented.");
}
}
Command::Remove { package_identifier } => {
error!("Remove is not yet implemented.");
}
Command::List { installed } => {
error!("List is not yet implemented.");
}
Command::Update => {
error!("Update is not yet implemented.");
}
Command::None => {
error!("No command was specified.");
}
}
}
} }

View file

@ -1,45 +1,19 @@
use clap::Parser;
use std::env; use std::env;
use std::process::exit; use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use getopts::Options; use crate::commands::{Cli, Command};
use log::{info, SetLoggerError}; use log::{info, SetLoggerError};
mod prelude;
mod commands; mod commands;
mod package; mod package;
fn main() { fn main() {
setup_logger() setup_logger().expect("Unable to setup logger.");
.expect("Unable to setup logger.");
let args: Vec<String> = env::args().collect(); let c = Cli::parse();
let program = args[0].clone(); info!("Command: {:?}", c);
let mut opts = Options::new();
opts.optflag("h", "help", "Show help message.");
let matches = opts.parse(&args[1..]).unwrap_or_else(|e| panic!("{}", e.to_string()));
if matches.opt_present("h") {
print_usage(&program, opts);
exit(1);
}
let command = if !matches.free.is_empty() {
matches.free[0].clone()
} else {
print_usage(&program, opts);
exit(1);
};
info!("Identifier: {}", package::identifier::PackageIdentifier::from_str(&command).unwrap());
}
fn print_usage(program: &str, opts: Options) {
let mut brief = format!("Usage: {} <COMMAND> [options]\n\n", program);
brief += &*format!("Commands: grab \n");
brief += &*format!(" remove\n");
print!("{}", opts.usage(&brief.trim_end()));
} }
fn setup_logger() -> Result<(), SetLoggerError> { fn setup_logger() -> Result<(), SetLoggerError> {

View file

@ -1,8 +1,9 @@
use std::error::Error;
use std::str::FromStr; use std::str::FromStr;
use regex::Regex; use regex::Regex;
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum PackageIdentifierError { pub enum PackageIdentifierError {
InvalidPackageLocator(String), InvalidPackageLocator(String),
InvalidURI(String), InvalidURI(String),
@ -11,13 +12,17 @@ pub enum PackageIdentifierError {
impl std::fmt::Display for PackageIdentifierError { impl std::fmt::Display for PackageIdentifierError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
PackageIdentifierError::InvalidPackageLocator(s) => write!(f, "Invalid package locator: {}", s), PackageIdentifierError::InvalidPackageLocator(s) => {
write!(f, "Invalid package locator: {}", s)
}
PackageIdentifierError::InvalidURI(s) => write!(f, "Invalid URI: {}", s), PackageIdentifierError::InvalidURI(s) => write!(f, "Invalid URI: {}", s),
} }
} }
} }
#[derive(Debug)] impl Error for PackageIdentifierError {}
#[derive(Debug, Clone)]
pub enum PackageIdentifier { pub enum PackageIdentifier {
PackageLocator(PackageLocator), PackageLocator(PackageLocator),
URI(String), URI(String),
@ -32,7 +37,7 @@ impl std::fmt::Display for PackageIdentifier {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct PackageLocator { pub struct PackageLocator {
pub name: String, pub name: String,
pub version: Option<u32>, pub version: Option<u32>,
@ -102,7 +107,14 @@ impl FromStr for PackageLocator {
} }
if let Some(caps) = tags_re.captures(s) { if let Some(caps) = tags_re.captures(s) {
tags = Some(caps.get(1).unwrap().as_str().split(",").map(|s| s.to_string()).collect()); tags = Some(
caps.get(1)
.unwrap()
.as_str()
.split(",")
.map(|s| s.to_string())
.collect(),
);
} }
Ok(PackageLocator { Ok(PackageLocator {

View file

@ -1 +0,0 @@
pub struct Arguments(Vec<String>);