clean main: divide clap config into multiple files, broke clone 70 lines width

This commit is contained in:
grimhilt
2023-10-28 23:46:12 +02:00
parent fc8e976c9c
commit 7719e27fe8
13 changed files with 277 additions and 188 deletions

38
src/subcommands/add.rs Normal file
View File

@@ -0,0 +1,38 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::commands;
use crate::commands::add::AddArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("add")
.arg(
Arg::with_name("files")
.required(true)
.conflicts_with("all")
.multiple(true)
.takes_value(true)
.value_name("FILE")
.help("Files to add"),
)
.arg(
Arg::with_name("force")
.short("f")
.long("force")
.help("Allow adding otherwise ignored files."),
)
.arg(
Arg::with_name("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<'_>) {
commands::add::add(AddArgs {
files: args.values_of("files"),
force: args.is_present("force"),
all: args.is_present("all"),
});
}

59
src/subcommands/clone.rs Normal file
View File

@@ -0,0 +1,59 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use std::borrow::Cow;
use textwrap::{fill, Options};
use crate::commands::clone::{self, CloneArgs};
use crate::global;
use crate::commands;
fn sized_str<'a>(content: &'a str) -> &'a str {
fill(content, Options::new(70).width).as_str();
"ok"
}
fn test() -> String {
String::from("ok")
}
pub fn create() -> App<'static, 'static> {
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")
.arg(
Arg::with_name("remote")
.required(true)
.takes_value(true)
.value_name("REMOTE")
//.help(_desc)
)
.arg(
Arg::with_name("depth")
.short("d")
.long("depth")
.required(false)
.takes_value(true)
//.help(&depth_desc)
)
.arg(
Arg::with_name("directory")
.required(false)
.takes_value(true)
.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()));
}
if let Some(remote) = args.values_of("remote") {
commands::clone::clone(CloneArgs {
remote,
depth: args.values_of("depth").map(
|mut val| val.next().unwrap().to_owned()
),
});
}
}

29
src/subcommands/config.rs Normal file
View File

@@ -0,0 +1,29 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("config")
.arg(
Arg::with_name("variable")
.required(true)
.takes_value(true)
.value_name("VARIABLE")
)
.arg(
Arg::with_name("value")
.required(true)
.takes_value(true)
.value_name("VALUE")
)
}
pub fn handler(args: &ArgMatches<'_>) {
if let Some(mut var) = args.values_of("variable") {
if let Some(mut val) = args.values_of("value") {
if commands::config::set(var.next().unwrap(), val.next().unwrap()).is_err() {
eprintln!("fatal: cannot save the value");
}
}
}
}

23
src/subcommands/init.rs Normal file
View File

@@ -0,0 +1,23 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("init")
.arg(
Arg::with_name("directory")
.required(false)
.takes_value(true)
.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()));
}
commands::init::init();
}

23
src/subcommands/pull.rs Normal file
View File

@@ -0,0 +1,23 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("pull")
.arg(
Arg::with_name("path")
.required(false)
.takes_value(true)
.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()));
}
commands::pull::pull();
}

6
src/subcommands/push.rs Normal file
View File

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

View File

@@ -0,0 +1,24 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::global;
use crate::commands;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("remote-diff")
.arg(
Arg::with_name("path")
.required(false)
.takes_value(true)
.value_name("PATH")
.help("The path to pull."),
)
.about("Fetch 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()));
}
commands::remote_diff::remote_diff();
}

6
src/subcommands/reset.rs Normal file
View File

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

30
src/subcommands/status.rs Normal file
View File

@@ -0,0 +1,30 @@
use clap::{App, Arg, SubCommand, ArgMatches};
use crate::global;
use crate::commands;
use crate::commands::status::StatusArgs;
pub fn create() -> App<'static, 'static> {
SubCommand::with_name("status")
.arg(
Arg::with_name("directory")
.required(false)
.takes_value(true)
.value_name("DIRECTORY")
)
.arg(
Arg::with_name("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()));
}
commands::status::status(StatusArgs {
nostyle: args.is_present("nostyle"),
});
}