Fix panic with hive path parsing and use value parser

This commit is contained in:
i1i1 2023-08-10 17:14:47 +03:00 committed by Zhaofeng Li
parent ce0782ccac
commit 1ad9301c62
5 changed files with 24 additions and 42 deletions

View file

@ -10,7 +10,7 @@ use clap_complete::Shell;
use const_format::{concatcp, formatcp};
use env_logger::fmt::WriteStyle;
use crate::command;
use crate::{command, nix::HivePath};
/// Base URL of the manual, without the trailing slash.
const MANUAL_URL_BASE: &str = "https://colmena.cli.rs";
@ -132,15 +132,8 @@ pub fn build_cli(include_internal: bool) -> ClapCommand {
.help("Path to a Hive expression, a flake.nix, or a Nix Flake URI")
.long_help(Some(CONFIG_HELP))
.display_order(HELP_ORDER_FIRST)
// The default value is a lie (sort of)!
//
// The default behavior is to search upwards from the
// current working directory for a file named "flake.nix"
// or "hive.nix". This behavior is disabled if --config/-f
// is explicitly supplied by the user (occurrences_of > 0).
.default_value("hive.nix")
.global(true))
.global(true)
.value_parser(value_parser!(HivePath)))
.arg(Arg::new("show-trace")
.long("show-trace")
.help("Show debug information for Nix commands")

View file

@ -1,4 +1,4 @@
use clap::{ArgMatches, Args, FromArgMatches, Command};
use clap::{ArgMatches, Args, Command, FromArgMatches};
use crate::error::ColmenaError;
use crate::nix::evaluator::nix_eval_jobs::get_pinned_nix_eval_jobs;

View file

@ -1,6 +1,6 @@
use clap::{Args, Command as ClapCommand};
use crate::{nix::Goal, util};
use crate::nix::Goal;
pub use super::apply::run;
use super::apply::DeployOpts;

View file

@ -122,20 +122,27 @@ impl FromStr for HivePath {
fn from_str(s: &str) -> Result<Self, Self::Err> {
// TODO: check for escaped colon maybe?
let path = std::path::Path::new(s);
let s = s.to_owned();
let path = std::path::PathBuf::from(&s);
let fut = async move {
if !path.exists() && s.contains(':') {
// Treat as flake URI
let flake = Flake::from_uri(s).await?;
log::info!("Using flake: {}", flake.uri());
Ok(Self::Flake(flake))
} else {
HivePath::from_path(path).await
}
};
let handle = tokio::runtime::Handle::try_current()
.expect("We should always be executed after we have a runtime");
if !path.exists() && s.contains(':') {
// Treat as flake URI
let flake = handle.block_on(Flake::from_uri(s))?;
log::info!("Using flake: {}", flake.uri());
Ok(Self::Flake(flake))
} else {
handle.block_on(HivePath::from_path(path))
}
std::thread::spawn(move || handle.block_on(fut))
.join()
.expect("Failed to join future")
}
}

View file

@ -4,7 +4,6 @@ use std::error::Error;
use std::process::Stdio;
use async_trait::async_trait;
use clap::{Arg, Command as ClapCommand};
use futures::future::join3;
use serde::de::DeserializeOwned;
use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
@ -207,23 +206,6 @@ where
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
pub fn register_selector_args(command: ClapCommand) -> ClapCommand {
command
.arg(Arg::new("on")
.long("on")
.value_name("NODES")
.help("Node selector")
.long_help(r#"Select a list of nodes to deploy to.
The list is comma-separated and globs are supported. To match tags, prepend the filter by @. Valid examples:
- host1,host2,host3
- edge-*
- edge-*,core-*
- @a-tag,@tags-can-have-*"#)
.num_args(1))
}
pub async fn capture_stream<R>(
mut stream: BufReader<R>,
job: Option<JobHandle>,