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

Update to clap 4 + rework to declarative style #32

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
68 changes: 55 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["command-line-utilities"]
bat = { version = "0.21.0", default-features = false, features = ["paging", "regex-fancy"]}
colored = { git = "https://github.com/mackwic/colored" }
chrono = "0.4.19"
clap = { version = "3.2.16", features = ["cargo", "env"] }
clap = { version = "4", features = ["derive", "env", "cargo"] }
tonky marked this conversation as resolved.
Show resolved Hide resolved
dirs = "4.0.0"
edit = "0.1.4"
exitcode = "1.1.2"
Expand All @@ -24,7 +24,7 @@ makedeb-srcinfo = "0.8.0"
quit = "1.1.4"
regex = "1.6.0"
reqwest = { version = "0.11.11", features = ["blocking", "json"] }
rust-apt = { git = "https://gitlab.com/volian/rust-apt", branch = "fix/tagfile-parsing" }
rust-apt = { git = "https://gitlab.com/volian/rust-apt" }
serde = { version = "1.0.142", features = ["derive"] }
serde_json = "1.0.83"
tempfile = "3.3.0"
Expand Down
182 changes: 182 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use clap::{Args, Parser, Subcommand, ValueEnum};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
/// Clone a package base from the MPR
Clone {
/// The package to clone
#[arg(required = true)]
package_name: String,

#[command(flatten)]
mpr_url: MprURL,
},
/// Comment on a package page
Comment {
/// The package to comment on
#[arg(required = true)]
package_name: String,

/// The comment to post
#[arg(short, long = "msg")]
message: Option<String>,

#[command(flatten)]
mpr_url: MprURL,

#[command(flatten)]
mpr_token: MprToken,
},
/// Install packages from APT and the MPR
Install {
/// The package(s) to install
#[arg(required = true)]
package_names: Vec<String>,

#[command(flatten)]
mpr_url: MprURL,
},
/// List packages available via APT and the MPR
List {
#[command(flatten)]
mpr_url: MprURL,

/// Output the package's name without any extra details
#[arg(long = "name-only", default_value_t = false)]
name_only: bool,

#[arg(long, value_enum, default_value_t=SearchMode::None)]
mode: SearchMode,

/// The package(s) to get information for
#[arg(required = true)]
package_names: Vec<String>,
},
/// List the comments on a package
ListComments {
#[command(flatten)]
mpr_url: MprURL,

/// When to send output to a pager
#[arg(long, value_enum, default_value_t=Paging::Auto)]
paging: Paging,

/// The package(s) to get information for
#[arg(required = true)]
package_name: String,
},
/// Remove packages from the system
Remove {
#[command(flatten)]
mpr_url: MprURL,

/// Remove configuration files along with the package(s)
#[arg(long)]
purge: bool,

/// Remove configuration files along with the package(s)
#[arg(long)]
autoremove: bool,

/// The package(s) to get information for
#[arg(required = true)]
package_names: Vec<String>,
},
/// Search for an APT/MPR package
Search {
#[command(flatten)]
mpr_url: MprURL,

/// Output the package's name without any extra details
#[arg(long = "name-only", default_value_t = false)]
name_only: bool,

#[arg(long, value_enum, default_value_t=SearchMode::None)]
mode: SearchMode,

/// The package(s) to get information for
#[arg(required = true)]
query: Vec<String>,
},
/// Update the APT cache on the system
Update {
#[command(flatten)]
mpr_url: MprURL,
},
/// Upgrade the packages on the system
Upgrade {
#[command(flatten)]
mpr_url: MprURL,

#[arg(long, value_enum, default_value_t=UpgradeMode::Both)]
mode: UpgradeMode,
},
/// Show the currently authenticated user
Whoami {
#[command(flatten)]
mpr_url: MprURL,

#[command(flatten)]
mpr_token: MprToken,
},
}

#[derive(Clone, Debug, ValueEnum)]
pub enum UpgradeMode {
/// Upgrade both APT and MPR packages
Both,
/// Only upgrade APT packages
AptOnly,
/// Only upgrade MPR packages
MprOnly,
}

#[derive(Clone, Debug, ValueEnum)]
pub enum Paging {
Auto,
Always,
Never,
}

#[derive(Args)]
pub struct MprURL {
/// URL to access the MPR from
#[arg(
env = "MPR_URL",
default_value = "https://mpr.makedeb.org",
long = "mpr-url"
)]
pub url: String,
}

#[derive(Args)]
pub struct MprToken {
/// The API token to authenticate to the MPR with
#[arg(
env = "MPR_TOKEN",
required = true,
hide_env_values = true,
long = "token"
)]
pub token: String,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum SearchMode {
/// No filter applied
None,
/// Only packages available on the MPR
MprOnly,
/// Only packages available via APT
AptOnly,
/// Only installed packages
Installed,
}
4 changes: 1 addition & 3 deletions src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::{
};
use rust_apt::cache::Cache as AptCache;

pub fn clone(args: &clap::ArgMatches) {
let pkg: &String = args.get_one("pkg").unwrap();
let mpr_url: &String = args.get_one("mpr-url").unwrap();
pub fn clone(pkg: &String, mpr_url: &String) {
let cache = Cache::new(AptCache::new(), MprCache::new());
let mut pkgbases: Vec<&String> = Vec::new();

Expand Down
14 changes: 2 additions & 12 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ struct CommentResult {
link: String,
}

pub fn comment(args: &clap::ArgMatches) {
let pkg: &String = args.get_one("pkg").unwrap();
let mpr_url: &String = args.get_one("mpr-url").unwrap();
let api_token: &String = match args.get_one("token") {
Some(token) => token,
None => {
message::error("No API token was provided.\n");
quit::with_code(exitcode::USAGE);
}
};

pub fn comment(pkg: &String, message: &Option<String>, api_token: String, mpr_url: String) {
// Get a list of packages.
let mpr_cache = MprCache::new();
let mut pkgnames: Vec<&String> = Vec::new();
Expand All @@ -36,7 +26,7 @@ pub fn comment(args: &clap::ArgMatches) {

// Get the message.
// If no message was supplied, get one from the user.
let msg: String = match args.get_one::<String>("msg") {
let msg: String = match message {
Some(msg) => (msg).to_owned(),
None => {
// Get the editor.
Expand Down
10 changes: 4 additions & 6 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use crate::{
};
use rust_apt::cache::Cache as AptCache;

pub fn install(args: &clap::ArgMatches) {
let pkglist: Vec<&String> = args.get_many("pkg").unwrap().collect();
let mpr_url: &String = args.get_one("mpr-url").unwrap();
pub fn install(pkglist: &Vec<String>, mpr_url: String) {
let cache = Cache::new(AptCache::new(), MprCache::new());

// Package sources.
Expand All @@ -20,7 +18,7 @@ pub fn install(args: &clap::ArgMatches) {
// should just show those packages and abort.
let mut unfindable = false;

for pkg in &pkglist {
for pkg in pkglist {
if cache.get_apt_pkg(pkg).is_none() && cache.get_mpr_pkg(pkg).is_none() {
message::error(&format!(
"Unable to find package '{}'.\n",
Expand All @@ -34,7 +32,7 @@ pub fn install(args: &clap::ArgMatches) {
quit::with_code(exitcode::USAGE);
}

for pkg in &pkglist {
for pkg in pkglist {
let apt_pkg = cache.get_apt_pkg(pkg);
let mpr_pkg = cache.get_mpr_pkg(pkg);

Expand Down Expand Up @@ -80,5 +78,5 @@ pub fn install(args: &clap::ArgMatches) {
quit::with_code(exitcode::UNAVAILABLE);
}

cache.commit(&mpr_install_order, mpr_url);
cache.commit(&mpr_install_order, &mpr_url);
}
Loading