Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current operating system name in Elixir?

Tags:

elixir

In Ruby, I would use the RUBY_PLATFORM constant to determine what operating system (Mac, Windows, Linux etc) my program is running on. Does Elixir have a way to get this information?

I'm currently attempting to re-create a Ruby program I wrote in Elixir, and I have a method that will make an OS-dependent system call to open a document. The method looks something like:

def self.open_document(filename)
  case RUBY_PLATFORM
  when %r(darwin)
    system('open', filename)
  when %r(linux)
    system('xdg-open', filename)
  when %r(windows)
    system('cmd', '/c', "\"start #{filename}\"")
  else
    puts "Don't know how to open file"
  end
end

I know I can run the Ruby Kernel.system commands using Elixir's System.cmd/3, but I'm not sure how to get the RUBY_PLATFORM value equivalent to make the switch on in the case statement, or whether I can actually get that information. Is this possible?

Update

As per Lol4t0's answer and for further reference:

iex> :os.type
{:unix, :darwin}
iex> System.cmd("uname", ["-s"])
{"Darwin\n", 0}
like image 925
Paul Fioravanti Avatar asked Nov 01 '15 10:11

Paul Fioravanti


1 Answers

You can call Erlang os:type to get platform name info:

type() -> {Osfamily, Osname}

Types:

Osfamily = unix | win32
Osname = atom()

Returns the Osfamily and, in some cases, Osname of the current operating system.

On Unix, Osname will have same value as uname -s returns, but in lower case. For example, on Solaris 1 and 2, it will be sunos.

In Windows, Osname will be either nt (on Windows NT), or windows (on Windows 95).

In Elixir you probably have to call

:os.type()

to refer to that function with Osfamily being :unix or :win32.

like image 65
Lol4t0 Avatar answered Nov 09 '22 03:11

Lol4t0