Skip to content

Instantly share code, notes, and snippets.

@coryt
Forked from iboard/shell.exs
Created April 10, 2020 04:25
Show Gist options
  • Select an option

  • Save coryt/00a12e73817334512263e2c9d4821795 to your computer and use it in GitHub Desktop.

Select an option

Save coryt/00a12e73817334512263e2c9d4821795 to your computer and use it in GitHub Desktop.

Revisions

  1. @iboard iboard revised this gist Feb 7, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions shell.exs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #! /usr/bin/env elixir
    #!/usr/bin/env elixir
    #
    # A Template for writing an Elixir script to be used on the
    # command-line.
    @@ -70,4 +70,4 @@ end
    #
    System.argv()
    |> Shell.main()
    |> IO.puts()
    |> IO.puts()
  2. @iboard iboard renamed this gist Jan 20, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @iboard iboard revised this gist Jan 20, 2019. 1 changed file with 0 additions and 61 deletions.
    61 changes: 0 additions & 61 deletions shell.exs
    Original file line number Diff line number Diff line change
    @@ -1,61 +0,0 @@
    #! /usr/bin/env elixir
    defmodule Shell do
    alias IO.{ANSI}

    #
    # Add your script here
    #
    @opts [
    strict: [
    cmd: :string,
    my_cmd: :string
    ],
    aliases: [
    c: :cmd,
    m: :my_cmd
    ]
    ]

    def my_script(str, _args) do
    str |> String.upcase()
    end

    #
    # Call your script here
    #
    def run(args) do
    args
    |> case do
    {[cmd: cmd], args} -> system(cmd, args)
    {[my_cmd: cmd], args} -> my_script(cmd, args)
    _ -> no_command()
    end
    end

    # Default
    def no_command(), do: red("ERROR: No command")

    # Example: Execute a system command. e.g. `-c ls`
    def system(cmd, args) do
    {result, _err} = System.cmd(cmd, args)

    [blue(cmd), red(inspect(args)), green(result |> String.trim())]
    |> Enum.join("\n")
    end

    def main(args) do
    OptionParser.parse!(args, @opts)
    |> run
    end

    defp blue(str), do: [ANSI.blue(), str, ANSI.reset()] |> Enum.join()
    defp green(str), do: [ANSI.green(), str, ANSI.reset()] |> Enum.join()
    defp red(str), do: [ANSI.red(), str, ANSI.reset()] |> Enum.join()
    end

    #
    # MAIN
    #
    System.argv()
    |> Shell.main()
    |> IO.puts()
  4. @iboard iboard revised this gist Jan 20, 2019. 1 changed file with 73 additions and 0 deletions.
    73 changes: 73 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    #! /usr/bin/env elixir
    #
    # A Template for writing an Elixir script to be used on the
    # command-line.
    #
    # (c) 2019 by Andreas Altendorfer <[email protected]>
    # License: Free to use without any warranty.
    #
    # Usage:
    # 1. Add your command to strict and aliases on @opts
    # 2. Implement your function (rename my_script() )
    # 3. Call your script in `run()` (line 38)
    #
    defmodule Shell do
    alias IO.{ANSI}

    #
    # Add your script here
    #
    @opts [
    strict: [
    cmd: :string,
    my_cmd: :string
    ],
    aliases: [
    c: :cmd,
    m: :my_cmd
    ]
    ]

    def my_script(str, _args) do
    str |> String.upcase()
    end

    #
    # Call your script here
    #
    def run(args) do
    args
    |> case do
    {[cmd: cmd], args} -> system(cmd, args)
    {[my_cmd: cmd], args} -> my_script(cmd, args)
    _ -> no_command()
    end
    end

    # Default
    def no_command(), do: red("ERROR: No command")

    # Example: Execute a system command. e.g. `-c ls`
    def system(cmd, args) do
    {result, _err} = System.cmd(cmd, args)

    [blue(cmd), red(inspect(args)), green(result |> String.trim())]
    |> Enum.join("\n")
    end

    def main(args) do
    OptionParser.parse!(args, @opts)
    |> run
    end

    defp blue(str), do: [ANSI.blue(), str, ANSI.reset()] |> Enum.join()
    defp green(str), do: [ANSI.green(), str, ANSI.reset()] |> Enum.join()
    defp red(str), do: [ANSI.red(), str, ANSI.reset()] |> Enum.join()
    end

    #
    # MAIN
    #
    System.argv()
    |> Shell.main()
    |> IO.puts()
  5. @iboard iboard created this gist Jan 20, 2019.
    61 changes: 61 additions & 0 deletions shell.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #! /usr/bin/env elixir
    defmodule Shell do
    alias IO.{ANSI}

    #
    # Add your script here
    #
    @opts [
    strict: [
    cmd: :string,
    my_cmd: :string
    ],
    aliases: [
    c: :cmd,
    m: :my_cmd
    ]
    ]

    def my_script(str, _args) do
    str |> String.upcase()
    end

    #
    # Call your script here
    #
    def run(args) do
    args
    |> case do
    {[cmd: cmd], args} -> system(cmd, args)
    {[my_cmd: cmd], args} -> my_script(cmd, args)
    _ -> no_command()
    end
    end

    # Default
    def no_command(), do: red("ERROR: No command")

    # Example: Execute a system command. e.g. `-c ls`
    def system(cmd, args) do
    {result, _err} = System.cmd(cmd, args)

    [blue(cmd), red(inspect(args)), green(result |> String.trim())]
    |> Enum.join("\n")
    end

    def main(args) do
    OptionParser.parse!(args, @opts)
    |> run
    end

    defp blue(str), do: [ANSI.blue(), str, ANSI.reset()] |> Enum.join()
    defp green(str), do: [ANSI.green(), str, ANSI.reset()] |> Enum.join()
    defp red(str), do: [ANSI.red(), str, ANSI.reset()] |> Enum.join()
    end

    #
    # MAIN
    #
    System.argv()
    |> Shell.main()
    |> IO.puts()