Skip to content

Commit

Permalink
Merge pull request #30 from AnilRedshift/refactor-commands
Browse files Browse the repository at this point in the history
Refactor commands
  • Loading branch information
Anil Kulkarni committed Jul 13, 2018
2 parents 887cafe + f1b8384 commit 1f68885
Show file tree
Hide file tree
Showing 45 changed files with 1,175 additions and 720 deletions.
27 changes: 15 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,44 @@ jobs:
working_directory: ~/wand

steps:
- checkout
- run: mix local.hex --force
- run: mix local.rebar --force
- run: mix archive.install hex wand_core --force
- checkout

- restore_cache:
keys:
- v1-mix-cache-{{ checksum "mix.lock" }}
- v1-mix-cache-{{ .Branch }}
- v1-mix-cache
- v2-mix-cache-{{ checksum "mix.lock" }}
- v2-mix-cache-{{ .Branch }}
- v2-mix-cache
- restore_cache:
keys:
- v1-build-cache-{{ .Branch }}
- v1-build-cache
- v2-build-cache-{{ .Branch }}
- v2-build-cache
- run: mix do deps.get, compile
- save_cache:
key: v1-mix-cache-{{ checksum "mix.lock" }}
key: v2-mix-cache-{{ checksum "mix.lock" }}
paths: "deps"
- save_cache:
key: v1-mix-cache-{{ .Branch }}
key: v2-mix-cache-{{ .Branch }}
paths: "deps"
- save_cache:
key: v1-mix-cache
key: v2-mix-cache
paths: "deps"
- save_cache:
key: v1-mix-cache-{{ checksum "mix.lock" }}
key: v2-mix-cache-{{ checksum "mix.lock" }}
paths: "_build"
- save_cache:
key: v1-build-cache-{{ .Branch }}
key: v2-build-cache-{{ .Branch }}
paths: "_build"
- save_cache:
key: v1-build-cache
key: v2-build-cache
paths: "_build"

- run: mix test
- run: mix coveralls.circle
- run: MIX_ENV=prod mix compile
- run: mix test --only external

- store_test_results:
path: _build/test/lib/wand
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ wand-*.tar

# Ignore the locally built escript
/wand

# Ignore files made by integration tests
/tmp
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ You need to have the wand_core archive added to your image before running mix de
`- run: mix archive.install hex wand_core --force`

## Local development
1. `git clone git@github.com:AnilRedshift/wand.git`
2. `cd wand`
3. `mix deps.get`
4. `mix test`
1. `mix archive.install hex wand_core --force`
2. `git clone git@github.com:AnilRedshift/wand.git`
3. `cd wand`
4. `mix deps.get`
5. `mix test`

## Integration tests
Wand also has tests which run the wand binary against real mix projects to verify their behavior. You can run these with `mix test --include external`

Additionally, you can see the CLI output of each command with `mix test --include external --include print`


# Build status
Expand Down
2 changes: 0 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ if Mix.env() == :test do
http: Wand.HttpMock,
io: Wand.IOMock,
system: Wand.SystemMock

config :wand_core, file: WandCore.FileMock
end
22 changes: 7 additions & 15 deletions lib/cli.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Wand.CLI do
alias Wand.CLI.Display
alias Wand.CLI.Executor
@system Wand.Interfaces.System.impl()
@moduledoc """
The main entrypoint for the wand escript
Expand All @@ -12,25 +12,17 @@ defmodule Wand.CLI do
end

defp route({key, data}) do
case Wand.CLI.Command.route(key, :execute, [data]) do
:ok ->
Display.success("Succeeded!")
:ok
module = Wand.CLI.Command.get_module(key)

{:ok, :silent} ->
:ok

{:ok, message} ->
Display.success(message)
:ok

{:error, code} ->
@system.halt(code)
case Executor.run(module, data) do
:ok -> :ok
{:error, code} -> @system.halt(code)
end
end

defp route({:help, key, data}) do
Wand.CLI.Command.route(key, :help, [data])
module = Wand.CLI.Command.get_module(key)
module.help(data)
@system.halt(1)
end
end
5 changes: 3 additions & 2 deletions lib/cli/arg_parser.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule Wand.CLI.ArgParser do
@moduledoc false
alias Wand.CLI.Command

def parse(args) do
global_flags = [
Expand Down Expand Up @@ -32,7 +31,9 @@ defmodule Wand.CLI.ArgParser do
defp parse_main(_args, [command | _rest]), do: {:help, :help, {:unrecognized, command}}

defp validate(key, args) do
case Command.route(key, :validate, [args]) do
module = Wand.CLI.Command.get_module(key)

case module.validate(args) do
{:ok, response} -> {key, response}
{:error, reason} -> {:help, key, reason}
{:help, module, reason} -> {:help, module, reason}
Expand Down
41 changes: 31 additions & 10 deletions lib/cli/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,36 @@ defmodule Wand.CLI.Command do
4. Update the help file in `Wand` with the appropriate text
"""

@callback execute(data :: any()) :: :ok | {:error, integer()}
@type ok_or_exit :: :ok | {:error, integer()}
@callback after_save(data :: any()) :: ok_or_exit
@callback execute(data :: any(), extras :: map()) :: ok_or_exit
@callback handle_error(type :: atom, data :: any()) :: String.t()
@callback help(type :: any()) :: any()
@callback options() :: keyword()
@callback validate(args :: list) :: {:ok, any()} | {:error, any()}

defmacro __using__(_opts) do
quote do
alias Wand.CLI.Executor.Result
@behaviour Wand.CLI.Command
@impl true
def after_save(_data), do: :ok

@impl true
def options(), do: []

@impl true
def handle_error(key, _data) do
"""
Error: An unexpected error has occured
The reason is: #{key}
"""
end

defoverridable Wand.CLI.Command
end
end

def routes() do
[
"add",
Expand All @@ -26,20 +52,15 @@ defmodule Wand.CLI.Command do
]
end

def route(key, name, args) do
get_module(key)
|> Kernel.apply(name, args)
def get_module(name) when is_atom(name), do: get_module(to_string(name))

def get_module(name) do
Module.concat(Wand.CLI.Commands, String.capitalize(name))
end

def parse_errors([]), do: :ok

def parse_errors([{flag, _} | _rest]) do
{:error, {:invalid_flag, flag}}
end

defp get_module(name) when is_atom(name), do: get_module(to_string(name))

defp get_module(name) do
Module.concat(Wand.CLI.Commands, String.capitalize(name))
end
end
26 changes: 24 additions & 2 deletions lib/cli/commands/add.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Wand.CLI.Commands.Add do
@behaviour Wand.CLI.Command
use Wand.CLI.Command

@moduledoc """
# Add
Add elixir packages to wand.json
Expand Down Expand Up @@ -133,12 +134,33 @@ defmodule Wand.CLI.Commands.Add do

@doc false
def moduledoc(), do: @moduledoc

@doc false
@impl true
def help(type), do: Wand.CLI.Commands.Add.Help.help(type)

@doc false
@impl true
def options() do
[
require_core: true,
load_wand_file: true
]
end

@doc false
@impl true
def validate(args), do: Wand.CLI.Commands.Add.Validate.validate(args)

@doc false
def execute(packages), do: Wand.CLI.Commands.Add.Execute.execute(packages)
@impl true
def execute(packages, extras), do: Wand.CLI.Commands.Add.Execute.execute(packages, extras)

@doc false
@impl true
def after_save(packages), do: Wand.CLI.Commands.Add.Execute.after_save(packages)

@doc false
@impl true
def handle_error(key, data), do: Wand.CLI.Commands.Add.Error.handle_error(key, data)
end

0 comments on commit 1f68885

Please sign in to comment.