3

How do I get the environment name when using environ in Clojure? I mean, :dev, :test, etc. The reason for wanting this is to pass it to Yeller so when it displays errors it can tell me which environment they happened into. Errors in staging are treated differently than errors in production.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • 1
    What is the use case here? If it is to take different actions depending on the environment, perhaps adding specific items into the environment (e.g. :database-url, :nrepl-port, etc) may address the issue better. You can also add a key with the environment name into the environment itself. – ez121sl Oct 22 '15 at 19:31

2 Answers2

2

Environ only provides access to environment variables, you need to set them yourself. You can use lein-environ to set environment variables in your project.clj in different profiles. These profiles will be picked by leiningen and merged together, which you can then access from your code. For example, we have:

  :profiles {:dev {:resource-paths ["test-resources"]
                   :env            {:environment    "development"
                                    :db-host        "localhost"
                                    :port           5000}}}

In production we provide actual environment variables instead.

Daniel Compton
  • 13,878
  • 4
  • 40
  • 60
1

Just the same as you want to "Tell, don't ask" in your code, you need to just use the config options and let environ figure out what the right one is based on the environment. In the rare event that you actually need the environment name itself just put it in the :env map for each environment.

orend
  • 21
  • 3