Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI updates #14

Merged
merged 21 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: crystal
install:
- shards install
- shards build

script:
- bin/ameba
- crystal tool format --check
- crystal spec
- crystal docs

Expand Down
2 changes: 1 addition & 1 deletion athena.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ routing:
cors:
enabled: false
strategy: blacklist
defaults:
defaults: &defaults
allow_origin: https://yourdomain.com
expose_headers: []
max_age: 0
Expand Down
24 changes: 20 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A command is created by defining a struct that inherits from `Athena::Cli::Comma
require "athena/cli"

struct MigrateEventsCommand < Athena::Cli::Command
self.command_name = "migrate:events"
self.name = "migrate:events"
self.description = "Migrates legacy events for a given customer"

def self.execute(customer_id : Int32, event_ids : Array(Int64)) : Nil
Expand All @@ -37,11 +37,27 @@ end

Then, after building the program.

```Text
./MyApp -c migrate:events --customer_id=83726 --event_ids=1,2,3,4,5
```bash
./MyApp -l
Registered commands:
migrate
migrate:events - Migrates legacy events for a given customer
./MyApp -c migrate:events --customer_id=83726 --event_ids 1,2,3,4,5
```

the `-l` or `--list` argument will list the available commands that can be executed via the binary. The commands are grouped based on the first part of the command name, separated by `:`. The `-e NAME` or `--explain NAME` can be used to get more detailed information about a given command.

Commands are executed by using the `--command NAME` or `-c NAME` syntax; where `NAME` is the name of the command. Arguments are passed via the `--key=value` or `--key value` format, where `key` matches the argument name from the `self.execute` method.

```bash
./MyApp -e migrate:events
Command
migrate:events - Migrates legacy events for a given customer
Usage
./YOUR_BINARY -c migrate:events [arguments]
Arguments
customer_id : Int32
event_ids : Array(Int64)
```

### Parameters
Expand All @@ -54,7 +70,7 @@ All primitive data types are supported including: `Int32`, `Bool`, `Float64`, e

### Required/Optional Parameters

Non-nilable parameters are considered required and will raise an exception if not supplied. Nilable parameters are considered optional and will be nil if not supplied.
Non-nilable parameters are considered required and will raise an exception if not supplied, without a default value. Nilable parameters are considered optional and will be nil if not supplied, without a default value.



6 changes: 6 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Athena takes a modular approach to its feature set. Each feature is encapsulated in its own module; and can be required independently of each other. This allows an application to only include what that application needs, without extra bloat.

## The `athena` executable

Upon install, Athena will build and add an `athena` executable to your projects `bin` directory. This binary can be used to run Athena related commands, all of which are listed in the [docs](<https://blacksmoke16.github.io/athena/Athena/Commands.html>).

## Modules

* [Routing](./routing.md) `require "athena/routing"` - _done_:
* [Defining routes](./routing.md#defining-routes)
* [Exception Handling](./routing.md#exception-handling)
Expand Down
11 changes: 7 additions & 4 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: athena

description: |
Annotation based JSON API framework with built in param conversion.
Modular, annotation based, API oriented framework with built in param conversion.

version: 0.4.0

Expand All @@ -13,11 +13,14 @@ crystal: 0.27.2
license: MIT

targets:
init:
main: src/config/init.cr
athena:
main: src/athena.cr

scripts:
postinstall: shards build --release --production && ./bin/init
postinstall: "shards build --release --production && ./bin/athena -c athena:generate:config_file --path=../../athena.yml"

executables:
- athena

dependencies:
CrSerializer:
Expand Down
72 changes: 72 additions & 0 deletions spec/athena_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "./spec_helper_methods"
require "../src/athena"

COMMANDS = <<-COMMANDS
Registered Commands:
\tathena
\t\tathena:generate:config_file - Generates the default config file for Athena\n
COMMANDS

EXPLAIN = <<-EXPLAIN
Command
\tathena:generate:config_file - Generates the default config file for Athena
Usage
\t./YOUR_BINARY -c athena:generate:config_file [arguments]
Arguments
\toverride : Bool = false
\tpath : String = "./athena.yml"\n
EXPLAIN

describe Athena do
describe "binary" do
describe "--list -l" do
it "should list avaliable commands" do
run_binary(args: ["-l"]) do |output|
output.should eq COMMANDS
end
end
end

describe "--explain -e" do
it "should print the help" do
run_binary(args: ["-e", "athena:generate:config_file"]) do |output|
output.should eq EXPLAIN
end
end
end

describe Athena::Commands do
describe "athena:generate:config_file" do
describe "when the config file already exists" do
it "should not recreate the file" do
created = File.info "athena.yml"
run_binary(args: ["-c", "athena:generate:config_file"]) do |_output|
modified = File.info "athena.yml"
created.modification_time.should eq modified.modification_time
end
end
end

describe "when using the override flag" do
it "should recreate the file" do
original = File.info "athena.yml"
run_binary(args: ["-c", "athena:generate:config_file", "--override=true"]) do |_output|
new = File.info "athena.yml"
(original.modification_time < new.modification_time).should be_true
end
end
end

describe "when using the path flag" do
it "should create the file at the given location" do
File.exists?("#{Dir.tempdir}/athena.yml").should be_false
run_binary(args: ["-c", "athena:generate:config_file", "--path=#{Dir.tempdir}/athena.yml"]) do |_output|
File.exists?("#{Dir.tempdir}/athena.yml").should be_true
File.delete("#{Dir.tempdir}/athena.yml")
end
end
end
end
end
end
end
3 changes: 1 addition & 2 deletions spec/cli/cli_spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "spec"

require "../spec_helper_methods"
require "../../src/cli"
require "./commands/*"

Expand Down
79 changes: 60 additions & 19 deletions spec/cli/command_spec.cr
Original file line number Diff line number Diff line change
@@ -1,55 +1,96 @@
require "./cli_spec_helper"

to_s = <<-TOS
Command
\tto_s - Command to test .to_s on
Usage
\t./YOUR_BINARY -c to_s [arguments]
Arguments
\toptional : (String | Nil)
\trequired : Bool
\tpath : String = "./"\n
TOS

describe Athena::Cli::Command do
describe "when parsing args from a command" do
describe "when there are none" do
it "should not try to parse arguments" do
NoParamsCommand.run_command([] of String).should eq "foo"
NoParamsCommand.run_command(["--id=123"]).should eq "foo"
NoParamsCommand.run_command(["--one=foo", "--three=3.14", "--two="]).should eq "foo"
end
end

describe "that are required" do
context "with one param" do
it "should convert correctly" do
CreateUserCommand.command.call(["--id=123"]).should eq 100
CreateUserCommand.run_command(["--id=123"]).should eq 100
CreateUserCommand.run_command(["--id 123"]).should eq 100
end

it "should raise if missing" do
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.command.call(["--id="]) }
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.command.call(["--id"]) }
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.command.call(["--i"]) }
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.run_command ["--id="] }
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.run_command ["--id"] }
expect_raises Exception, "Required argument 'id' was not supplied" { CreateUserCommand.run_command ["--i"] }
end
end

context "with multiple params" do
it "should convert correctly" do
MultiParamCommand.command.call(["--one=foo", "--three=3.14", "--two=8"]).should eq "foo is 11.14"
MultiParamCommand.run_command(["--one=foo", "--three=3.14", "--two=8"]).should eq "foo is 11.14"
MultiParamCommand.run_command(["--one=foo", "--three 3.14", "--two=8"]).should eq "foo is 11.14"
end

it "should raise if missing" do
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.command.call ["--one=foo", "--three=3.14", "--two="] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.command.call ["--one=foo", "--three=3.14", "--two"] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.command.call ["--one=foo", "--three=3.14", "--t"] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.command.call ["--one=foo", "--three=3.14"] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.run_command ["--one=foo", "--three=3.14", "--two="] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.run_command ["--one=foo", "--three=3.14", "--two"] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.run_command ["--one=foo", "--three=3.14", "--t"] }
expect_raises Exception, "Required argument 'two' was not supplied" { MultiParamCommand.run_command ["--one=foo", "--three=3.14"] }
end
end

context "with a default value" do
it "should use default value if no value is given" do
DefaultValueCommand.run_command([] of String).should eq "./"
end
end

context "without a default value" do
it "should use given value" do
DefaultValueCommand.run_command(["--path=/user/config"]).should eq "/user/config"
DefaultValueCommand.run_command(["--path /user/config"]).should eq "/user/config"
end
end

context "with an array param" do
it "should convert correctly" do
ArrayBoolCommand.command.call(["--bools=true,false,false,true"]).should eq [true, false, false, true]
ArrayBoolCommand.run_command(["--bools=true,false,false,true"]).should eq [true, false, false, true]
ArrayBoolCommand.run_command(["--bools true,false,false,true"]).should eq [true, false, false, true]
end

it "should raise if missing" do
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.command.call ["--bools="] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.command.call ["--bools"] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.command.call ["--bos"] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.command.call [] of String }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.run_command ["--bools="] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.run_command ["--bools"] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.run_command ["--bos"] }
expect_raises Exception, "Required argument 'bools' was not supplied" { ArrayBoolCommand.run_command [] of String }
end
end
end

describe "that are optional" do
it "should return nil if missing" do
OptionalParamCommand.command.call(["--u=123"]).should be_nil
OptionalParamCommand.command.call(["--u"]).should be_nil
OptionalParamCommand.command.call(["--"]).should be_nil
OptionalParamCommand.command.call(["--foo"]).should be_nil
OptionalParamCommand.command.call(["--g=1.2,1.1"]).should be_nil
OptionalParamCommand.run_command(["--u=123"]).should be_nil
OptionalParamCommand.run_command(["--u"]).should be_nil
OptionalParamCommand.run_command(["--"]).should be_nil
OptionalParamCommand.run_command(["--foo"]).should be_nil
OptionalParamCommand.run_command(["--g=1.2,1.1"]).should be_nil
end
end
end

describe ".to_s" do
it "should print correctly" do
ToSCommand.to_s.should eq to_s
end
end
end
2 changes: 1 addition & 1 deletion spec/cli/commands/array_bool_comand.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../cli_spec_helper"

struct ArrayBoolCommand < Athena::Cli::Command
self.command_name = "array"
self.name = "aa:array"
self.description = "Array of bools"

def self.execute(bools : Array(Bool)) : Array(Bool)
Expand Down
2 changes: 1 addition & 1 deletion spec/cli/commands/create_user_command.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../cli_spec_helper"

struct CreateUserCommand < Athena::Cli::Command
self.command_name = "user"
self.name = "user"
self.description = "Creates a user with the given id"

def self.execute(id : Int32) : Int32
Expand Down
11 changes: 11 additions & 0 deletions spec/cli/commands/default_value_command.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "../cli_spec_helper"

struct DefaultValueCommand < Athena::Cli::Command
self.name = "params:default"
self.description = "Required param with a default value"

def self.execute(path : String = "./") : String
path.should be_a(String)
path
end
end
2 changes: 1 addition & 1 deletion spec/cli/commands/multi_param_command.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../cli_spec_helper"

struct MultiParamCommand < Athena::Cli::Command
self.command_name = "multi"
self.name = "params:multi"
self.description = "Has multiple required params"

def self.execute(one : String, two : Int8, three : Float64) : String
Expand Down
10 changes: 10 additions & 0 deletions spec/cli/commands/no_params_command.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "../cli_spec_helper"

struct NoParamsCommand < Athena::Cli::Command
self.name = "no_params"
self.description = "No params"

def self.execute : String
"foo"
end
end
2 changes: 1 addition & 1 deletion spec/cli/commands/optional_param_command.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../cli_spec_helper"

struct OptionalParamCommand < Athena::Cli::Command
self.command_name = "optional"
self.name = "params:optional"
self.description = "optional string"

def self.execute(u : String?, g : Array(Float32)?) : Nil
Expand Down
10 changes: 10 additions & 0 deletions spec/cli/commands/to_s_command.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "../cli_spec_helper"

struct ToSCommand < Athena::Cli::Command
self.name = "to_s"
self.description = "Command to test .to_s on"

def self.execute(optional : String?, required : Bool, path : String = "./") : String
path
end
end
9 changes: 9 additions & 0 deletions spec/cli/compile_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./cli_spec_helper"

describe Athena::Cli do
describe "with a command that does not have an .execute method" do
it "should not compile" do
assert_error "cli/compiler/no_execute.cr", "NoExecuteCommand must implement a `self.execute` method."
end
end
end
6 changes: 6 additions & 0 deletions spec/cli/compiler/no_execute.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "../cli_spec_helper"

struct NoExecuteCommand < Athena::Cli::Command
self.name = "no:execute"
self.description = "Command with no execute method"
end
Loading