use eyre::Result;
use crate::config::{Config, Settings};
use crate::task::Task;
use crate::{env, file};
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct TaskEdit {
#[clap()]
task: String,
#[clap(long, short, verbatim_doc_comment)]
path: bool,
}
impl TaskEdit {
pub fn run(self) -> Result<()> {
let config = Config::try_get()?;
let settings = Settings::try_get()?;
settings.ensure_experimental()?;
let task = config.tasks().get(&self.task).cloned().map_or_else(
|| {
let path = config
.project_root
.as_ref()
.unwrap_or(&env::current_dir()?)
.join(".rtx")
.join("tasks")
.join(&self.task);
Task::from_path(path)
},
Ok,
)?;
let file = &task.config_source;
if !file.exists() {
file::create(file)?;
file::make_executable(file)?;
}
if self.path {
rtxprintln!("{}", file.display());
} else {
cmd!(&*env::EDITOR, &file).run()?;
}
Ok(())
}
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>rtx task edit build</bold>
$ <bold>rtx task edit test</bold>
"#
);