chore: update clap

This commit is contained in:
grimhilt
2024-03-31 22:17:26 +02:00
parent 1aa02a24af
commit 3420634bea
20 changed files with 241 additions and 240 deletions

View File

@@ -1,6 +1,5 @@
use std::io::Write;
use std::path::{Path, PathBuf};
use clap::Values;
use glob::glob;
use crate::store::{self, object::Object};
use crate::utils::{self, path};
@@ -8,8 +7,8 @@ use crate::store::object::object::{Obj, ObjMethods};
use crate::utils::nextsyncignore::{self, ignore_file};
use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string};
pub struct AddArgs<'a> {
pub files: Option<Values<'a>>,
pub struct AddArgs {
pub files: Vec<String>,
pub force: bool,
pub all: bool,
}
@@ -18,13 +17,13 @@ pub struct AddArgs<'a> {
pub fn add(args: AddArgs) {
let mut pattern: String;
let file_vec: Vec<&str> = match args.all {
let file_vec: Vec<String> = match args.all {
true => {
pattern = path_buf_to_string(repo_root());
pattern.push_str("/*");
vec![&pattern]
vec![pattern]
},
false => args.files.unwrap().collect(),
false => args.files,
};
let mut added_files: Vec<String> = vec![];
@@ -32,8 +31,7 @@ pub fn add(args: AddArgs) {
let rules = nextsyncignore::get_rules();
for file in file_vec {
dbg!(&file);
let f = match normalize_relative(file) {
let f = match normalize_relative(&file) {
Ok(f) => f,
Err(err) => {
eprintln!("err: {} {}", file, err);

View File

@@ -2,7 +2,6 @@ use std::io;
use std::io::prelude::*;
use std::fs::DirBuilder;
use std::path::{Path, PathBuf};
use clap::Values;
use regex::Regex;
use crate::services::downloader::Downloader;
use crate::utils::api::ApiProps;
@@ -18,16 +17,16 @@ use crate::commands::init;
pub const DEPTH: &str = "3";
pub struct CloneArgs<'a> {
pub remote: Values<'a>,
pub struct CloneArgs {
pub remote: String,
pub depth: Option<String>,
}
pub fn clone(args: CloneArgs) {
let d = DIR_PATH.lock().unwrap().clone();
let url = args.remote.clone().next().unwrap();
let (host, tmp_user, dist_path_str) = get_url_props(url);
let url = args.remote.clone();
let (host, tmp_user, dist_path_str) = get_url_props(&url);
let username = match tmp_user {
Some(u) => u.to_string(),
None => {

View File

@@ -1,12 +1,11 @@
use std::fs::OpenOptions;
use clap::Values;
use std::io::{self, Write, BufRead, Seek, SeekFrom};
use crate::utils::{path, read};
use std::collections::HashMap;
pub struct ConfigSetArgs<'a> {
pub name: Option<Values<'a>>,
pub value: Option<Values<'a>>,
pub struct ConfigSetArgs {
pub name: String,
pub value: String,
}
pub fn config_set(args: ConfigSetArgs) {
@@ -15,17 +14,14 @@ pub fn config_set(args: ConfigSetArgs) {
option_categories.insert("force_insecure", "core");
option_categories.insert("token", "core");
let name = args.name.unwrap().next().unwrap();
let value = args.value.unwrap().next().unwrap();
// get category of option
let category = option_categories.get(name);
let category = option_categories.get(args.name.as_str());
if category.is_none() {
eprintln!("fatal: '{}' is not a valid option.", name);
eprintln!("fatal: '{}' is not a valid option.", args.name.clone());
std::process::exit(1);
}
let _ = write_option_in_cat(category.unwrap(), name, value);
let _ = write_option_in_cat(category.unwrap(), &args.name, &args.value);
}

View File

@@ -1,4 +1,3 @@
use clap::Values;
use crate::commands::clone::get_url_props;
use crate::services::api::ApiError::RequestError;
@@ -6,9 +5,9 @@ use crate::services::login::Login;
use crate::services::api_call::ApiCall;
use crate::commands::config;
pub struct CredentialArgs<'a> {
pub username: Option<Values<'a>>,
pub password: Option<Values<'a>>,
pub struct CredentialArgs {
pub username: String,
pub password: Option<String>,
}
pub fn credential_add(args: CredentialArgs) {
@@ -23,9 +22,9 @@ pub fn credential_add(args: CredentialArgs) {
let (host, _, _) = get_url_props(&remote);
// get username and password
let username = args.username.unwrap().next().unwrap();
let username = args.username.to_owned();
let password = match args.password {
Some(mut pwd) => pwd.next().unwrap().to_owned(),
Some(mut pwd) => pwd.to_owned(),
None => {
println!("Please enter the password for {}: ", username);
rpassword::read_password().unwrap()
@@ -34,7 +33,7 @@ pub fn credential_add(args: CredentialArgs) {
// get token
let get_token = Login::new()
.set_auth(username, &password)
.set_auth(&username, &password)
.set_host(Some(host))
.send_login();

View File

@@ -1,7 +1,6 @@
use std::env;
use std::fs::{DirBuilder, File};
use std::path::PathBuf;
use crate::utils::read::read_folder;
use crate::global::global::DIR_PATH;
pub fn init() {

View File

@@ -1,24 +1,14 @@
use clap::Values;
use crate::commands::config;
use super::config::get_all_remote;
pub struct RemoteArgs<'a> {
pub name: Option<Values<'a>>,
pub url: Option<Values<'a>>,
pub struct RemoteArgs {
pub name: String,
pub url: String,
}
pub fn remote_add(args: RemoteArgs) {
if args.name.is_none() || args.url.is_none() {
eprintln!("Missing argument: remote add command need a name and an url");
return;
}
let name = args.name.unwrap().next().unwrap();
let url = args.url.unwrap().next().unwrap();
let _ = config::add_remote(name, url);
let _ = config::add_remote(&args.name, &args.url);
}
pub fn remote_list(verbose: bool) {

View File

@@ -1,4 +1,4 @@
use clap::{App, SubCommand};
use clap::Command;
mod subcommands;
@@ -9,42 +9,41 @@ mod global;
mod store;
fn main() {
let app = App::new("Nextsync")
let app = Command::new("Nextsync")
.version("1.0")
.author("grimhilt")
.about("A git-line command line tool to interact with nextcloud")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(subcommands::clone::create())
.subcommand(subcommands::init::create())
.subcommand(subcommands::status::create())
.subcommand(subcommands::add::create())
.subcommand(subcommands::push::create())
.subcommand(subcommands::reset::create())
.subcommand(subcommands::remote::create())
.subcommand(subcommands::config::create())
.subcommand(subcommands::remote_diff::create())
.subcommand(subcommands::pull::create())
.subcommand(subcommands::credential::create())
.subcommand(
SubCommand::with_name("test")
);
.subcommands([
subcommands::clone::create(),
subcommands::init::create(),
subcommands::status::create(),
subcommands::add::create(),
subcommands::push::create(),
subcommands::reset::create(),
subcommands::remote::create(),
subcommands::config::create(),
subcommands::remote_diff::create(),
subcommands::pull::create(),
subcommands::credential::create(),
]);
// .setting(clap::AppSettings::SubcommandRequiredElseHelp);
let matches = app.get_matches();
match matches.subcommand() {
("init", Some(args)) => subcommands::init::handler(args),
("status", Some(args)) => subcommands::status::handler(args),
("add", Some(args)) => subcommands::add::handler(args),
("reset", Some(_)) => commands::reset::reset(),
("clone", Some(args)) => subcommands::clone::handler(args),
("push", Some(_)) => commands::push::push(),
("config", Some(args)) => subcommands::config::handler(args),
("remote-diff", Some(args)) => subcommands::remote_diff::handler(args),
("pull", Some(args)) => subcommands::pull::handler(args),
("remote", Some(args)) => subcommands::remote::handler(args),
("credential", Some(args)) => subcommands::credential::handler(args),
(_, _) => {},
Some(("init", args)) => subcommands::init::handler(args),
Some(("status", args)) => subcommands::status::handler(args),
Some(("add", args)) => subcommands::add::handler(args),
Some(("reset", _)) => commands::reset::reset(),
Some(("clone", args)) => subcommands::clone::handler(args),
Some(("push", _)) => commands::push::push(),
Some(("config", args)) => subcommands::config::handler(args),
Some(("remote-diff", args)) => subcommands::remote_diff::handler(args),
Some(("pull", args)) => subcommands::pull::handler(args),
Some(("remote", args)) => subcommands::remote::handler(args),
Some(("credential", args)) => subcommands::credential::handler(args),
Some((_, _)) => {},
None => {},
};
}

View File

@@ -1,38 +1,40 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, ArgMatches, Command};
use crate::commands;
use crate::commands::add::AddArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("add")
pub fn create() -> Command {
Command::new("add")
.arg(
Arg::with_name("files")
Arg::new("files")
.required(true)
.conflicts_with("all")
.multiple(true)
.takes_value(true)
.num_args(1..)
.value_name("FILE")
.help("Files to add"),
)
.arg(
Arg::with_name("force")
.short("f")
Arg::new("force")
.short('f')
.long("force")
.help("Allow adding otherwise ignored files."),
)
.arg(
Arg::with_name("all")
.short("A")
Arg::new("all")
.short('A')
.long("all")
.help("This adds, modifies, and removes index entries to match the working tree"),
)
.about("Add changes to the index")
}
pub fn handler(args: &ArgMatches<'_>) {
pub fn handler(args: &ArgMatches) {
commands::add::add(AddArgs {
files: args.values_of("files"),
force: args.is_present("force"),
all: args.is_present("all"),
files: match args.get_many::<String>("files") {
None => vec![],
Some(vals) => vals.map(|s| s.to_string()).collect(),
},
force: args.contains_id("force"),
all: args.contains_id("all"),
});
}

View File

@@ -1,4 +1,4 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
// use textwrap::{fill, Options};
use crate::commands::clone::CloneArgs;
@@ -10,45 +10,43 @@ use crate::commands;
// "ok"
// }
pub fn create() -> App<'static, 'static> {
pub fn create() -> Command {
// let remote_desc = sized_str(&format!("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories."));
// let depth_desc = sized_str(&format!("Depth of the recursive fetch of object properties. This value should be lower when there are a lot of files per directory and higher when there are a lot of subdirectories with fewer files. (Default: {})", clone::DEPTH));
SubCommand::with_name("clone")
Command::new("clone")
.arg(
Arg::with_name("remote")
Arg::new("remote")
.required(true)
.takes_value(true)
.num_args(1)
.value_name("REMOTE")
//.help(_desc)
)
.arg(
Arg::with_name("depth")
.short("d")
Arg::new("depth")
.short('d')
.long("depth")
.required(false)
.takes_value(true)
.num_args(1)
//.help(&depth_desc)
)
.arg(
Arg::with_name("directory")
Arg::new("directory")
.required(false)
.takes_value(true)
.num_args(1)
.value_name("DIRECTORY")
)
.about("Clone a repository into a new directory")
.after_help("NEXTSYNC URLS\nThe following syntaxes may be used:\n\t- user@host.xz/path/to/repo\n\t- http[s]://host.xz/apps/files/?dir=/path/to/repo&fileid=111111\n\t- [http[s]://]host.xz/remote.php/dav/files/user/path/to/repo\n")
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(val) = args.values_of("directory") {
global::global::set_dir_path(String::from(val.clone().next().unwrap()));
pub fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("directory") {
global::global::set_dir_path(String::from(val.to_string()));
}
if let Some(remote) = args.values_of("remote") {
if let Some(remote) = args.get_one::<String>("remote") {
commands::clone::clone(CloneArgs {
remote,
depth: args.values_of("depth").map(
|mut val| val.next().unwrap().to_owned()
),
remote: remote.to_string(),
depth: args.get_one::<String>("depth").cloned(),
});
}
}

View File

@@ -1,32 +1,32 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::commands::config::ConfigSetArgs;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("config")
pub fn create() -> Command {
Command::new("config")
.about("Get and set repository or global options")
.subcommand(
SubCommand::with_name("get")
Command::new("get")
.about("Get the value of a configuration variable")
.arg(
Arg::with_name("name")
Arg::new("name")
.help("The name of the configuration variable")
.required(true)
.index(1)
)
)
.subcommand(
SubCommand::with_name("set")
Command::new("set")
.about("Set a configuration variable")
.arg(
Arg::with_name("name")
Arg::new("name")
.help("The name of the configuration variable")
.required(true)
.index(1)
)
.arg(
Arg::with_name("value")
Arg::new("value")
.help("The value to set")
.required(true)
.index(2)
@@ -34,20 +34,15 @@ pub fn create() -> App<'static, 'static> {
)
}
pub fn handler(args: &ArgMatches<'_>) {
pub fn handler(args: &ArgMatches) {
match args.subcommand() {
("set", Some(set_matches)) => {
Some(("set", set_matches)) => {
commands::config::config_set(ConfigSetArgs {
name: set_matches.values_of("name"),
value: set_matches.values_of("value"),
name: set_matches.get_one::<String>("name").unwrap().to_string(),
value: set_matches.get_one::<String>("value").unwrap().to_string(),
});
}
_ => println!("Invalid or missing subcommand for 'config'"),
}
// AddArgs {
// files: args.values_of("files"),
// force: args.is_present("force"),
// all: args.is_present("all"),
// });
}

View File

@@ -1,24 +1,24 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::commands;
use crate::commands::credential::CredentialArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("credential")
pub fn create() -> Command {
Command::new("credential")
.about("Manage set of credentials")
.subcommand(
SubCommand::with_name("add")
Command::new("add")
.arg(
Arg::with_name("username")
Arg::new("username")
.required(true)
.takes_value(true)
.num_args(1)
.value_name("NAME")
.help("The username used to connect to nextcloud"),
)
.arg(
Arg::with_name("password")
Arg::new("password")
.required(false)
.takes_value(true)
.num_args(1)
.value_name("PASSWORD")
.help("The passowd used to connect to nextcloud (optional)"),
)
@@ -26,12 +26,12 @@ pub fn create() -> App<'static, 'static> {
)
}
pub fn handler(args: &ArgMatches<'_>) {
pub fn handler(args: &ArgMatches) {
match args.subcommand() {
("add", Some(add_matches)) => {
Some(("add", add_matches)) => {
commands::credential::credential_add(CredentialArgs {
username: add_matches.values_of("username"),
password: add_matches.values_of("password"),
username: add_matches.get_one::<String>("username").unwrap().to_string(),
password: add_matches.get_one::<String>("password").cloned(),
});
}
_ => println!("Invalid or missing subcommand for 'credential'"),

View File

@@ -1,23 +1,23 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("init")
pub fn create() -> Command {
Command::new("init")
.arg(
Arg::with_name("directory")
Arg::new("directory")
.required(false)
.takes_value(true)
.num_args(1)
.value_name("DIRECTORY")
)
.about("Create an empty Nextsync repository")
// Create an empty nextsync repository or reinitialize an existing one
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(val) = args.values_of("directory") {
global::global::set_dir_path(String::from(val.clone().next().unwrap()));
pub fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("directory") {
global::global::set_dir_path(val.to_string());
}
commands::init::init();
}

View File

@@ -1,23 +1,23 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("pull")
pub fn create() -> Command {
Command::new("pull")
.arg(
Arg::with_name("path")
Arg::new("path")
.required(false)
.takes_value(true)
.num_args(1)
.value_name("PATH")
.help("The path to pull."),
)
.about("Fetch and integrate changes from the nextcloud server.")
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(val) = args.values_of("path") {
global::global::set_dir_path(String::from(val.clone().next().unwrap()));
pub fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("path") {
global::global::set_dir_path(val.to_string());
}
commands::pull::pull();
}

View File

@@ -1,6 +1,6 @@
use clap::{App, SubCommand};
use clap::Command;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("push")
pub fn create() -> Command {
Command::new("push")
.about("Push changes on nextcloud")
}

View File

@@ -1,21 +1,21 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::commands;
use crate::commands::remote::RemoteArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("remote")
pub fn create() -> Command {
Command::new("remote")
.about("Manage set of tracked repositories")
.subcommand(
SubCommand::with_name("add")
Command::new("add")
.arg(
Arg::with_name("name")
Arg::new("name")
.required(true)
.index(1)
.help("The name of the remote"),
)
.arg(
Arg::with_name("url")
Arg::new("url")
.required(true)
.index(2)
.help("The url of the remote"),
@@ -23,25 +23,23 @@ pub fn create() -> App<'static, 'static> {
.about("Add a new remote to this repository")
)
.arg(
Arg::with_name("verbose")
.short("v")
Arg::new("verbose")
.short('v')
.long("verbose")
.required(false)
.takes_value(false)
.help("Be a little more verbose and show remote url after name.")
)
}
pub fn handler(args: &ArgMatches<'_>) {
pub fn handler(args: &ArgMatches) {
match args.subcommand() {
("add", Some(add_matches)) => {
Some(("add", add_matches)) => {
commands::remote::remote_add(RemoteArgs {
name: add_matches.values_of("name"),
url: add_matches.values_of("url"),
name: add_matches.get_one::<String>("name").unwrap().to_string(),
url: add_matches.get_one::<String>("url").unwrap().to_string(),
});
}
_ => {
commands::remote::remote_list(args.is_present("verbose"));
commands::remote::remote_list(args.contains_id("verbose"));
}
}
}

View File

@@ -1,14 +1,14 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("remote-diff")
pub fn create() -> Command {
Command::new("remote-diff")
.arg(
Arg::with_name("path")
Arg::new("path")
.required(false)
.takes_value(true)
.num_args(1)
.value_name("PATH")
.help("The path to pull."),
)
@@ -16,9 +16,9 @@ pub fn create() -> App<'static, 'static> {
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(val) = args.values_of("path") {
global::global::set_dir_path(String::from(val.clone().next().unwrap()));
pub fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("path") {
global::global::set_dir_path(val.to_string());
}
commands::remote_diff::remote_diff();
}

View File

@@ -1,6 +1,6 @@
use clap::{App, SubCommand};
use clap::Command;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("reset")
pub fn create() -> Command {
Command::new("reset")
.about("Clear the index")
}

View File

@@ -1,30 +1,30 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use clap::{Arg, Command, ArgMatches};
use crate::global;
use crate::commands;
use crate::commands::status::StatusArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("status")
pub fn create() -> Command {
Command::new("status")
.arg(
Arg::with_name("directory")
.required(false)
.takes_value(true)
Arg::new("directory")
.num_args(1)
.value_name("DIRECTORY")
)
.arg(
Arg::with_name("nostyle")
Arg::new("nostyle")
.long("nostyle")
.help("Status with minium information and style"),
)
.about("Show the working tree status")
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(val) = args.values_of("directory") {
global::global::set_dir_path(String::from(val.clone().next().unwrap()));
pub fn handler(args: &ArgMatches) {
if let Some(val) = args.get_one::<String>("directory") {
global::global::set_dir_path(val.to_string());
}
commands::status::status(StatusArgs {
nostyle: args.is_present("nostyle"),
nostyle: args.contains_id("nostyle"),
});
}