mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
[feature] Added os.delEnv; add delEnv support to nimscript too (#11466)
[feature] Fixes https://github.com/nim-lang/Nim/issues/11452.
This commit is contained in:
committed by
Andreas Rumpf
parent
eadea343ef
commit
7182922622
@@ -9,6 +9,8 @@ proc c_getenv(env: cstring): cstring {.
|
||||
importc: "getenv", header: "<stdlib.h>".}
|
||||
proc c_putenv(env: cstring): cint {.
|
||||
importc: "putenv", header: "<stdlib.h>".}
|
||||
proc c_unsetenv(env: cstring): cint {.
|
||||
importc: "unsetenv", header: "<stdlib.h>".}
|
||||
|
||||
# Environment handling cannot be put into RTL, because the ``envPairs``
|
||||
# iterator depends on ``environment``.
|
||||
@@ -109,6 +111,7 @@ proc getEnv*(key: string, default = ""): TaintedString {.tags: [ReadEnvEffect].}
|
||||
## See also:
|
||||
## * `existsEnv proc <#existsEnv,string>`_
|
||||
## * `putEnv proc <#putEnv,string,string>`_
|
||||
## * `delEnv proc <#delEnv,string>`_
|
||||
## * `envPairs iterator <#envPairs.i>`_
|
||||
runnableExamples:
|
||||
assert getEnv("unknownEnv") == ""
|
||||
@@ -132,6 +135,7 @@ proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
|
||||
## See also:
|
||||
## * `getEnv proc <#getEnv,string,string>`_
|
||||
## * `putEnv proc <#putEnv,string,string>`_
|
||||
## * `delEnv proc <#delEnv,string>`_
|
||||
## * `envPairs iterator <#envPairs.i>`_
|
||||
runnableExamples:
|
||||
assert not existsEnv("unknownEnv")
|
||||
@@ -149,6 +153,7 @@ proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
|
||||
## See also:
|
||||
## * `getEnv proc <#getEnv,string,string>`_
|
||||
## * `existsEnv proc <#existsEnv,string>`_
|
||||
## * `delEnv proc <#delEnv,string>`_
|
||||
## * `envPairs iterator <#envPairs.i>`_
|
||||
|
||||
# Note: by storing the string in the environment sequence,
|
||||
@@ -175,6 +180,31 @@ proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
|
||||
if c_putenv(environment[indx]) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
proc delEnv*(key: string) {.tags: [WriteEnvEffect].} =
|
||||
## Deletes the `environment variable`:idx: named `key`.
|
||||
## If an error occurs, `OSError` is raised.
|
||||
##
|
||||
## See also:ven
|
||||
## * `getEnv proc <#getEnv,string,string>`_
|
||||
## * `existsEnv proc <#existsEnv,string>`_
|
||||
## * `putEnv proc <#putEnv,string,string>`_
|
||||
## * `envPairs iterator <#envPairs.i>`_
|
||||
when nimvm:
|
||||
discard "built into the compiler"
|
||||
else:
|
||||
var indx = findEnvVar(key)
|
||||
if indx < 0: return # Do nothing if the env var is not already set
|
||||
when defined(windows) and not defined(nimscript):
|
||||
when useWinUnicode:
|
||||
var k = newWideCString(key)
|
||||
if setEnvironmentVariableW(k, nil) == 0'i32: raiseOSError(osLastError())
|
||||
else:
|
||||
if setEnvironmentVariableA(key, nil) == 0'i32: raiseOSError(osLastError())
|
||||
else:
|
||||
if c_unsetenv(key) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
environment.delete(indx)
|
||||
|
||||
iterator envPairs*(): tuple[key, value: TaintedString] {.tags: [ReadEnvEffect].} =
|
||||
## Iterate over all `environments variables`:idx:.
|
||||
##
|
||||
@@ -185,6 +215,7 @@ iterator envPairs*(): tuple[key, value: TaintedString] {.tags: [ReadEnvEffect].}
|
||||
## * `getEnv proc <#getEnv,string,string>`_
|
||||
## * `existsEnv proc <#existsEnv,string>`_
|
||||
## * `putEnv proc <#putEnv,string,string>`_
|
||||
## * `delEnv proc <#delEnv,string>`_
|
||||
getEnvVarsC()
|
||||
for i in 0..high(environment):
|
||||
var p = find(environment[i], '=')
|
||||
|
||||
@@ -114,15 +114,19 @@ proc cmpic*(a, b: string): int =
|
||||
cmpIgnoreCase(a, b)
|
||||
|
||||
proc getEnv*(key: string; default = ""): string {.tags: [ReadIOEffect].} =
|
||||
## Retrieves the environment variable of name `key`.
|
||||
## Retrieves the environment variable of name ``key``.
|
||||
builtin
|
||||
|
||||
proc existsEnv*(key: string): bool {.tags: [ReadIOEffect].} =
|
||||
## Checks for the existence of an environment variable named `key`.
|
||||
## Checks for the existence of an environment variable named ``key``.
|
||||
builtin
|
||||
|
||||
proc putEnv*(key, val: string) {.tags: [WriteIOEffect].} =
|
||||
## Sets the value of the environment variable named key to val.
|
||||
## Sets the value of the environment variable named ``key`` to ``val``.
|
||||
builtin
|
||||
|
||||
proc delEnv*(key: string) {.tags: [WriteIOEffect].} =
|
||||
## Deletes the environment variable named ``key``.
|
||||
builtin
|
||||
|
||||
proc fileExists*(filename: string): bool {.tags: [ReadIOEffect].} =
|
||||
|
||||
Reference in New Issue
Block a user