1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
#![deny( absolute_paths_not_starting_with_crate, ambiguous_associated_items, anonymous_parameters, const_err, dead_code, deprecated, deprecated_in_future, duplicate_macro_exports, ellipsis_inclusive_range_patterns, exceeding_bitshifts, explicit_outlives_requirements, exported_private_dependencies, ill_formed_attribute_input, illegal_floating_point_literal_pattern, improper_ctypes, incoherent_fundamental_impls, invalid_type_param_default, irrefutable_let_patterns, keyword_idents, late_bound_lifetime_arguments, legacy_constructor_visibility, legacy_directory_ownership, macro_expanded_macro_exports_accessed_by_absolute_paths, missing_copy_implementations, missing_docs, missing_fragment_specifier, mutable_borrow_reservation_conflict, mutable_transmutes, nested_impl_trait, no_mangle_const_items, no_mangle_generic_items, non_camel_case_types, non_shorthand_field_patterns, non_snake_case, non_upper_case_globals, order_dependent_trait_objects, overflowing_literals, parenthesized_params_in_types_and_modules, path_statements, patterns_in_fns_without_body, plugin_as_library, private_doc_tests, private_in_public, proc_macro_derive_resolution_fallback, pub_use_of_private_extern_crate, question_mark_macro_sep, safe_extern_statics, safe_packed_borrows, stable_features, trivial_bounds, trivial_casts, trivial_numeric_casts, type_alias_bounds, tyvar_behind_raw_pointer, unconditional_recursion, unions_with_drop_fields, unknown_crate_types, unnameable_test_items, unreachable_code, unreachable_patterns, unreachable_pub, unsafe_code, unstable_features, unstable_name_collisions, unused_allocation, unused_assignments, unused_attributes, unused_comparisons, unused_doc_comments, unused_extern_crates, unused_features, unused_import_braces, unused_imports, unused_labels, unused_lifetimes, unused_macros, unused_must_use, unused_mut, unused_parens, unused_qualifications, unused_unsafe, unused_variables, where_clauses_object_safety, while_true )] #![warn(macro_use_extern_crate, unknown_lints)] #![allow( bare_trait_objects, box_pointers, elided_lifetimes_in_paths, intra_doc_link_resolution_failure, missing_doc_code_examples, missing_debug_implementations, single_use_lifetimes, unused_results, variant_size_differences, warnings, renamed_and_removed_lints )] #![cfg_attr(feature = "clippy", feature(plugin))] //! # envmnt //! //! Environment variables utility functions. //! //! This library has many helper functions to access/modify/check environment variables. //! //! # Examples //! //! ## Get/Set/Remove environment variables //! //! ``` //! extern crate envmnt; //! //! fn main() { //! if !envmnt::exists("MY_ENV_VAR") { //! envmnt::set("MY_ENV_VAR", "SOME VALUE"); //! } //! //! let mut value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE"); //! println!("Env Value: {}", &value); //! //! value = envmnt::get_or_panic("MY_ENV_VAR"); //! println!("Env Value: {}", &value); //! //! let pre_value = envmnt::get_set("MY_ENV_VAR", "SOME NEW VALUE"); //! //! let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE"); //! println!("New Env Value: {}", &value); //! println!("Previous Env Value: {:?}", &pre_value); //! //! let var_was_set = envmnt::set_optional("MY_ENV_VAR", &Some("OPTIONAL VALUE")); //! println!("Env Was Modified: {}", var_was_set); //! //! let all_vars = envmnt::vars(); // returned as Vec<(String, String)> //! //! for (key, value) in all_vars { //! println!("{}: {}", key, value); //! } //! } //! ``` //! //! ## Get/Set boolean environment variables and other comparisons //! //! ``` //! extern crate envmnt; //! //! fn main() { //! envmnt::set_bool("FLAG_VAR", true); //! let mut flag_value = envmnt::is_or("FLAG_VAR", false); //! println!("Bool Flag: {}", &flag_value); //! //! flag_value = envmnt::is("FLAG_VAR"); //! assert!(flag_value); //! //! envmnt::set_bool("FLAG_VAR", true); //! assert!(envmnt::is_equal("FLAG_VAR", "true")); //! //! envmnt::set("MY_ENV_VAR", "SOME VALUE"); //! let same = envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"); //! println!("Value Is Same: {}", &same); //! } //! ``` //! //! ## Bulk Operations //! //! ``` //! extern crate envmnt; //! extern crate indexmap; //! //! use indexmap::IndexMap; //! //! fn main() { //! let mut env: IndexMap<String, String> = IndexMap::new(); //! env.insert("ENV_VAR1".to_string(), "MY VALUE".to_string()); //! env.insert("ENV_VAR2".to_string(), "MY VALUE2".to_string()); //! //! envmnt::set_all(&env); //! //! let value = envmnt::get_or_panic("ENV_VAR1"); //! println!("Value Is: {}", &value); //! //! let mut found = envmnt::is_any_exists(&vec!["ENV_VAR1", "ENV_VAR2"]); //! //! println!("Any Found: {}", &found); //! //! found = envmnt::is_all_exists(&vec!["ENV_VAR1", "ENV_VAR2"]); //! //! println!("All Found: {}", &found); //! //! env = IndexMap::new(); //! env.insert("ENV_VAR1".to_string(), "MY VALUE".to_string()); //! env.insert("ENV_VAR2".to_string(), "MY VALUE2".to_string()); //! //! let eval_env = |value: String| { //! let mut buffer = String::from("VALUE-"); //! buffer.push_str(&value); //! buffer //! }; //! //! envmnt::evaluate_and_set_all(&env, eval_env); //! //! let value = envmnt::get_or_panic("ENV_VAR1"); //! println!("Value Is: {}", &value); //! } //! ``` //! //! ## File Operations //! //! ``` //! extern crate envmnt; //! //! fn main() { //! let mut output = envmnt::load_file("./src/test/var.env"); //! assert!(output.is_ok()); //! //! let eval_env = |value: String| { //! let mut buffer = String::from("PREFIX-"); //! buffer.push_str(&value); //! buffer //! }; //! //! output = envmnt::evaluate_and_load_file("./src/test/var.env", eval_env); //! assert!(output.is_ok()); //! } //! ``` //! //! # Installation //! In order to use this library, just add it as a dependency: //! //! ```ini //! [dependencies] //! envmnt = "*" //! ``` //! //! # Contributing //! See [contributing guide](https://github.com/sagiegurari/envmnt/blob/master/.github/CONTRIBUTING.md) //! //! # License //! Developed by Sagie Gur-Ari and licensed under the //! [Apache 2](https://github.com/sagiegurari/envmnt/blob/master/LICENSE) open source license. //! #[cfg(test)] #[path = "./lib_test.rs"] mod lib_test; extern crate indexmap; mod bulk; mod environment; mod errors; mod file; mod util; use crate::errors::EnvmntError; use indexmap::IndexMap; use std::ffi::OsStr; /// Returns true environment variable is defined. /// /// # Arguments /// /// * `key` - The environment variable name /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// if !envmnt::exists("MY_ENV_VAR") { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// } /// } /// ``` pub fn exists<K: AsRef<OsStr>>(key: K) -> bool { environment::exists(key) } /// Removes the provided environment variable. /// /// # Arguments /// /// * `key` - The environment variable to remove /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// envmnt::remove("MY_ENV_VAR"); /// assert!(!envmnt::exists("MY_ENV_VAR")); /// } /// ``` pub fn remove<K: AsRef<OsStr>>(key: K) { environment::remove(key) } /// Removes the provided environment variable and returns its previous value (if any). /// /// # Arguments /// /// * `key` - The environment variable to remove /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// let value = envmnt::get_remove("MY_ENV_VAR"); /// assert!(!envmnt::exists("MY_ENV_VAR")); /// assert_eq!(value.unwrap(), "SOME VALUE"); /// } /// ``` pub fn get_remove<K: AsRef<OsStr>>(key: K) -> Option<String> { environment::get_remove(key) } /// Returns the environment variable value or if is not defined, the default value will be returned. /// /// # Arguments /// /// * `key` - The environment variable name /// * `default_value` - In case the environment variable is not defined, this value will be returned. /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// let mut value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE"); /// assert_eq!(value, "SOME VALUE"); /// /// value = envmnt::get_or("ANOTHER_ENV_VAR", "DEFAULT_VALUE"); /// assert_eq!(value, "DEFAULT_VALUE"); /// } /// ``` pub fn get_or<K: AsRef<OsStr>>(key: K, default_value: &str) -> String { environment::get_or(key, default_value) } /// Returns the environment variable value. /// If the variable is not defined, this function will panic. /// /// # Arguments /// /// * `key` - The environment variable name /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// let value = envmnt::get_or_panic("MY_ENV_VAR"); /// assert_eq!(value, "SOME VALUE"); /// } /// ``` pub fn get_or_panic<K: AsRef<OsStr>>(key: K) -> String { environment::get_or_panic(key) } /// Returns false if environment variable value if falsy. /// The value is falsy if it is one of the following: /// * Empty string /// * "false" (case insensitive) /// * "no" (case insensitive) /// * "0" /// Any other value is returned as true. /// /// # Arguments /// /// * `key` - The environment variable name /// * `default_value` - In case the environment variable is not defined, this value will be returned. /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set_bool("FLAG_VAR", true); /// assert!(envmnt::is_equal("FLAG_VAR", "true")); /// /// let flag_value = envmnt::is_or("FLAG_VAR", false); /// assert!(flag_value); /// } /// ``` pub fn is_or<K: AsRef<OsStr>>(key: K, default_value: bool) -> bool { environment::is_or(key, default_value) } /// Returns false if environment variable value if falsy. /// The value is falsy if it is one of the following: /// * Empty string /// * "false" (case insensitive) /// * "no" (case insensitive) /// * "0" /// Any other value is returned as true. /// This is same as calling is_or("varname", false) /// /// # Arguments /// /// * `key` - The environment variable name /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set_bool("FLAG_VAR", true); /// assert!(envmnt::is_equal("FLAG_VAR", "true")); /// /// let flag_value = envmnt::is("FLAG_VAR"); /// assert!(flag_value); /// } /// ``` pub fn is<K: AsRef<OsStr>>(key: K) -> bool { environment::is(key) } /// Sets the environment variable value. /// /// # Arguments /// /// * `key` - The environment variable name /// * `value` - The environment variable value /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE"); /// assert_eq!(value, "SOME VALUE"); /// } /// ``` pub fn set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) { environment::set(key, value) } /// Sets the environment variable with a true/false value as string. /// /// # Arguments /// /// * `key` - The environment variable name /// * `value` - true/false boolean value which will be converted to string /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set_bool("FLAG_VAR", true); /// assert!(envmnt::is_equal("FLAG_VAR", "true")); /// /// let flag_value = envmnt::is_or("FLAG_VAR", false); /// assert!(flag_value); /// } /// ``` pub fn set_bool<K: AsRef<OsStr>>(key: K, value: bool) { environment::set_bool(key, value) } /// Sets the environment variable if the provided option contains a value. /// /// # Arguments /// /// * `key` - The environment variable name /// * `value` - The optional value to set /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// let output = envmnt::set_optional("MY_ENV_VAR", &Some("OPTIONAL VALUE")); /// /// assert!(output); /// assert!(envmnt::is_equal( /// "MY_ENV_VAR", /// "OPTIONAL VALUE" /// )); /// } /// ``` pub fn set_optional<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: &Option<V>) -> bool { environment::set_optional(key, value) } /// Sets the environment variable value and returns the previous value. /// /// # Arguments /// /// * `key` - The environment variable name /// * `value` - The environment variable value /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// assert!(envmnt::is_equal("MY_ENV_VAR", "SOME VALUE")); /// /// let pre_value = envmnt::get_set("MY_ENV_VAR", "NEW VALUE"); /// /// let value = envmnt::get_or("MY_ENV_VAR", "DEFAULT_VALUE"); /// assert_eq!(value, "NEW VALUE"); /// assert_eq!(pre_value.unwrap(), "SOME VALUE"); /// } /// ``` pub fn get_set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) -> Option<String> { environment::get_set(key, value) } /// Returns all environment variables as a vector. /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// let all_vars = envmnt::vars(); /// /// for (key, value) in all_vars { /// println!("{}: {}", key, value); /// } /// } /// ``` pub fn vars() -> Vec<(String, String)> { environment::vars() } /// Returns true if the provided environment variable is defined and equals the provided value. /// /// # Arguments /// /// * `key` - The environment variable name /// * `value` - The value to check /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("MY_ENV_VAR", "SOME VALUE"); /// /// let same = envmnt::is_equal("MY_ENV_VAR", "SOME VALUE"); /// assert!(same); /// } /// ``` pub fn is_equal<K: AsRef<OsStr>>(key: K, value: &str) -> bool { environment::is_equal(key, value) } /// Sets all the provided env key/value pairs. /// /// # Arguments /// /// * `env` - The environment variables to set /// /// # Example /// /// ``` /// extern crate envmnt; /// extern crate indexmap; /// /// use indexmap::IndexMap; /// /// fn main() { /// let mut env: IndexMap<String, String> = IndexMap::new(); /// env.insert("MY_ENV_VAR".to_string(), "MY VALUE".to_string()); /// env.insert("MY_ENV_VAR2".to_string(), "MY VALUE2".to_string()); /// /// envmnt::set_all(&env); /// /// let mut value = envmnt::get_or_panic("MY_ENV_VAR"); /// assert_eq!(value, "MY VALUE"); /// value = envmnt::get_or_panic("MY_ENV_VAR2"); /// assert_eq!(value, "MY VALUE2"); /// } /// ``` pub fn set_all(env: &IndexMap<String, String>) { bulk::set_all(&env) } /// Sets all the provided env key/value pairs. /// /// # Arguments /// /// * `env` - The environment variables to set /// * `evaluate` - Evalute function which will modify the read value before it is loaded into the environment /// /// # Example /// /// ``` /// extern crate envmnt; /// extern crate indexmap; /// /// use indexmap::IndexMap; /// /// fn main() { /// let mut env: IndexMap<String, String> = IndexMap::new(); /// env.insert("MY_ENV_VAR".to_string(), "MY VALUE".to_string()); /// env.insert("MY_ENV_VAR2".to_string(), "MY VALUE2".to_string()); /// /// let eval_env = |value: String| { /// let mut buffer = String::from("VALUE-"); /// buffer.push_str(&value); /// buffer /// }; /// /// envmnt::evaluate_and_set_all(&env, eval_env); /// /// let mut value = envmnt::get_or_panic("MY_ENV_VAR"); /// assert_eq!(value, "VALUE-MY VALUE"); /// value = envmnt::get_or_panic("MY_ENV_VAR2"); /// assert_eq!(value, "VALUE-MY VALUE2"); /// } /// ``` pub fn evaluate_and_set_all<F>(env: &IndexMap<String, String>, evaluate: F) where F: Fn(String) -> String, { bulk::evaluate_and_set_all(&env, evaluate) } /// Returns true if any of environment variables is defined. /// /// # Arguments /// /// * `keys` - The environment variable names /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("ENV_VAR1", "SOME VALUE"); /// envmnt::remove("ENV_VAR2"); /// /// let found = envmnt::is_any_exists(&vec![ /// "ENV_VAR1", /// "ENV_VAR2", /// ]); /// /// assert!(found); /// } /// ``` pub fn is_any_exists<K: AsRef<OsStr>>(keys: &Vec<K>) -> bool { bulk::is_any_exists(keys) } /// Returns true if all of environment variables are defined. /// /// # Arguments /// /// * `keys` - The environment variable names /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// envmnt::set("ENV_VAR1", "SOME VALUE"); /// envmnt::set("ENV_VAR2", "SOME VALUE"); /// /// let mut found = envmnt::is_all_exists(&vec![ /// "ENV_VAR1", /// "ENV_VAR2", /// ]); /// /// assert!(found); /// /// envmnt::remove("ENV_VAR2"); /// /// found = envmnt::is_all_exists(&vec![ /// "ENV_VAR1", /// "ENV_VAR2", /// ]); /// /// assert!(!found); /// } /// ``` pub fn is_all_exists<K: AsRef<OsStr>>(keys: &Vec<K>) -> bool { bulk::is_all_exists(keys) } /// Parses the provided env file and loads all environment variables. /// /// # Arguments /// /// * `file` - The file path to load and parse /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// let output = envmnt::load_file("./src/test/var.env"); /// /// assert!(output.is_ok()); /// } /// ``` pub fn load_file(file: &str) -> Result<(), EnvmntError> { file::load_file(file) } /// Parses the provided env file and loads all environment variables. /// /// # Arguments /// /// * `file` - The file path to load and parse /// * `evaluate` - Evalute function which will modify the read value before it is loaded into the environment /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// let eval_env = |value: String| { /// let mut buffer = String::from("PREFIX-"); /// buffer.push_str(&value); /// buffer /// }; /// /// let output = envmnt::evaluate_and_load_file("./src/test/var.env", eval_env); /// /// assert!(output.is_ok()); /// } /// ``` pub fn evaluate_and_load_file<F>(file: &str, evaluate: F) -> Result<(), EnvmntError> where F: Fn(String) -> String, { file::evaluate_and_load_file(file, evaluate) } /// Parses the provided env file and returns its content as a map of key/value. /// /// # Arguments /// /// * `file` - The file path to load and parse /// /// # Example /// /// ``` /// extern crate envmnt; /// /// fn main() { /// let env = envmnt::parse_file("./src/test/var.env").unwrap(); /// /// println!("Parsed Env: {:?}", &env); /// } /// ``` pub fn parse_file(file: &str) -> Result<IndexMap<String, String>, EnvmntError> { file::parse_file(file) }