Skip to content

Commit

Permalink
alright it does the thing!
Browse files Browse the repository at this point in the history
  • Loading branch information
OutdatedVersion committed Sep 17, 2023
1 parent 3d7857b commit 2f3f025
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fixture/comments.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OH=hey
# ignore this
PLEASE=1
Empty file added fixture/empty.env
Empty file.
3 changes: 3 additions & 0 deletions fixture/multi.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HELLO=frends
how=are
YOU=1
3 changes: 3 additions & 0 deletions fixture/new-line.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HELLO=1

A=1
1 change: 1 addition & 0 deletions fixture/one.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HEY=1
56 changes: 54 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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::<Vec<_>>()
.join("\n");

println!("{}", help);
return ExitCode::SUCCESS;
}
}
}

0 comments on commit 2f3f025

Please sign in to comment.