How to read project.clj file.

434 views
Skip to first unread message

mmwaikar

unread,
Aug 9, 2011, 3:22:06 AM8/9/11
to clo...@googlegroups.com
Hi,

Assuming there are some DB credentials specified in a project.clj file as a map, how should I read those credentials in my clojure code -

1) should I use slurp and then parse that text?
2) should I (use 'leiningen.core) and then (load-file "project.clj")
3) or something else

Please let me know the best / idiomatic way to accomplish this.

Thanks and regards,
Manoj.

Shantanu Kumar

unread,
Aug 9, 2011, 5:35:03 AM8/9/11
to Clojure
project.clj is available to Leiningen plugins. Assuming you are open
to put the DB credentials elsewhere, I'd suggest putting it in another
file that can be easily discoverable. This idea is implemented in Clj-
DBCP:

https://bitbucket.org/kumarshantanu/clj-dbcp/overview (jump to the
section "Create DataSource from `.properties` file")

Regards,
Shantanu

Daniel E. Renfer

unread,
Aug 9, 2011, 9:35:10 AM8/9/11
to clo...@googlegroups.com

When it comes to loading config options, I always use my Ciste library
which has the ciste.config namespace. By default, it looks for a file
named "config.clj" in the root of the project.

config.clj contains a map with the keys being the environment names and
the values are maps of config options. It needs a bit more work, but it
serves my purposes.

The config function will either return the config map, or it will look
up the passed params in the map.

(use 'ciste.config)

(environment) => :development

(load-config)

(config :database :username) => "root"

Even if you don't use Ciste (there's a lot of stuff in there that may
not be relevant to your project) The config ns should at least get you
started.
https://github.com/duck1123/ciste/blob/master/src/main/clojure/ciste/config.clj


https://github.com/duck1123/ciste

signature.asc

mmwaikar

unread,
Aug 10, 2011, 9:09:32 AM8/10/11
to clo...@googlegroups.com
Thanks Shantanu and Daniel.

I am trying to merge some functionality from two different projects, so the credentials could be stored in properties file or the project.clj file. And mine is not a leiningen plugin so I parsed the project.clj file.
Reply all
Reply to author
Forward
0 new messages