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

Change confusing puts to info and add error logger #796

Merged
merged 14 commits into from
May 27, 2018
6 changes: 5 additions & 1 deletion src/amber/cli/command.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
abstract class Command < Cli::Command
def puts(msg)
def info(msg)
Amber::CLI.logger.info msg, Class.name, :light_cyan
end

def error(msg)
Amber::CLI.logger.error msg, Class.name, :red
end
end
2 changes: 1 addition & 1 deletion src/amber/cli/commands/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module Amber::CLI
Micrate::DB.connection_url = url.gsub(path, "/#{uri.scheme}")
return path.gsub("/", "")
else
CLI.logger.info "Could not determine database name", "Error", :red
error "Could not determine database name"
end
end

Expand Down
5 changes: 3 additions & 2 deletions src/amber/cli/commands/exec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Amber::CLI
arg "code", desc: "Crystal code or .cr file to execute within the application scope", default: ""
string ["-e", "--editor"], desc: "Preferred editor: [vim, nano, pico, etc], only used when no code or .cr file is specified", default: "vim"
string ["-b", "--back"], desc: "Runs previous command files: 'amber exec -b [times_ago]'", default: "0"
bool "--no-color", desc: "Disable colored output", default: false
help
end

Expand All @@ -35,7 +36,7 @@ module Amber::CLI
File.open(@filelogs, "r") do |file|
loop do
output = file.gets_to_end
STDOUT.puts output unless output.empty?
puts output unless output.empty?
sleep 1.millisecond
end
end
Expand All @@ -58,8 +59,8 @@ module Amber::CLI
end

def run
CLI.toggle_colors(options.no_color?)
exit_code = 0

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

if args.code.blank? || File.exists?(args.code)
Expand Down
3 changes: 2 additions & 1 deletion src/amber/cli/commands/generate.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Amber::CLI
end

def run
CLI.toggle_colors(options.no_color?)
if args.type == "error"
template = Template.new("error", ".")
else
Expand All @@ -39,7 +40,7 @@ module Amber::CLI

private def ensure_name_argument!
unless args.name?
CLI.logger.info "Parsing Error: The NAME argument is required.", "Error", :red
error "Parsing Error: The NAME argument is required."
exit! help: true, error: true
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/amber/cli/commands/new.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ module Amber::CLI
end

def run
Amber::CLI.color = !options.no_color?
CLI.toggle_colors(options.no_color?)
full_path_name = File.join(Dir.current, args.name)
if full_path_name =~ /\s+/
CLI.logger.error "Path and project name can't contain a space."
CLI.logger.error "Replace spaces with underscores or dashes."
CLI.logger.error "#{full_path_name} should be #{full_path_name.gsub(/\s+/, "_")}"
exit 1
error "Path and project name can't contain a space."
info "Replace spaces with underscores or dashes."
info "#{full_path_name} should be #{full_path_name.gsub(/\s+/, "_")}"
exit! error: true
end
name = File.basename(args.name)

Expand Down
15 changes: 8 additions & 7 deletions src/amber/cli/commands/pipelines.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ module Amber::CLI
command_name "pipelines"

def run
CLI.toggle_colors(options.no_color?)
parse_routes
print_pipelines
rescue ex : BadRoutesException
CLI.logger.error(ex.message.colorize(:red))
CLI.logger.error "Good bye :("
exit 1
error ex.message
info "Good bye :("
exit! error: true
rescue ex
CLI.logger.error "Error: Not valid project root directory.".colorize(:red)
CLI.logger.error "Run `amber pipelines` in project root directory.".colorize(:light_blue)
CLI.logger.error "Good bye :("
exit 1
error "Error: Not valid project root directory."
info "Run `amber pipelines` in project root directory."
info "Good bye :("
exit! error: true
end

private def parse_routes
Expand Down
9 changes: 5 additions & 4 deletions src/amber/cli/commands/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ module Amber::CLI
end

def run
CLI.toggle_colors(options.no_color?)
parse_routes
print_routes_table
rescue
puts "Error: Not valid project root directory.".colorize(:red)
puts "Run `amber routes` in project root directory.".colorize(:light_blue)
puts "Good bye :("
exit 1
error "Not valid project root directory."
info "Run `amber routes` in project root directory."
info "Good bye :("
exit! error: true
end

private def parse_routes
Expand Down
22 changes: 14 additions & 8 deletions src/amber/cli/recipes/recipe.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module Amber::Recipes
if name.match(/\A[a-zA-Z]/)
@name = name.underscore
else
raise "Name is not valid."
error "Name is not valid."
exit 1
end

@directory = File.join(directory)
Expand All @@ -53,21 +54,21 @@ module Amber::Recipes
case template
when "app"
if options
log_message "Rendering App #{name} in #{directory} from #{options.r}"
info "Rendering App #{name} in #{directory} from #{options.r}"
App.new(name, options.d, options.t, options.m, options.r).render(directory, list: true, color: true)
if options.deps?
log_message "Installing Dependencies"
info "Installing Dependencies"
Amber::CLI::Helpers.run("cd #{name} && shards update")
end
end
when "controller"
log_message "Rendering Controller #{name} from #{@recipe}"
info "Rendering Controller #{name} from #{@recipe}"
Controller.new(name, @recipe, @fields).render(directory, list: true, color: true)
when "model"
log_message "Rendering Model #{name} from #{@recipe}"
info "Rendering Model #{name} from #{@recipe}"
Model.new(name, @recipe, @fields).render(directory, list: true, color: true)
when "scaffold"
log_message "Rendering Scaffold #{name} from #{@recipe}"
info "Rendering Scaffold #{name} from #{@recipe}"
if model == "crecto"
Amber::CLI::CrectoMigration.new(name, @fields).render(directory, list: true, color: true)
else
Expand All @@ -77,17 +78,22 @@ module Amber::Recipes
Scaffold::Controller.new(name, @recipe, @fields).render(directory, list: true, color: true)
Scaffold::View.new(name, @recipe, @fields).render(directory, list: true, color: true)
else
CLI.logger.error "Template not found", "Generate", :light_red
error "Template not found"
exit 1
end
end

def model
CLI.config.model
end

def log_message(msg)
def info(msg)
CLI.logger.info msg, "Generate", :light_cyan
end

def error(msg)
CLI.logger.error msg, "Generate", :light_red
end
end
end

Expand Down
34 changes: 20 additions & 14 deletions src/amber/cli/templates/template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module Amber::CLI
if name.match(/\A[a-zA-Z]/)
@name = name.underscore
else
raise "Name is not valid."
error "Name is not valid."
exit 1
end

@directory = File.join(directory)
Expand All @@ -43,18 +44,18 @@ module Amber::CLI
case template
when "app"
if options
puts "Rendering App #{name} in #{directory}"
info "Rendering App #{name} in #{directory}"
App.new(name, options.d, options.t, options.m).render(directory, list: true, color: true)
if options.deps?
puts "Installing Dependencies"
info "Installing Dependencies"
Helpers.run("cd #{name} && shards update")
end
end
when "migration"
puts "Rendering Migration #{name}"
info "Rendering Migration #{name}"
Migration.new(name, fields).render(directory, list: true, color: true)
when "model"
puts "Rendering Model #{name}"
info "Rendering Model #{name}"
if model == "crecto"
CrectoMigration.new(name, fields).render(directory, list: true, color: true)
CrectoModel.new(name, fields).render(directory, list: true, color: true)
Expand All @@ -63,10 +64,10 @@ module Amber::CLI
GraniteModel.new(name, fields).render(directory, list: true, color: true)
end
when "controller"
puts "Rendering Controller #{name}"
info "Rendering Controller #{name}"
Controller.new(name, fields).render(directory, list: true, color: true)
when "scaffold"
puts "Rendering Scaffold #{name}"
info "Rendering Scaffold #{name}"
if model == "crecto"
CrectoMigration.new(name, fields).render(directory, list: true, color: true)
CrectoModel.new(name, fields).render(directory, list: true, color: true)
Expand All @@ -78,27 +79,28 @@ module Amber::CLI
end
Scaffold::View.new(name, fields).render(directory, list: true, color: true)
when "mailer"
puts "Rendering Mailer #{name}"
info "Rendering Mailer #{name}"
Mailer.new(name, fields).render(directory, list: true, color: true)
when "socket"
puts "Rendering Socket #{name}"
info "Rendering Socket #{name}"
if fields != [] of String
fields.each do |field|
WebSocketChannel.new(field).render(directory, list: true, color: true)
end
end
WebSocket.new(name, fields).render(directory, list: true, color: true)
when "channel"
puts "Rendering Channel #{name}"
info "Rendering Channel #{name}"
WebSocketChannel.new(name).render(directory, list: true, color: true)
when "auth"
puts "Rendering Auth #{name}"
info "Rendering Auth #{name}"
if model == "crecto"
raise "Auth not supported for crecto yet"
error "Auth not supported for crecto yet"
exit 1
end
Auth.new(name, fields).render(directory, list: true, color: true)
when "error"
puts "Rendering Error Template"
info "Rendering Error Template"
actions = ["forbidden", "not_found", "internal_server_error"]
ErrorTemplate.new("error", actions).render(directory, list: true, color: true)
else
Expand All @@ -110,9 +112,13 @@ module Amber::CLI
CLI.config.model
end

def puts(msg)
def info(msg)
CLI.logger.info msg, "Generate", :light_cyan
end

def error(msg)
CLI.logger.error msg, "Generate", :red
end
end
end

Expand Down