rune icon indicating copy to clipboard operation
rune copied to clipboard

request: add std::env::var

Open hh9527 opened this issue 4 years ago • 2 comments

I want to write some rune script to get os environment, but I dont know how to read environtment variables of OS. Is there a missing?

hh9527 avatar Apr 06 '21 06:04 hh9527

No, not yet!

But it's pretty easy to write a module which does anything you want, including getting environment variables. See one of these for inspiration: https://github.com/rune-rs/rune/tree/main/crates/rune-modules/src

udoprog avatar Apr 06 '21 08:04 udoprog

This is what I use:

use rune::{ContextError, Module};
use rune::runtime::Object;

pub fn env() -> Object {
    let mut map = Object::new();
    std::env::vars().for_each(|(k, v)| {
        map.insert(k.into(), v.into());
    });
    map
}

pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::with_crate("sys");
    module.function(["env"], env)?;
    Ok(module)
}

jasal82 avatar Mar 24 '23 20:03 jasal82