feat: made all repo fields options

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-07-24 06:52:46 +02:00
parent 8de0fa8eae
commit ad569e23ba
Signed by: cafkafk
GPG key ID: CDDC792F655251ED
2 changed files with 87 additions and 36 deletions

View file

@ -106,9 +106,10 @@ pub struct Link {
/// Holds a single git repository and related fields. /// Holds a single git repository and related fields.
#[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct Repo { pub struct Repo {
pub name: String, pub name: Option<String>,
pub path: String, pub path: Option<String>,
pub url: String, pub url: Option<String>,
// TODO: make default a standard GitRepo
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<RepoKinds>, // FIXME: not implemented pub kind: Option<RepoKinds>, // FIXME: not implemented
pub flags: Option<Vec<RepoFlags>>, pub flags: Option<Vec<RepoFlags>>,
@ -183,17 +184,20 @@ impl Repo {
.expect("failed to unwrap flags") .expect("failed to unwrap flags")
.contains(&RepoFlags::Clone) .contains(&RepoFlags::Clone)
{ {
// TODO: check if &self.name already exists in dir // TODO: check if &self.name.as_ref() already exists in dir
let output = Command::new("git") let output = Command::new("git")
.current_dir(&self.path) .current_dir(&self.path.as_ref().unwrap())
.arg("clone") .arg("clone")
.arg(&self.url) .arg(&self.url.as_ref().unwrap())
.arg(&self.name) .arg(&self.name.as_ref().unwrap())
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to clone: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to clone: {:?}", &self,));
output.status.success() output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!(
"{} has clone set to false, not cloned",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -207,13 +211,20 @@ impl Repo {
.any(|s| s == &RepoFlags::Pull || s == &RepoFlags::Fast) .any(|s| s == &RepoFlags::Pull || s == &RepoFlags::Fast)
{ {
let output = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!(
"{}{}",
&self.path.as_ref().unwrap(),
&self.name.as_ref().unwrap()
))
.arg("pull") .arg("pull")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,));
output.status.success() output.status.success()
} else { } else {
info!("{} has clone set to false, not pulled", &self.name); info!(
"{} has clone set to false, not pulled",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -227,14 +238,21 @@ impl Repo {
.any(|s| s == &RepoFlags::Add || s == &RepoFlags::Quick || s == &RepoFlags::Fast) .any(|s| s == &RepoFlags::Add || s == &RepoFlags::Quick || s == &RepoFlags::Fast)
{ {
let output = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!(
"{}{}",
&self.path.as_ref().unwrap(),
&self.name.as_ref().unwrap()
))
.arg("add") .arg("add")
.arg(".") .arg(".")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,));
output.status.success() output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!(
"{} has clone set to false, not cloned",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -255,13 +273,20 @@ impl Repo {
.any(|s| s == &RepoFlags::Commit || s == &RepoFlags::Quick || s == &RepoFlags::Fast) .any(|s| s == &RepoFlags::Commit || s == &RepoFlags::Quick || s == &RepoFlags::Fast)
{ {
let status = Command::new("git") let status = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!(
"{}{}",
&self.path.as_ref().unwrap(),
&self.name.as_ref().unwrap()
))
.arg("commit") .arg("commit")
.status() .status()
.unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,));
status.success() status.success()
} else { } else {
info!("{} has push set to false, not cloned", &self.name); info!(
"{} has push set to false, not cloned",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -275,7 +300,11 @@ impl Repo {
.any(|s| s == &RepoFlags::Commit || s == &RepoFlags::Quick || s == &RepoFlags::Fast) .any(|s| s == &RepoFlags::Commit || s == &RepoFlags::Quick || s == &RepoFlags::Fast)
{ {
let output = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!(
"{}{}",
&self.path.as_ref().unwrap(),
&self.name.as_ref().unwrap()
))
.arg("commit") .arg("commit")
.arg("-m") .arg("-m")
.arg(msg) .arg(msg)
@ -283,7 +312,10 @@ impl Repo {
.unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,));
output.status.success() output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!(
"{} has clone set to false, not cloned",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -297,13 +329,20 @@ impl Repo {
.any(|s| s == &RepoFlags::Push || s == &RepoFlags::Quick || s == &RepoFlags::Fast) .any(|s| s == &RepoFlags::Push || s == &RepoFlags::Quick || s == &RepoFlags::Fast)
{ {
let output = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!(
"{}{}",
&self.path.as_ref().unwrap(),
&self.name.as_ref().unwrap()
))
.arg("push") .arg("push")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to push: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to push: {:?}", &self,));
output.status.success() output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!(
"{} has clone set to false, not cloned",
&self.name.as_ref().unwrap()
);
false false
} }
} }
@ -313,9 +352,12 @@ impl Repo {
fn remove() -> Result<(), std::io::Error> { fn remove() -> Result<(), std::io::Error> {
// https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html // https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html
unimplemented!("This seems to easy to missuse/exploit"); unimplemented!("This seems to easy to missuse/exploit");
// fs::remove_dir_all(format!("{}{}", &self.path, &self.name)) // fs::remove_dir_all(format!("{}{}", &self.path.as_ref(), &self.name.as_ref()))
} }
fn check_is_valid_GitRepo(&self) -> bool { fn check_is_valid_GitRepo(&self) -> bool {
// pub name: String,
// pub path: String,
// pub url: String,
todo!(); todo!();
} }
fn check_is_valid_GitHubRepo(&self) -> bool { fn check_is_valid_GitHubRepo(&self) -> bool {
@ -448,11 +490,11 @@ impl Config {
// for category in self.categories.values() { // for category in self.categories.values() {
// for (_, repo) in category.repos.as_ref().expect("failed to get repos").iter() { // for (_, repo) in category.repos.as_ref().expect("failed to get repos").iter() {
// if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { // if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) {
// let mut sp = Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name, op)); // let mut sp = Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name.as_ref(), op));
// if f(repo) { // if f(repo) {
// sp.stop_and_persist(success_str(), format!("{}: {}", repo.name, op)); // sp.stop_and_persist(success_str(), format!("{}: {}", repo.name.as_ref(), op));
// } else { // } else {
// sp.stop_and_persist(failure_str(), format!("{}: {}", repo.name, op)); // sp.stop_and_persist(failure_str(), format!("{}: {}", repo.name.as_ref(), op));
// } // }
// } else { // } else {
// f(repo); // f(repo);
@ -470,17 +512,19 @@ impl Config {
Some(repos) => { Some(repos) => {
for repo in repos.values() { for repo in repos.values() {
if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) {
let mut sp = let mut sp = Spinner::new(
Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name, op)); Spinners::Dots10,
format!("{}: {}", repo.name.as_ref().unwrap(), op),
);
if f(repo) { if f(repo) {
sp.stop_and_persist( sp.stop_and_persist(
success_str(), success_str(),
format!("{}: {}", repo.name, op), format!("{}: {}", repo.name.as_ref().unwrap(), op),
); );
} else { } else {
sp.stop_and_persist( sp.stop_and_persist(
failure_str(), failure_str(),
format!("{}: {}", repo.name, op), format!("{}: {}", repo.name.as_ref().unwrap(), op),
); );
} }
} else { } else {
@ -567,17 +611,17 @@ impl Config {
if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) {
let mut sp = Spinner::new( let mut sp = Spinner::new(
Spinners::Dots10, Spinners::Dots10,
format!("{}: {}", repo.name, op), format!("{}: {}", repo.name.as_ref().unwrap(), op),
); );
if f(repo) { if f(repo) {
sp.stop_and_persist( sp.stop_and_persist(
success_str(), success_str(),
format!("{}: {}", repo.name, op), format!("{}: {}", repo.name.as_ref().unwrap(), op),
); );
} else { } else {
sp.stop_and_persist( sp.stop_and_persist(
failure_str(), failure_str(),
format!("{}: {}", repo.name, op), format!("{}: {}", repo.name.as_ref().unwrap(), op),
); );
if break_on_err { if break_on_err {
break; break;

View file

@ -146,7 +146,11 @@ fn main() {
Some(Commands::Jump(cmd)) => match cmd { Some(Commands::Jump(cmd)) => match cmd {
JumpCommands::Repo { category, name } => { JumpCommands::Repo { category, name } => {
config.get_repo(category, name, |repo| { config.get_repo(category, name, |repo| {
println!("{}{}", repo.path, repo.name); println!(
"{}{}",
repo.path.as_ref().unwrap(),
repo.name.as_ref().unwrap()
);
}); });
} }
JumpCommands::Link { category, name } => { JumpCommands::Link { category, name } => {
@ -198,9 +202,9 @@ mod config {
.insert( .insert(
format!("{}", i).to_string(), format!("{}", i).to_string(),
Repo { Repo {
name: "test repo".to_string(), name: Some("test repo".to_string()),
path: "/tmp".to_string(), path: Some("/tmp".to_string()),
url: "https://github.com/cafkafk/gg".to_string(), url: Some("https://github.com/cafkafk/gg".to_string()),
flags: Some(vec![Clone, Push]), flags: Some(vec![Clone, Push]),
kind: None, kind: None,
}, },
@ -258,9 +262,12 @@ mod config {
#[allow(clippy::bool_assert_comparison)] #[allow(clippy::bool_assert_comparison)]
{ {
(&config).get_repo("config", "qmk_firmware", |repo| { (&config).get_repo("config", "qmk_firmware", |repo| {
assert_eq!(repo.name, "qmk_firmware"); assert_eq!(repo.name.as_ref().unwrap(), "qmk_firmware");
assert_eq!(repo.path, "/home/ces/org/src/git/"); assert_eq!(repo.path.as_ref().unwrap(), "/home/ces/org/src/git/");
assert_eq!(repo.url, "git@github.com:cafkafk/qmk_firmware.git"); assert_eq!(
repo.url.as_ref().unwrap(),
"git@github.com:cafkafk/qmk_firmware.git"
);
}); });
(&config).get_link("stuff", "gg", |link| { (&config).get_link("stuff", "gg", |link| {
assert_eq!(link.name, "gg"); assert_eq!(link.name, "gg");