Print the store path to stdout for build and generate.

This commit is contained in:
r-vdp 2023-05-15 20:02:16 +02:00
parent 3ed3a26b56
commit c430035749
No known key found for this signature in database
2 changed files with 37 additions and 11 deletions

View file

@ -4,6 +4,7 @@ mod systemd;
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::os::unix; use std::os::unix;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{fs, str}; use std::{fs, str};
@ -65,6 +66,24 @@ impl std::fmt::Display for StorePath {
} }
} }
impl AsRef<StorePath> for StorePath {
fn as_ref(&self) -> &StorePath {
self
}
}
impl<'a> From<StorePath> for Cow<'a, StorePath> {
fn from(value: StorePath) -> Self {
Cow::Owned(value)
}
}
impl<'a> From<&'a StorePath> for Cow<'a, StorePath> {
fn from(value: &'a StorePath) -> Self {
Cow::Borrowed(value)
}
}
fn create_store_link(store_path: &StorePath, from: &Path) -> Result<()> { fn create_store_link(store_path: &StorePath, from: &Path) -> Result<()> {
create_link(Path::new(&store_path.store_path), from) create_link(Path::new(&store_path.store_path), from)
} }

View file

@ -1,5 +1,6 @@
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use std::borrow::Cow;
use std::ffi::OsString; use std::ffi::OsString;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::{self, ExitCode}; use std::process::{self, ExitCode};
@ -135,12 +136,12 @@ fn go(args: Args) -> Result<()> {
} }
Action::Build { Action::Build {
build_args: BuildArgs { flake_uri }, build_args: BuildArgs { flake_uri },
} => build(&flake_uri, &target_host), } => build(&flake_uri, &target_host).and_then(print_store_path),
Action::Deactivate { Action::Deactivate {
optional_store_path_args: OptionalStorePathArgs { maybe_store_path }, optional_store_path_args: OptionalStorePathArgs { maybe_store_path },
} => deactivate(maybe_store_path, &target_host, use_remote_sudo), } => deactivate(maybe_store_path, &target_host, use_remote_sudo),
Action::Generate { generate_args } => { Action::Generate { generate_args } => {
generate(&generate_args, &target_host, use_remote_sudo) generate(&generate_args, &target_host, use_remote_sudo).and_then(print_store_path)
} }
Action::Switch { Action::Switch {
build_args: BuildArgs { flake_uri }, build_args: BuildArgs { flake_uri },
@ -154,23 +155,27 @@ fn go(args: Args) -> Result<()> {
} }
} }
fn build(flake_uri: &str, target_host: &Option<String>) -> Result<()> { fn print_store_path<SP: AsRef<StorePath>>(store_path: SP) -> Result<()> {
// Print the raw store path to stdout
println!("{}", store_path.as_ref());
Ok(())
}
fn build(flake_uri: &str, target_host: &Option<String>) -> Result<StorePath> {
let store_path = do_build(flake_uri)?; let store_path = do_build(flake_uri)?;
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
// Print the raw store path to stdout Ok(store_path)
println!("{store_path}");
Ok(())
} }
fn do_build(flake_uri: &str) -> Result<StorePath> { fn do_build(flake_uri: &str) -> Result<StorePath> {
system_manager::generate::build(flake_uri) system_manager::generate::build(flake_uri)
} }
fn generate( fn generate<'a>(
args: &GenerateArgs, args: &'a GenerateArgs,
target_host: &Option<String>, target_host: &Option<String>,
use_remote_sudo: bool, use_remote_sudo: bool,
) -> Result<()> { ) -> Result<Cow<'a, StorePath>> {
match args { match args {
GenerateArgs { GenerateArgs {
flake_uri: Some(flake_uri), flake_uri: Some(flake_uri),
@ -178,14 +183,16 @@ fn generate(
} => { } => {
let store_path = do_build(flake_uri)?; let store_path = do_build(flake_uri)?;
copy_closure(&store_path, target_host)?; copy_closure(&store_path, target_host)?;
do_generate(&store_path, target_host, use_remote_sudo) do_generate(&store_path, target_host, use_remote_sudo)?;
Ok(store_path.into())
} }
GenerateArgs { GenerateArgs {
flake_uri: None, flake_uri: None,
store_path: Some(store_path), store_path: Some(store_path),
} => { } => {
copy_closure(store_path, target_host)?; copy_closure(store_path, target_host)?;
do_generate(store_path, target_host, use_remote_sudo) do_generate(store_path, target_host, use_remote_sudo)?;
Ok(store_path.into())
} }
_ => { _ => {
anyhow::bail!("Supply either a flake URI or a store path.") anyhow::bail!("Supply either a flake URI or a store path.")