From 2f3f02506ecaf7c0b2bb04a503639cc7d2076943 Mon Sep 17 00:00:00 2001 From: Ben Watkins Date: Sat, 16 Sep 2023 22:14:48 -0500 Subject: [PATCH] alright it does the thing! --- fixture/comments.env | 3 +++ fixture/empty.env | 0 fixture/multi.env | 3 +++ fixture/new-line.env | 3 +++ fixture/one.env | 1 + src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 fixture/comments.env create mode 100644 fixture/empty.env create mode 100644 fixture/multi.env create mode 100644 fixture/new-line.env create mode 100644 fixture/one.env diff --git a/fixture/comments.env b/fixture/comments.env new file mode 100644 index 0000000..5fb49a1 --- /dev/null +++ b/fixture/comments.env @@ -0,0 +1,3 @@ +OH=hey +# ignore this +PLEASE=1 diff --git a/fixture/empty.env b/fixture/empty.env new file mode 100644 index 0000000..e69de29 diff --git a/fixture/multi.env b/fixture/multi.env new file mode 100644 index 0000000..c9d303c --- /dev/null +++ b/fixture/multi.env @@ -0,0 +1,3 @@ +HELLO=frends +how=are +YOU=1 diff --git a/fixture/new-line.env b/fixture/new-line.env new file mode 100644 index 0000000..5b18fde --- /dev/null +++ b/fixture/new-line.env @@ -0,0 +1,3 @@ +HELLO=1 + +A=1 diff --git a/fixture/one.env b/fixture/one.env new file mode 100644 index 0000000..3cea584 --- /dev/null +++ b/fixture/one.env @@ -0,0 +1 @@ +HEY=1 diff --git a/src/main.rs b/src/main.rs index e7a11a9..7e0211d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,55 @@ -fn main() { - println!("Hello, world!"); +use std::{env, process::ExitCode, fs}; + +// much more elegant solution than the monstrosity I had gotten to +// https://stackoverflow.com/a/58113997 +fn bin_name() -> Option { + std::env::current_exe() + .ok()? + .file_name()? + .to_str()? + .to_owned() + .into() +} + +fn main() -> ExitCode { + match env::args().skip(1).next() { + None => { + let bin = match bin_name() { + Some(name) => name, + None => "read-dotenv".to_owned() + }; + + // These packages look great but we don't need KBs of + // dependencies for three lines of ANSI codes here. :] + // https://lib.rs/crates/colored + // https://lib.rs/crates/termcolor + let red = "\x1b[38;5;203m"; + let reset = "\x1B[0m"; + let grey = "\x1b[38;5;247m"; + + eprintln!("‼️ {}Provide path to a .env file!{}", red, reset); + eprintln!("{}{} ~/example.env{}", grey, bin, reset); + + return ExitCode::FAILURE; + }, + Some(file_path) => { + let content = fs::read_to_string(file_path) + .expect("could not read .env file"); + + let help = content.split("\n") + .filter(|line| !line.is_empty() && !line.starts_with("#")) + .map(|line| { + if line.starts_with("export") { + return line.to_owned(); + } else { + return format!("export {}", line); + } + }) + .collect::>() + .join("\n"); + + println!("{}", help); + return ExitCode::SUCCESS; + } + } }