This commit is contained in:
Hamcha 2020-01-21 17:18:58 +01:00
commit 1c28fddbe7
Signed by: hamcha
GPG Key ID: 44AD3571EB09A39E
5 changed files with 314 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
**/*.rs.bk

140
Cargo.lock generated Normal file
View File

@ -0,0 +1,140 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hermit-abi"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "isatty"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "osenv"
version = "0.1.0"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"isatty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-width"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
"checksum isatty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc"
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "osenv"
version = "0.1.0"
authors = ["Hamcha <hamcha@crunchy.rocks>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33"
isatty = "0.1.9"

43
README.md Normal file
View File

@ -0,0 +1,43 @@
# osenv
Apply .env files to shell environments!
## What is it
Given a .env like this:
```env
DB_USER=admin
DB_PASSWORD=changeme
```
it will generate scripts for bash (and likely other *sh) and powershell, like:
### Powershell
Example output:
```ps1
$env:DB_USER = "admin"; $env:DB_PASSWORD = "changeme"
```
Use `osenv` like this:
```ps1
osenv -s ps1 | iex
```
### *nix shell
Example output:
```sh
export DB_USER=admin
export DB_PASSWORD=changeme
```
Run `osenv` like this:
```sh
eval $(osenv)
```

118
src/main.rs Normal file
View File

@ -0,0 +1,118 @@
extern crate clap;
use clap::{App, Arg};
use isatty::stdout_isatty;
use std::env;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::Path;
fn current_args() -> String {
let mut out = String::from("osenv");
for argument in env::args().skip(1) {
out += format!(" {}", argument).as_str();
}
out
}
fn main() -> std::io::Result<()> {
#[cfg(target_os = "macos")]
static OS_DEFAULT_SHELL: &str = "sh";
#[cfg(target_os = "linux")]
static OS_DEFAULT_SHELL: &str = "sh";
#[cfg(target_os = "windows")]
static OS_DEFAULT_SHELL: &str = "ps1";
let cmd = App::new("osenv")
.version("0.1")
.author("Alessandro Gatti <hamcha@crunchy.rocks>")
.about("Cross-platform env helper")
.arg(
Arg::with_name("directory")
.short("d")
.long("directory")
.value_name("DIRECTORY")
.help("Directory to scan for .env files")
.default_value(".")
.takes_value(true),
)
.arg(
Arg::with_name("file")
.short("f")
.long("file")
.value_name("ENVFILE")
.help("Env file")
.default_value(".env")
.takes_value(true),
)
.arg(
Arg::with_name("environment")
.short("e")
.long("env")
.value_name("ENVIRONMENT")
.help("If provided, will look for .env.ENVIRONMENT")
.takes_value(true),
)
.arg(
Arg::with_name("shell")
.short("s")
.long("shell")
.help("Output format")
.takes_value(true)
.default_value(OS_DEFAULT_SHELL)
.possible_values(&["ps1", "sh"]),
)
.get_matches();
let dir = cmd.value_of("directory").unwrap();
let file = cmd.value_of("file").unwrap();
let env = cmd.value_of("environment");
let shell = cmd.value_of("shell").unwrap();
// If -env was specified, add it as a suffix
let filename = if env.is_some() {
format!("{}.{}", file, env.unwrap())
} else {
String::from(file)
};
// Join directory and file
let path = Path::new(dir).join(filename);
if !path.exists() {
println!("{}: file not found", path.display());
std::process::exit(1);
}
let file = File::open(path)?;
let reader = BufReader::new(file);
// Print help messages if printing to terminal
if stdout_isatty() {
match shell {
"ps1" => println!("#\n# It looks like you are not running osenv properly\n# The proper command on Powershell is:\n# {} | iex\n# Hope this helps!\n#", current_args()),
"sh" => println!("#\n# It looks like you are not running osenv properly\n# The proper command on unix shells is:\n# eval $({})\n# Hope this helps!\n#", current_args()),
_ => (),
}
}
for line in reader.lines() {
let envline = line.unwrap();
let idx = envline.find('=');
if idx.is_some() {
match shell {
"ps1" => {
// val needs to be shifted to remove the separator
println!(
"$env:{} = \"{}\"; ",
envline.get(..idx.unwrap()).unwrap(),
envline.get(idx.unwrap() + 1..).unwrap()
)
}
"sh" => println!("export {}", envline),
_ => (),
}
}
}
Ok(())
}