Skip to content

Commit

Permalink
Add unsetenv builtin (closes #140)
Browse files Browse the repository at this point in the history
  • Loading branch information
dundalek committed Jul 5, 2019
1 parent f7b393c commit 7da57fb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [master](https://github.com/dundalek/closh/compare/v0.4.0...master) (unreleased)

- Add unsetenv builtin ([#14](https://github.com/dundalek/closh/issues/140))

### Fixes

- Fix cd which broke with Java 11 ([#144](https://github.com/dundalek/closh/issues/144))
Expand Down
5 changes: 5 additions & 0 deletions src/common/closh/zero/builtin.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
(fn [[k v]] (process/setenv k v))
(partition 2 args))))

(defcmd unsetenv
"Unsets environment variables."
[& args]
(doall (map process/unsetenv args)))

(defcmd cd
"Changes current working directory to a path of a first given argument."
[& args]
Expand Down
2 changes: 1 addition & 1 deletion src/common/closh/zero/env.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'[closh.zero.compiler]
'[closh.zero.parser]
'[closh.zero.core :refer [shx expand]]
'[closh.zero.builtin :refer [cd exit quit getenv setenv]]
'[closh.zero.builtin :refer [cd exit quit getenv setenv unsetenv]]
'[closh.zero.platform.process]
'[closh.zero.pipeline]
'[clojure.string :as str]
Expand Down
4 changes: 2 additions & 2 deletions src/common/closh/zero/util.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns closh.zero.util
(:require [clojure.data :refer [diff]]
[closh.zero.platform.process :refer [shx setenv getenv]]
[closh.zero.platform.process :refer [shx setenv getenv unsetenv]]
[closh.zero.pipeline :refer [process-value]]
#?(:clj [clojure.data.json :as json])
#?@(:cljs [[fs] [tmp]])))
Expand Down Expand Up @@ -30,7 +30,7 @@
(let [var-diff (diff before after)
removed (remove #(ignore-env-vars (first %)) (first var-diff))
changed (remove #(ignore-env-vars (first %)) (second var-diff))]
(doseq [[k _] removed] (setenv k))
(doseq [[k _] removed] (unsetenv k))
(doseq [[k v] changed] (setenv k v))))

(defn source-shell
Expand Down
12 changes: 7 additions & 5 deletions src/jvm/closh/zero/platform/process.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
(reset! *cwd* target)
(throw (Exception. (str target ": Is not a directory"))))))

(defn setenv
([k] (swap! *env* dissoc k))
([k v] (let [val (str v)]
(swap! *env* assoc k val)
val)))
(defn setenv [k v]
(let [val (str v)]
(swap! *env* assoc k val)
val))

(defn unsetenv [k]
(swap! *env* dissoc k))

(defn getenv
([] (merge
Expand Down
10 changes: 6 additions & 4 deletions src/lumo/closh/zero/platform/process.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
(apply array (flatten args))
#js{:stdio (open-io-streams (:redir opts))})))

(defn setenv
([k] (js-delete js/process.env k))
([k v] (do (gobj/set js/process.env k v)
v)))
(defn setenv [k v]
(do (gobj/set js/process.env k v)
v))

(defn unsetenv [k]
(js-delete js/process.env k))

(defn getenv
([] (jsx->clj js/process.env))
Expand Down
8 changes: 6 additions & 2 deletions test/closh/util_test.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns closh.util-test
(:require [clojure.test :refer [deftest testing is are run-tests]]
[closh.zero.platform.process :refer [getenv setenv shx]]
[closh.zero.platform.process :refer [getenv setenv unsetenv shx]]
[closh.zero.pipeline :refer [pipeline-value]]
[closh.zero.util :refer [source-shell]]))

Expand All @@ -25,4 +25,8 @@
(getenv "B"))))

(is (= "hello\n" (do (setenv "B" "hello")
(pipeline-value (shx "bash" ["-c" "echo $B"]))))))
(pipeline-value (shx "bash" ["-c" "echo $B"])))))

(is (= nil (do (setenv "B" "hi")
(unsetenv "B")
(getenv "B")))))

0 comments on commit 7da57fb

Please sign in to comment.