From 33c8321c977e5c71f1c788e6aa700ae153270174 Mon Sep 17 00:00:00 2001 From: Slope Date: Thu, 15 Sep 2022 21:40:54 +0900 Subject: [PATCH] Added hashbang to output --- src/cli.rs | 6 +++--- src/env.rs | 1 + src/main.rs | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 71109de..117a7be 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,9 +18,9 @@ pub struct Cli { #[clap(short, long, arg_enum, value_parser)] pub format: Format, - /// Sets a custom config file - #[clap(short, long, value_parser, value_name = "CONFIG")] - pub config: Option, + /// The path of the shell to use in the hashbang + #[clap(short, long, value_parser)] + pub shell_path: Option, /// Print extra information #[clap(short, long, action)] diff --git a/src/env.rs b/src/env.rs index 32814e6..e6d0e10 100644 --- a/src/env.rs +++ b/src/env.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; pub struct Env { #[serde(alias = "env-vars")] env_vars: HashMap>, + // TODO add aliases } impl Env { diff --git a/src/main.rs b/src/main.rs index f73ef95..b28a499 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use format::*; use std::fs; use env::Env; use std::io::Write; +use std::path::PathBuf; fn main() { let cli = Cli::parse(); @@ -52,10 +53,23 @@ fn main() { Format::Fish => data.to_fish(), Format::Tcsh => data.to_tcsh(), }; - vprintln!(v, "Output:\n{}", output); + + // create new output file, clear if already exists + let mut output_file = fs::File::create(output_name).unwrap(); + + // add hashbang + // TODO do it more safely + let shell_path: PathBuf = match cli.shell_path { + Some(shell) => shell, + None => match cli.format { + Format::Sh => PathBuf::from("/usr/bin/sh"), + Format::Fish => PathBuf::from("/usr/bin/fish"), + Format::Tcsh => PathBuf::from("/usr/bin/tcsh"), + } + }; + output_file.write_all(vec!["#!", shell_path.to_str().unwrap()].join("").as_bytes()).unwrap(); // write output to file // TODO do it more safely - let mut output_file = fs::File::create(output_name).unwrap(); output_file.write_all(output.as_bytes()).unwrap(); }