PerfectlySoft / Perfect

Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)
https://www.perfect.org
Apache License 2.0
13.83k stars 944 forks source link

Implementing Environmental Utility #272

Open RockfordWei opened 5 years ago

RockfordWei commented 5 years ago

Environmental Operations

Usage

First, ensure the PerfectLib is imported in your Swift file:

import PerfectLib

You are now able to use the Env class to operate the environmental variables

Set

This statement is equal to bash command "export foo=bar"

Env.set("foo", value: "bar")

It is also possible to set a group of variables in a dictionary style:

Env.set(["foo":"bar", "koo":"kar"])
// the result is identically the same as "export foo=bar && export koo=kar"

Get

guard let foo = Env.get("foo") else {
    // there is no such a variable
}
guard let foo = Env.get("foo", defaultValue: "bar") else {
    // there is no such a variable even with a default value??
}
let all = Env.get()
// the result of all is a dictionary [String: String]

Delete

Env.del("foo")
ThomasDeLange commented 5 years ago

This should be added👍🏼

CLAassistant commented 1 year ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

:white_check_mark: RockfordWei
:x: RockyCognizant
You have signed the CLA already but the status is still pending? Let us recheck it.