2022-10-26 08:29:39 +00:00
|
|
|
use crate::{app, yaml};
|
2022-10-25 14:01:06 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2022-10-26 08:54:55 +00:00
|
|
|
use app::ExternalMsg;
|
2022-10-27 09:03:21 +00:00
|
|
|
use path_absolutize::*;
|
2022-10-25 14:01:06 +00:00
|
|
|
use serde_json as json;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::{env, fs};
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-09-17 03:47:28 +00:00
|
|
|
/// The arguments to pass
|
2021-09-16 14:59:18 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct Cli {
|
2022-10-25 14:01:06 +00:00
|
|
|
pub bin: String,
|
2021-09-16 14:59:18 +00:00
|
|
|
pub version: bool,
|
|
|
|
pub help: bool,
|
|
|
|
pub read_only: bool,
|
2021-10-30 05:12:21 +00:00
|
|
|
pub force_focus: bool,
|
2022-02-03 03:43:03 +00:00
|
|
|
pub print_pwd_as_result: bool,
|
2022-05-20 18:43:17 +00:00
|
|
|
pub read0: bool,
|
|
|
|
pub write0: bool,
|
2022-10-27 09:03:21 +00:00
|
|
|
pub vroot: Option<PathBuf>,
|
2021-09-16 14:59:18 +00:00
|
|
|
pub config: Option<PathBuf>,
|
|
|
|
pub extra_config: Vec<PathBuf>,
|
|
|
|
pub on_load: Vec<app::ExternalMsg>,
|
2022-10-25 14:01:06 +00:00
|
|
|
pub pipe_msg_in: Vec<String>,
|
2022-10-25 19:38:34 +00:00
|
|
|
pub print_msg_in: Vec<String>,
|
2021-10-30 05:12:21 +00:00
|
|
|
pub paths: Vec<PathBuf>,
|
2021-09-16 14:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cli {
|
2022-10-28 11:46:34 +00:00
|
|
|
fn read_path(arg: &str) -> Result<PathBuf> {
|
2021-10-30 05:12:21 +00:00
|
|
|
if arg.is_empty() {
|
2021-10-30 10:43:47 +00:00
|
|
|
bail!("empty string passed")
|
2021-10-30 05:12:21 +00:00
|
|
|
};
|
|
|
|
|
2022-10-27 09:03:21 +00:00
|
|
|
let path = PathBuf::from(arg).absolutize()?.to_path_buf();
|
2021-10-30 05:12:21 +00:00
|
|
|
if path.exists() {
|
2022-10-28 11:46:34 +00:00
|
|
|
Ok(path)
|
2021-10-30 10:43:47 +00:00
|
|
|
} else {
|
2022-10-28 11:46:34 +00:00
|
|
|
bail!("path doesn't exist: {}", path.to_string_lossy())
|
2021-10-30 10:43:47 +00:00
|
|
|
}
|
2021-10-30 05:12:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 03:47:28 +00:00
|
|
|
/// Parse arguments from the command-line
|
2021-09-16 14:59:18 +00:00
|
|
|
pub fn parse(args: env::Args) -> Result<Self> {
|
|
|
|
let mut cli = Self::default();
|
2022-10-25 14:01:06 +00:00
|
|
|
let mut args = args.peekable();
|
2022-10-27 10:44:56 +00:00
|
|
|
cli.bin = args
|
|
|
|
.next()
|
|
|
|
.map(which::which)
|
|
|
|
.context("failed to parse xplr binary path")?
|
|
|
|
.context("failed to find xplr binary path")?
|
|
|
|
.absolutize()?
|
|
|
|
.to_path_buf()
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_string();
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
let mut flag_ends = false;
|
|
|
|
|
2022-10-24 14:06:28 +00:00
|
|
|
while let Some(arg) = args.next() {
|
2021-10-30 05:12:21 +00:00
|
|
|
if flag_ends {
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.paths.push(Cli::read_path(&arg)?);
|
2021-10-30 05:12:21 +00:00
|
|
|
} else {
|
|
|
|
match arg.as_str() {
|
|
|
|
// Flags
|
|
|
|
"-" => {
|
2022-05-20 18:43:17 +00:00
|
|
|
let reader = BufReader::new(std::io::stdin());
|
|
|
|
if cli.read0 {
|
|
|
|
for path in reader.split(b'\0') {
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.paths
|
|
|
|
.push(Cli::read_path(&String::from_utf8(path?)?)?);
|
2022-05-20 18:43:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for path in reader.lines() {
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.paths.push(Cli::read_path(&path?)?);
|
2022-05-20 18:43:17 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-30 05:12:21 +00:00
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"-h" | "--help" => {
|
|
|
|
cli.help = true;
|
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"-V" | "--version" => {
|
|
|
|
cli.version = true;
|
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2022-05-20 18:43:17 +00:00
|
|
|
"--read0" => {
|
|
|
|
cli.read0 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
"--write0" => {
|
|
|
|
cli.write0 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
"-0" | "--null" => {
|
|
|
|
cli.read0 = true;
|
|
|
|
cli.write0 = true;
|
|
|
|
}
|
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"--" => {
|
|
|
|
flag_ends = true;
|
2021-09-16 14:59:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
// Options
|
2022-10-27 09:03:21 +00:00
|
|
|
"-c" | "--config" => {
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.config = Some(
|
2023-03-19 19:37:04 +00:00
|
|
|
args.next()
|
|
|
|
.map(|a| Cli::read_path(&a))
|
|
|
|
.with_context(|| format!("usage: xplr {arg} PATH"))??,
|
2022-10-28 11:46:34 +00:00
|
|
|
);
|
2022-10-27 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"--vroot" => {
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.vroot = Some(
|
2023-03-19 19:37:04 +00:00
|
|
|
args.next()
|
|
|
|
.map(|a| Cli::read_path(&a))
|
|
|
|
.with_context(|| format!("usage: xplr {arg} PATH"))??,
|
2022-10-28 11:46:34 +00:00
|
|
|
);
|
2022-10-27 09:03:21 +00:00
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"-C" | "--extra-config" => {
|
2022-10-24 14:06:28 +00:00
|
|
|
while let Some(path) =
|
|
|
|
args.next_if(|path| !path.starts_with('-'))
|
|
|
|
{
|
2022-10-28 11:46:34 +00:00
|
|
|
cli.extra_config.push(Cli::read_path(&path)?);
|
2021-09-16 14:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"--read-only" => cli.read_only = true,
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"--on-load" => {
|
2022-10-24 14:06:28 +00:00
|
|
|
while let Some(msg) = args.next_if(|msg| !msg.starts_with('-')) {
|
2022-10-26 08:29:39 +00:00
|
|
|
cli.on_load.push(yaml::from_str(&msg)?);
|
2021-10-27 14:29:20 +00:00
|
|
|
}
|
2021-10-26 14:38:20 +00:00
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
"--force-focus" => {
|
|
|
|
cli.force_focus = true;
|
|
|
|
}
|
2021-10-29 14:43:05 +00:00
|
|
|
|
2022-02-03 03:43:03 +00:00
|
|
|
"--print-pwd-as-result" => {
|
|
|
|
cli.print_pwd_as_result = true;
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:01:06 +00:00
|
|
|
"-m" | "--pipe-msg-in" => {
|
2022-10-25 19:38:34 +00:00
|
|
|
cli.pipe_msg_in.extend(args.by_ref());
|
2022-10-26 06:14:32 +00:00
|
|
|
if cli.pipe_msg_in.is_empty() {
|
2022-10-28 11:46:34 +00:00
|
|
|
bail!("usage: xplr {} FORMAT [ARGUMENT]...", arg)
|
2022-10-26 06:14:32 +00:00
|
|
|
}
|
2022-10-25 19:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"-M" | "--print-msg-in" => {
|
|
|
|
cli.print_msg_in.extend(args.by_ref());
|
2022-10-26 06:14:32 +00:00
|
|
|
if cli.print_msg_in.is_empty() {
|
2022-10-28 11:46:34 +00:00
|
|
|
bail!("usage: xplr {} FORMAT [ARGUMENT]...", arg)
|
2022-10-26 06:14:32 +00:00
|
|
|
}
|
2022-10-25 14:01:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 05:12:21 +00:00
|
|
|
// path
|
|
|
|
path => {
|
2022-10-28 11:46:34 +00:00
|
|
|
if path.starts_with('-') && !flag_ends {
|
|
|
|
bail!(
|
|
|
|
"invalid argument: {0:?}, try `-- {0:?}` or `--help`",
|
|
|
|
path
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
cli.paths.push(Cli::read_path(path)?);
|
|
|
|
}
|
2021-09-16 14:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(cli)
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 14:01:06 +00:00
|
|
|
|
|
|
|
pub fn pipe_msg_in(args: Vec<String>) -> Result<()> {
|
2022-10-25 19:38:34 +00:00
|
|
|
let mut msg = fmt_msg_in(args)?;
|
2022-10-25 14:01:06 +00:00
|
|
|
|
2022-10-25 19:38:34 +00:00
|
|
|
if let Ok(path) = std::env::var("XPLR_PIPE_MSG_IN") {
|
|
|
|
let delimiter = fs::read(&path)?
|
|
|
|
.first()
|
|
|
|
.cloned()
|
|
|
|
.context("failed to detect delimmiter")?;
|
|
|
|
|
2024-01-03 10:58:02 +00:00
|
|
|
msg.push(delimiter.into());
|
2022-10-25 19:38:34 +00:00
|
|
|
File::options()
|
|
|
|
.append(true)
|
|
|
|
.open(&path)?
|
|
|
|
.write_all(msg.as_bytes())?;
|
|
|
|
} else {
|
2023-03-19 19:37:04 +00:00
|
|
|
println!("{msg}");
|
2022-10-25 19:38:34 +00:00
|
|
|
};
|
2022-10-25 14:01:06 +00:00
|
|
|
|
2022-10-25 19:38:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_msg_in(args: Vec<String>) -> Result<()> {
|
|
|
|
let msg = fmt_msg_in(args)?;
|
2023-03-19 19:37:04 +00:00
|
|
|
print!("{msg}");
|
2022-10-25 19:38:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_msg_in(args: Vec<String>) -> Result<String> {
|
2023-05-19 06:07:27 +00:00
|
|
|
let msg = match jf::format(args.into_iter().map(Into::into)) {
|
|
|
|
Ok(msg) => msg,
|
|
|
|
Err(jf::Error::Jf(e)) => bail!("xplr -m: {e}"),
|
|
|
|
Err(jf::Error::Json(e)) => bail!("xplr -m: json: {e}"),
|
|
|
|
Err(jf::Error::Yaml(e)) => bail!("xplr -m: yaml: {e}"),
|
2023-07-15 19:48:23 +00:00
|
|
|
Err(jf::Error::Io(e)) => bail!("xplr -m: io: {e}"),
|
2022-10-26 08:51:28 +00:00
|
|
|
};
|
2022-10-26 08:29:39 +00:00
|
|
|
|
2023-05-19 06:07:27 +00:00
|
|
|
// validate
|
|
|
|
let _: ExternalMsg = json::from_str(&msg)?;
|
|
|
|
|
2022-10-25 19:38:34 +00:00
|
|
|
Ok(msg)
|
2022-10-25 14:01:06 +00:00
|
|
|
}
|