Hi all,
Recently I've been working on some utility Elixir scripts. One of the scripts I was working on was a "Work In Progress" script for checking my current progress into git. The thing that I ran across that was interesting to me is (I hope) demonstrated by the code below:
# We may want to memoize this at some point in the future but it hardly seems worth the extra complication right now.
defp find_exe(command_to_find, default_command) do
case {command, _} = System.cmd get_correct_search_command(),[command_to_find] do
{_,1} -> default_command
{_,0} -> String.trim(command,get_correct_line_ending())
end
end
defp get_correct_line_ending() do
case {_osfamily, _} = :os.type() do
{:unix,_} -> "\n"
{:win32,_} -> "\r\n"
{:ose,_} -> "\n"
{_,_} -> "\n" # This should not ever get hit
end
end
defp get_correct_search_command() do
case {_osfamily, _} = :os.type() do
{os,_} when os in [:unix,:ose] -> "which"
{:win32,_} -> "where"
end
end
I'm trying to find the git command on the path. As far as I know on Linux and OSX that's "which" and on Windows it's "where". But the thing is "where" returns the string with a "\r\n" at the end. I can trim this but, of course, I need to figure out the right line ending for the OS I'm currently on. I confess I didn't try this on Linux or Mac to see if "which" behaves the same or not but I can see some utility to knowing which line-ending is present on your current OS as part of the standard System module.
I don't think there's anything in the System module to simply get the correct line-ending for the current OS--at least I couldn't find it. I could swear that Jose mentioned to me once that there's something internally to get the correct line ending for the current OS. Could we maybe expose the current correct line ending? Maybe System.os_specific_line_ending or something of that sort? As I say, I recall Jose mentioning something to me about this in the past and if we've already discussed it and decided against it, I apologize. I didn't turn up anything on a search of the archives.
Does this seem like a good idea? Thoughts? Criticisms? :)
oc