feat(config): create a proper config file with proper settings manipulation

This commit is contained in:
lol 2024-02-20 21:25:21 +01:00
parent 628c26ef81
commit cc916c0ed6
13 changed files with 328 additions and 142 deletions

205
Cargo.lock generated
View File

@ -64,6 +64,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.13.0" version = "3.13.0"
@ -111,7 +117,7 @@ checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"atty", "atty",
"bitflags", "bitflags 1.3.2",
"strsim", "strsim",
"textwrap 0.11.0", "textwrap 0.11.0",
"unicode-width", "unicode-width",
@ -181,33 +187,19 @@ dependencies = [
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.1" version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [ dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc", "libc",
"windows-sys 0.52.0",
] ]
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.9.0" version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
dependencies = [
"instant",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
@ -312,6 +304,17 @@ version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
[[package]]
name = "getrandom"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]] [[package]]
name = "glob" name = "glob"
version = "0.3.1" version = "0.3.1"
@ -361,12 +364,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.9" version = "0.2.9"
@ -503,17 +500,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
dependencies = [
"hermit-abi 0.3.1",
"libc",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.7.2" version = "2.7.2"
@ -543,15 +529,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.144" version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.3.8" version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -639,10 +625,12 @@ dependencies = [
"indicatif", "indicatif",
"lazy_static", "lazy_static",
"md5", "md5",
"rand 0.8.5",
"regex", "regex",
"reqwest", "reqwest",
"rpassword", "rpassword",
"rust-crypto", "rust-crypto",
"tempfile",
"textwrap 0.13.4", "textwrap 0.13.4",
"tokio", "tokio",
"xml-rs", "xml-rs",
@ -685,7 +673,7 @@ version = "0.10.53"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12df40a956736488b7b44fe79fe12d4f245bb5b3f5a1f6095e499760015be392" checksum = "12df40a956736488b7b44fe79fe12d4f245bb5b3f5a1f6095e499760015be392"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
"cfg-if", "cfg-if",
"foreign-types", "foreign-types",
"libc", "libc",
@ -741,7 +729,7 @@ checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"redox_syscall 0.2.16", "redox_syscall",
"smallvec", "smallvec",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -776,6 +764,12 @@ version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "767eb9f07d4a5ebcb39bbf2d452058a93c011373abf6832e24194a1c3f004794" checksum = "767eb9f07d4a5ebcb39bbf2d452058a93c011373abf6832e24194a1c3f004794"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.66" version = "1.0.66"
@ -817,6 +811,27 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
]
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.3.1" version = "0.3.1"
@ -832,6 +847,15 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "rdrand" name = "rdrand"
version = "0.4.0" version = "0.4.0"
@ -847,16 +871,7 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags",
] ]
[[package]] [[package]]
@ -958,16 +973,15 @@ checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.37.19" version = "0.38.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949"
dependencies = [ dependencies = [
"bitflags", "bitflags 2.4.2",
"errno", "errno",
"io-lifetimes",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -997,7 +1011,7 @@ version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8" checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
"core-foundation", "core-foundation",
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -1102,15 +1116,14 @@ dependencies = [
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.5.0" version = "3.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
"redox_syscall 0.3.5",
"rustix", "rustix",
"windows-sys 0.45.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1484,6 +1497,15 @@ dependencies = [
"windows-targets 0.48.0", "windows-targets 0.48.0",
] ]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.42.2" version = "0.42.2"
@ -1514,6 +1536,21 @@ dependencies = [
"windows_x86_64_msvc 0.48.0", "windows_x86_64_msvc 0.48.0",
] ]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.42.2" version = "0.42.2"
@ -1526,6 +1563,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.42.2" version = "0.42.2"
@ -1538,6 +1581,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.42.2" version = "0.42.2"
@ -1550,6 +1599,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.42.2" version = "0.42.2"
@ -1562,6 +1617,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.42.2" version = "0.42.2"
@ -1574,6 +1635,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.2" version = "0.42.2"
@ -1586,6 +1653,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.42.2" version = "0.42.2"
@ -1598,6 +1671,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.10.1" version = "0.10.1"

View File

@ -22,6 +22,8 @@ indicatif = "0.17.5"
md5 = "0.7.0" md5 = "0.7.0"
futures-util = "0.3.28" futures-util = "0.3.28"
rpassword = "7.2" rpassword = "7.2"
rand = "0.8"
tempfile = "3.10.0"
[profile.release] [profile.release]
debug = true debug = true

View File

@ -8,3 +8,4 @@ pub mod config;
pub mod remote_diff; pub mod remote_diff;
pub mod remote; pub mod remote;
pub mod pull; pub mod pull;
pub mod credential;

View File

@ -1,6 +1,121 @@
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::{self, Write}; use clap::Values;
use std::io::{self, Write, BufRead, Seek, SeekFrom};
use crate::utils::{path, read}; use crate::utils::{path, read};
use tempfile::tempfile;
use std::collections::HashMap;
pub struct ConfigSetArgs<'a> {
pub name: Option<Values<'a>>,
pub value: Option<Values<'a>>,
}
pub fn config_set(args: ConfigSetArgs) {
// configure possible options and their associated category
let mut option_categories: HashMap<&str, &str> = HashMap::new();
option_categories.insert("force_insecure", "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);
if category.is_none() {
eprintln!("fatal: '{}' is not a valid option.", name);
std::process::exit(1);
}
write_option_in_cat(category.unwrap(), name, value);
}
fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
let mut root = path::nextsync();
root.push("config");
let mut in_target_category = false;
if let Ok(mut lines) = read::read_lines(root) {
for line in lines {
if let Ok(line) = line {
let trimmed_line = line.trim();
if trimmed_line.starts_with('[') && trimmed_line.ends_with(']') {
in_target_category = trimmed_line == format!("[{}]", category);
} else if in_target_category {
let parts: Vec<&str> = trimmed_line.splitn(2, '=').collect();
if parts.len() == 2 && parts[0].trim() == option {
return Some(parts[1].trim().to_string());
}
}
}
}
}
None
}
pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Result<()> {
let mut root = path::nextsync();
root.push("config");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&root)?;
let mut in_target_category = false;
let mut option_found = false;
// Go to the beginning of the file
file.seek(SeekFrom::Start(0))?;
// Create a temporary file to hold the modified content
let mut tmp_file = tempfile::Builder::new()
.prefix(".nextsyncconfig")
.tempfile()?;
let reader = io::BufReader::new(&file);
for line in reader.lines() {
let line = line?;
let trimmed_line = line.trim();
if trimmed_line.starts_with('[') && trimmed_line.ends_with(']') {
in_target_category = trimmed_line == format!("[{}]", category);
}
if in_target_category && !option_found && trimmed_line.starts_with(&format!("{} =", option)) {
// Option already exists, update its value
writeln!(&mut tmp_file, "\t{} = {}", option, value)?;
option_found = true;
} else {
// Write the original line
writeln!(&mut tmp_file, "{}", line)?;
}
}
if !option_found {
// If the option doesn't exist, add it to the category
writeln!(&mut tmp_file, "[{}]", category)?;
writeln!(&mut tmp_file, "\t{} = {}", option, value)?;
}
// Flush and sync the temporary file to ensure data is written to disk
tmp_file.flush()?;
// Go back to the beginning of the file
tmp_file.seek(SeekFrom::Start(0))?;
file.seek(SeekFrom::Start(0))?;
// Copy the contents of the temporary file to the original file
io::copy(&mut tmp_file, &mut file)?;
Ok(())
}
pub fn add_remote(name: &str, url: &str) -> io::Result<()> { pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
let mut root = path::nextsync(); let mut root = path::nextsync();
@ -27,32 +142,11 @@ pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
} }
pub fn get_remote(name: &str) -> Option<String> { pub fn get_remote(name: &str) -> Option<String> {
let mut root = path::nextsync(); find_option_in_cat(&format!("remote \"{}\"", name), "url")
root.push("config"); }
let target = String::from(format!("[remote \"{}\"]", name)); pub fn get_core(name: &str) -> Option<String> {
find_option_in_cat("core", name)
if let Ok(mut lines) = read::read_lines(root) {
while let Some(line_res) = lines.next() {
if let Ok(line) = line_res {
if line == target {
// get the first line starting with 'url = '
while let Some(line_res) = lines.next() {
if let Ok(line) = line_res {
let mut splitted_res = line.split("=");
if let Some(splitted) = splitted_res.next() {
if splitted == "\turl " {
return Some(splitted_res.next().unwrap().to_owned());
}
}
}
}
}
}
}
}
return None;
} }
pub fn add_core(name: &str, value: &str) -> io::Result<()> { pub fn add_core(name: &str, value: &str) -> io::Result<()> {
@ -72,25 +166,6 @@ pub fn add_core(name: &str, value: &str) -> io::Result<()> {
Ok(()) Ok(())
} }
pub fn set(var: &str, val: &str) -> io::Result<()> {
let mut root = path::nextsync();
root.push("config");
// todo check if exist
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.append(true)
.open(root)?;
let mut line = var.to_owned();
line.push_str(" ");
line.push_str(val);
writeln!(file, "{}", line)?;
Ok(())
}
pub fn get(var: &str) -> Option<String> { pub fn get(var: &str) -> Option<String> {
let mut root = path::nextsync(); let mut root = path::nextsync();
root.push("config"); root.push("config");
@ -98,7 +173,7 @@ pub fn get(var: &str) -> Option<String> {
if let Ok(lines) = read::read_lines(root) { if let Ok(lines) = read::read_lines(root) {
for line in lines { for line in lines {
if let Ok(l) = line { if let Ok(l) = line {
if l.starts_with(var.clone()) { if l.starts_with(var) {
let (_, val) = l.split_once(" ").unwrap(); let (_, val) = l.split_once(" ").unwrap();
return Some(val.to_owned()); return Some(val.to_owned());
} }
@ -107,3 +182,5 @@ pub fn get(var: &str) -> Option<String> {
} }
None None
} }

View File

@ -16,7 +16,7 @@ pub mod copied;
pub fn push() { pub fn push() {
// todo err when pushing new folder // todo err when pushing new folder
let _remote = match config::get("remote") { let _remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: no remote set in configuration"); eprintln!("fatal: no remote set in configuration");

View File

@ -42,12 +42,19 @@ impl ApiBuilder {
} }
pub fn set_url(&mut self, method: Method, url: &str) -> &mut ApiBuilder { pub fn set_url(&mut self, method: Method, url: &str) -> &mut ApiBuilder {
self.request = Some(self.client.request(method, url)); let mut new_url = url.to_owned();
if let Some(active) = config::get_core("force_insecure") {
if active == "true" {
new_url = url.replace("https", "http");
}
}
self.request = Some(self.client.request(method, new_url));
self self
} }
pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder { pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder {
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: unable to find a remote"); eprintln!("fatal: unable to find a remote");
@ -64,8 +71,8 @@ impl ApiBuilder {
if path != "/" { if path != "/" {
url.push_str(path); url.push_str(path);
} }
self.request = Some(self.client.request(method, url));
self self.set_url(method, &url)
} }
pub fn set_req(&mut self, meth: Method, p: &str, api_props: &ApiProps) -> &mut ApiBuilder { pub fn set_req(&mut self, meth: Method, p: &str, api_props: &ApiProps) -> &mut ApiBuilder {
@ -79,8 +86,8 @@ impl ApiBuilder {
if p != "/" { if p != "/" {
url.push_str(p); url.push_str(p);
} }
self.request = Some(self.client.request(meth, url));
self self.set_url(meth, &url)
} }
pub fn set_basic_auth(&mut self, login: String, pwd: String) -> &mut ApiBuilder { pub fn set_basic_auth(&mut self, login: String, pwd: String) -> &mut ApiBuilder {

View File

@ -24,7 +24,7 @@ impl Copy {
pub fn set_url_copy(&mut self, url: &str, destination: &str) -> &mut Copy { pub fn set_url_copy(&mut self, url: &str, destination: &str) -> &mut Copy {
self.api_builder.build_request(Method::from_bytes(b"COPY").unwrap(), url); self.api_builder.build_request(Method::from_bytes(b"COPY").unwrap(), url);
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: unable to find a remote"); eprintln!("fatal: unable to find a remote");

View File

@ -25,7 +25,7 @@ impl Move {
pub fn set_url_move(&mut self, url: &str, destination: &str) -> &mut Move { pub fn set_url_move(&mut self, url: &str, destination: &str) -> &mut Move {
self.api_builder.build_request(Method::from_bytes(b"MOVE").unwrap(), url); self.api_builder.build_request(Method::from_bytes(b"MOVE").unwrap(), url);
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: unable to find a remote"); eprintln!("fatal: unable to find a remote");

View File

@ -67,7 +67,7 @@ impl ApiCall for ReqProps {
} }
fn set_url(&mut self, url: &str) -> &mut ReqProps { fn set_url(&mut self, url: &str) -> &mut ReqProps {
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: unable to find a remote"); eprintln!("fatal: unable to find a remote");

View File

@ -39,7 +39,7 @@ impl RequestManager {
{ {
if self.host.is_none() if self.host.is_none()
{ {
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
// todo ask user instead // todo ask user instead

View File

@ -1,29 +1,53 @@
use clap::{App, Arg, SubCommand, ArgMatches}; use clap::{App, Arg, SubCommand, ArgMatches};
use crate::commands::config::ConfigSetArgs;
use crate::commands; use crate::commands;
pub fn create() -> App<'static, 'static> { pub fn create() -> App<'static, 'static> {
SubCommand::with_name("config") SubCommand::with_name("config")
.arg( .about("Get and set repository or global options")
Arg::with_name("variable") .subcommand(
.required(true) SubCommand::with_name("get")
.takes_value(true) .about("Get the value of a configuration variable")
.value_name("VARIABLE") .arg(
Arg::with_name("name")
.help("The name of the configuration variable")
.required(true)
.index(1)
)
) )
.arg( .subcommand(
Arg::with_name("value") SubCommand::with_name("set")
.required(true) .about("Set a configuration variable")
.takes_value(true) .arg(
.value_name("VALUE") Arg::with_name("name")
.help("The name of the configuration variable")
.required(true)
.index(1)
)
.arg(
Arg::with_name("value")
.help("The value to set")
.required(true)
.index(2)
)
) )
} }
pub fn handler(args: &ArgMatches<'_>) { pub fn handler(args: &ArgMatches<'_>) {
if let Some(mut var) = args.values_of("variable") {
if let Some(mut val) = args.values_of("value") { match args.subcommand() {
if commands::config::set(var.next().unwrap(), val.next().unwrap()).is_err() { ("set", Some(set_matches)) => {
eprintln!("fatal: cannot save the value"); commands::config::config_set(ConfigSetArgs {
} name: set_matches.values_of("name"),
value: set_matches.values_of("value"),
});
} }
_ => 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

@ -11,17 +11,13 @@ pub fn create() -> App<'static, 'static> {
.arg( .arg(
Arg::with_name("name") Arg::with_name("name")
.required(true) .required(true)
.multiple(false) .index(1)
.takes_value(true)
.value_name("NAME")
.help("The name of the remote"), .help("The name of the remote"),
) )
.arg( .arg(
Arg::with_name("url") Arg::with_name("url")
.required(true) .required(true)
.multiple(false) .index(2)
.takes_value(true)
.value_name("URL")
.help("The url of the remote"), .help("The url of the remote"),
) )
.about("Add a new remote to this repository") .about("Add a new remote to this repository")

View File

@ -18,7 +18,7 @@ impl Clone for ApiProps {
} }
pub fn get_api_props() -> ApiProps { pub fn get_api_props() -> ApiProps {
let remote = match config::get("remote") { let remote = match config::get_remote("origin") {
Some(r) => r, Some(r) => r,
None => { None => {
eprintln!("fatal: unable to find a remote"); eprintln!("fatal: unable to find a remote");