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: add "amber exec" command for executing one-liners (#352) #371

Merged
merged 24 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
561ad25
cli: add "amber exec" command for executing one-liners (#352)
sam0x17 Nov 9, 2017
4c71900
this now behaves as close to a console as we can make it
elorest Nov 10, 2017
4824370
Merge branch 'master' into amber-exec
elorest Nov 10, 2017
2d8078a
finishing touches for amber exec
sam0x17 Nov 10, 2017
cf0f98b
amber exec specs
sam0x17 Nov 10, 2017
1457fda
cleaned up code a bit and copied existing file to new temp file for e…
elorest Nov 10, 2017
e57ebb6
Merge branch 'master' into amber-exec
elorest Nov 10, 2017
0f95033
specs pass yay. MainCommand.run always returns nil which makes testin…
elorest Nov 11, 2017
c33e4bd
Merge branch 'master' into amber-exec
elorest Nov 11, 2017
7242925
new push to see if travis passes. Pass locally so I'm confused.
elorest Nov 11, 2017
fedd29c
debugging to figure out why travis doesn't pass
elorest Nov 11, 2017
85b5fd6
added sleeps to see if that works
elorest Nov 11, 2017
0928fe6
one last thing
elorest Nov 11, 2017
4d20886
what the what?
elorest Nov 11, 2017
4672a3f
i hope this will clear it up
elorest Nov 11, 2017
ad812cc
weird hack that i think fixes specs on linux
elorest Nov 11, 2017
2737797
commented weird hack
elorest Nov 11, 2017
3ea29d5
native ls had better work
elorest Nov 11, 2017
491c71e
formatted and fixed tests probably
elorest Nov 11, 2017
e66e782
removed some vestigual code.
elorest Nov 11, 2017
5d3c3df
removed macro flag for now
elorest Nov 11, 2017
1a5e0ba
test47 of travis
elorest Nov 11, 2017
9f720f3
namespaces specs
elorest Nov 11, 2017
2b1bb15
sorted ls results
elorest Nov 11, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions spec/amber/cli/commands/exec_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# {% if flag?(:run_build_tests) %}
require "../../../spec_helper"
require "../../../support/helpers/cli_helper"

include CLIHelper

module Amber::CLI
describe "amber exec" do
context "within project" do
cleanup
scaffold_app(TESTING_APP)
`shards`

it "executes one-liners from the first command-line argument" do
expected_result = "[:a, :b, :c, :d]\n"
MainCommand.run(["exec", "[:a, :b, :c] + [:d]"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq expected_result
end

it "executes a .cr file from the first command-line argument" do
File.write "amber_exec_spec_test.cr", "puts([:a] + [:b])"
MainCommand.run(["exec", "amber_exec_spec_test.cr", "-e", "tail"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "[:a, :b]\n"
File.delete("amber_exec_spec_test.cr")
end

it "opens editor and executes .cr file on close" do
MainCommand.run(["exec", "-e", "echo 'puts 1000' > "])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "1000\n"
end

it "copies previous run into new file for editing and runs it returning results" do
MainCommand.run(["exec", "1337"])
MainCommand.run(["exec", "-e", "tail", "-b", "1"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq "1337\n"
end

cleanup
end

context "outside of project" do
it "complains if not in the root of a project" do
expected_result = "Error: 'amber exec' can only be used from the root of a valid amber project"
MainCommand.run(["exec", ":hello"])
logs = `ls tmp/*_console_result.log`.strip.split(/\s/).sort
File.read(logs.last?.to_s).should eq expected_result
end

cleanup
end
end
end
# {% end %}
4 changes: 0 additions & 4 deletions spec/amber/dsl/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require "../../../spec_helper"
module Amber
module DSL
describe Server do

describe "pipeline" do
context "generating a single ':custom' pipeline" do
server = Amber::Server.instance
Expand All @@ -26,7 +25,6 @@ module Amber
plugs.should_not be nil
(plugs.map(&.class).should eq expected) if plugs
end

end

context "generating a single pipeline with multiple calls" do
Expand Down Expand Up @@ -121,9 +119,7 @@ module Amber
(plugs.map(&.class).should eq expected) if plugs
end
end

end
end

end
end
53 changes: 53 additions & 0 deletions src/amber/cli/commands/exec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "cli"

module Amber::CLI
class MainCommand < ::Cli::Supercommand
command "x", aliased: "exec"

class Exec < ::Cli::Command
command_name "exec"
@filename = "./tmp/#{Time.now.epoch}_console.cr"

class Options
arg "code", desc: "Crystal code or .cr file to execute within the application scope", default: ""
string ["-e", "--editor"], desc: "Prefered editor: [vim, nano, pico, etc], only used when no code or .cr file is specified", default: "vim"
string ["-b", "--back"], desc: "Runs prevous command files: 'amber exec -b [times_ago]'", default: "0"
end

class Help
caption "# It runs Crystal code within the application scope"
end

def prepare_file
_filename = if File.exists?(args.code)
args.code
elsif options.back.to_i(strict: false) > 0
Dir.glob("./tmp/*_console.cr").reverse[options.back.to_i(strict: false) - 1]?
end

system("cp #{_filename} #{@filename}") if _filename
end

def run
Dir.mkdir("tmp") unless Dir.exists?("tmp")

unless args.code.blank? || File.exists?(args.code)
File.write(@filename, "puts (#{args.code}).inspect")
else
prepare_file
system("#{options.editor} #{@filename}")
end

result = ""
result = `crystal eval 'require "./config/*"; require "#{@filename}"'` if File.exists?(@filename)

if result.includes?("while requiring \"./config/*\": can't find file './config/*' relative to '.'")
result = "Error: 'amber exec' can only be used from the root of a valid amber project"
end

File.write(@filename.sub("console.cr", "console_result.log"), result) unless result.blank?
puts result
end
end
end
end
1 change: 1 addition & 0 deletions src/amber/cli/templates/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.crystal/
/.shards/
/.vscode/
/tmp/
.env
.amber_secret_key
production.yml
Expand Down
2 changes: 1 addition & 1 deletion src/amber/scripts/environment_loader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ str = String.build do |s|
s.puts %(@port_reuse = #{settings["port_reuse"]? == nil ? true : settings["port_reuse"]})
s.puts %(@process_count = #{settings["process_count"]? || 1})
s.puts %(@log = #{settings["log"]? || "::Logger.new(STDOUT)"}.tap{|l| l.level = #{settings["log_level"]? || "::Logger::INFO"}})
#s.puts %(@log.level = #{settings["log_level"]? || "::Logger::INFO"})
# s.puts %(@log.level = #{settings["log_level"]? || "::Logger::INFO"})
s.puts %(@color = #{settings["color"]? == nil ? true : settings["color"]})
s.puts %(@redis_url = "#{settings["redis_url"]? || "redis://localhost:6379"}")
s.puts %(@port = #{settings["port"]? || 3000})
Expand Down
2 changes: 1 addition & 1 deletion src/amber/server/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Amber
def self.instance
@@instance ||= new
end

# Configure should probably be deprecated in favor of settings.
def self.configure
with settings yield settings
Expand Down