new: setup device for 2nd and subsequent

This commit is contained in:
qwjyh 2023-08-26 00:09:46 +09:00
parent 92b27b876b
commit ff34012eff

View file

@ -32,7 +32,7 @@ struct Cli {
enum Commands { enum Commands {
/// Initialize /// Initialize
Init { Init {
repo: Option<String>, // url? repo_url: Option<String>, // url?
}, },
/// Print config dir. /// Print config dir.
@ -89,12 +89,18 @@ fn main() -> Result<(), String> {
trace!("Config dir: {:?}", config_dir); trace!("Config dir: {:?}", config_dir);
match cli.command { match cli.command {
Commands::Init { repo } => { Commands::Init { repo_url } => {
let is_first_device: bool;
// get repo or initialize it // get repo or initialize it
let repo = match repo { let repo_url = match repo_url {
Some(repo) => { Some(repo_url) => {
trace!("repo: {}", repo); trace!("repo: {}", repo_url);
unimplemented!("Need to use git2"); // TODO 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 => { None => {
trace!("No repo provided"); trace!("No repo provided");
@ -122,6 +128,7 @@ fn main() -> Result<(), String> {
Err(e) => return Err(e.to_string()), Err(e) => return Err(e.to_string()),
}; };
} }
is_first_device = true;
repo repo
} }
}; };
@ -142,8 +149,11 @@ fn main() -> Result<(), String> {
// Add new device to devices.yml // Add new device to devices.yml
{ {
// let mut devices = get_devices(&config_dir)?; let mut devices: Vec<Device> = if is_first_device {
let mut devices: Vec<Device> = vec![]; vec![]
} else {
get_devices(&config_dir)?
};
devices.push(device.clone()); devices.push(device.clone());
trace!("Devices: {:?}", devices); trace!("Devices: {:?}", devices);
write_devices(&config_dir, devices)?; write_devices(&config_dir, devices)?;
@ -151,7 +161,7 @@ fn main() -> Result<(), String> {
// commit // commit
match add_and_commit( match add_and_commit(
&repo, &repo_url,
&Path::new(DEVICESFILE), &Path::new(DEVICESFILE),
&format!("Add new devname: {}", &device.name), &format!("Add new devname: {}", &device.name),
) { ) {