From ff34012eff45bb8fc796b9a692af92778a6382c5 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sat, 26 Aug 2023 00:09:46 +0900 Subject: [PATCH] new: setup device for 2nd and subsequent --- src/main.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 859b145..9477d93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ struct Cli { enum Commands { /// Initialize Init { - repo: Option, // url? + repo_url: Option, // url? }, /// Print config dir. @@ -89,12 +89,18 @@ fn main() -> Result<(), String> { trace!("Config dir: {:?}", config_dir); match cli.command { - Commands::Init { repo } => { + Commands::Init { repo_url } => { + let is_first_device: bool; // get repo or initialize it - let repo = match repo { - Some(repo) => { - trace!("repo: {}", repo); - unimplemented!("Need to use git2"); // TODO + let repo_url = match repo_url { + Some(repo_url) => { + trace!("repo: {}", repo_url); + let repo = match Repository::clone(&repo_url, &config_dir) { + Ok(repo) => repo, + Err(e) => return Err(e.to_string()), + }; + is_first_device = false; + repo } None => { trace!("No repo provided"); @@ -122,6 +128,7 @@ fn main() -> Result<(), String> { Err(e) => return Err(e.to_string()), }; } + is_first_device = true; repo } }; @@ -142,8 +149,11 @@ fn main() -> Result<(), String> { // Add new device to devices.yml { - // let mut devices = get_devices(&config_dir)?; - let mut devices: Vec = vec![]; + let mut devices: Vec = if is_first_device { + vec![] + } else { + get_devices(&config_dir)? + }; devices.push(device.clone()); trace!("Devices: {:?}", devices); write_devices(&config_dir, devices)?; @@ -151,7 +161,7 @@ fn main() -> Result<(), String> { // commit match add_and_commit( - &repo, + &repo_url, &Path::new(DEVICESFILE), &format!("Add new devname: {}", &device.name), ) {