Skip to content

Commit

Permalink
Added hashbang to output
Browse files Browse the repository at this point in the history
  • Loading branch information
y-mx-b committed Sep 15, 2022
1 parent 97c0272 commit 33c8321
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// The path of the shell to use in the hashbang
#[clap(short, long, value_parser)]
pub shell_path: Option<PathBuf>,

/// Print extra information
#[clap(short, long, action)]
Expand Down
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::HashMap;
pub struct Env {
#[serde(alias = "env-vars")]
env_vars: HashMap<String, Vec<String>>,
// TODO add aliases
}

impl Env {
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

0 comments on commit 33c8321

Please sign in to comment.