Skip to content

Commit

Permalink
cli: add "amber exec" command for executing one-liners (amberframewor…
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Nov 10, 2017
1 parent 83a4005 commit 14359a6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/amber/cli/commands/exec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "cli"

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

class Exec < ::Cli::Command
command_name "exec"

class Options
arg "code", desc: "Crystal code or .cr file to execute within the application scope", required: false, default: nil
string ["-e", "--editor"], desc: "Prefered editor: [vim, nano, pico, etc], only used when no code or .cr file is specified", default: "vim"
end

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

def run
result = ""
if args.size > 0 && args.code
if args.code.ends_with?(".cr") && File.exists?(args.code)
result = `crystal eval 'require "../config/*"; require "./#{args.code}"'`
else
result = `crystal eval 'require "../config/*"; #{args.code}'`
end
else
system("#{options.editor} .amber_exec.cr")
if File.exists?(".amber_exec.cr")
result = `crystal eval 'require "../config/*"; require "./.amber_exec.cr"'`
File.delete(".amber_exec.cr")
end
end
if result.includes?("Error in line 1:") &&
result.includes?("while requiring \"../config/*\": can't find file '../config/*' relative to '.'")
puts "error: 'amber exec' can only be used within the root of a valid amber project"
else
puts result
end
end
end
end
end

0 comments on commit 14359a6

Please sign in to comment.