Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/edit: Minor tweaks #167

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ pub(crate) struct SwitchOpts {
/// Perform an edit operation
#[derive(Debug, Parser)]
pub(crate) struct EditOpts {
/// Path to new system specification; use `-` for stdin
pub(crate) filename: String,
/// Use filename to edit system specification
#[clap(long, short = 'f')]
pub(crate) filename: Option<String>,

/// Don't display progress
#[clap(long)]
Expand Down Expand Up @@ -426,19 +427,20 @@ async fn edit(opts: EditOpts) -> Result<()> {
let repo = &sysroot.repo();
let (booted_deployment, _deployments, host) =
crate::status::get_status_require_booted(sysroot)?;
let new_host: Host = if opts.filename == "-" {
let new_host: Host = if let Some(filename) = opts.filename {
let mut r = std::io::BufReader::new(std::fs::File::open(&filename)?);
serde_yaml::from_reader(&mut r)?
} else {
let tmpf = tempfile::NamedTempFile::new()?;
serde_yaml::to_writer(std::io::BufWriter::new(tmpf.as_file()), &host)?;
crate::utils::spawn_editor(&tmpf)?;
tmpf.as_file().seek(std::io::SeekFrom::Start(0))?;
serde_yaml::from_reader(&mut tmpf.as_file())?
} else {
let mut r = std::io::BufReader::new(std::fs::File::open(opts.filename)?);
serde_yaml::from_reader(&mut r)?
};

if new_host.spec == host.spec {
anyhow::bail!("No changes in current host spec");
println!("Edit cancelled, no changes made.");
return Ok(());
}
let new_spec = RequiredHostSpec::from_spec(&new_host.spec)?;
let fetched = pull(repo, &new_spec.image, opts.quiet).await?;
Expand Down
Loading