Skip to content

Instantly share code, notes, and snippets.

@nivertech
Forked from rob-brown/ExecuteSigil.ex
Created October 31, 2013 01:07
Show Gist options
  • Select an option

  • Save nivertech/7242965 to your computer and use it in GitHub Desktop.

Select an option

Save nivertech/7242965 to your computer and use it in GitHub Desktop.
defmodule CommandSigil do
# 'x' is for 'execute'.
defmacro sigil_x({ :<<>>, _line, [string] }, []) when is_binary(string) do
string
|> Macro.unescape_string()
|> String.to_char_list!()
|> :os.cmd()
end
defmacro sigil_x({ :<<>>, line, pieces }, []) do
binary = { :<<>>, line, Macro.unescape_tokens(pieces) }
quote do
unquote(binary)
|> String.to_char_list!()
|> :os.cmd()
end
end
defmacro sigil_X({ :<<>>, _line, [string] }, []) when is_binary(string) do
string
|> String.to_char_list!()
|> :os.cmd()
end
end
defmodule CommandSigil.Test do
import CommandSigil
def test_sigil_with_var do
dir = "~/"
%x"ls -al #{dir}/*"
end
def test_sigil_interpolated do
%x"echo Hello \x45\x6c\x69\x78\x69\x72\x21" # => 'Hello Elixir!\n'
end
def test_sigil_non_interpolated do
%X"echo Hello \x45\x6c\x69\x78\x69\x72\x21" # => 'Hello x45x6cx69x78x69x72x21\n'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment