Detect operating system in Clojure

2020-08-10 06:33发布

Is there an equivalent of Common Lisp's *features* in Clojure, so you can detect the OS and other environment configuration? Or do I just go through the Java API for that?

3条回答
够拽才男人
2楼-- · 2020-08-10 06:35

Probably use the Java API. It's easy enough, no sense re-inventing the wheel.

user> (System/getProperty "os.name")
"Linux"
user> (System/getProperty "os.version")
"2.6.36-ARCH"
user> (System/getProperty "os.arch")
"amd64"
查看更多
别忘想泡老子
3楼-- · 2020-08-10 06:43

To add to Brian Carper's answer, you could easily create a map of system properties via the Java API and bind it to the symbol features:

(def *features* {
  :name (System/getProperty "os.name"),
  :version (System/getProperty "os.version"),
  :arch (System/getProperty "os.arch")})

Which gives you this structure, for example:

{:name "Windows 7", :version "6.1", :arch "x86"}

Then access a property in any one of the following ways:

(:name *features*)
(*features* :name)
(get *features* :name)

Whichever floats your boat.

查看更多
对你真心纯属浪费
4楼-- · 2020-08-10 06:47

Other answers are handling how to get the system info from Java pretty well. If you want more help interpreting it, here are some examples of how Terracotta did that:

查看更多
登录 后发表回答