Skip to content

Commit

Permalink
Take advantage of Forwardable module (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr authored Jul 23, 2024
1 parent 594b706 commit c9f6adf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/command/apply_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ApplyTemplate < Base # rubocop:disable Metrics/ClassLength
VALIDATIONS = %w[config templates].freeze

def call # rubocop:disable Metrics/MethodLength
@template_parser = TemplateParser.new(config)
@template_parser = TemplateParser.new(self)
@names_to_filenames = config.args.to_h do |name|
[name, @template_parser.template_filename(name)]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/command/doctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def call
validations = config.options[:validations].split(",")
ensure_required_options!(validations)

doctor_service = DoctorService.new(config)
doctor_service = DoctorService.new(self)
doctor_service.run_validations(validations)
end

Expand Down
14 changes: 6 additions & 8 deletions lib/core/doctor_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
class ValidationError < StandardError; end

class DoctorService
attr_reader :config
extend Forwardable

def initialize(config)
@config = config
def_delegators :@command, :config, :progress

def initialize(command)
@command = command
end

def run_validations(validations, silent_if_passing: false) # rubocop:disable Metrics/MethodLength
Expand Down Expand Up @@ -37,7 +39,7 @@ def validate_config
end

def validate_templates
@template_parser = TemplateParser.new(config)
@template_parser = TemplateParser.new(@command)
filenames = Dir.glob("#{@template_parser.template_dir}/*.yml")
templates = @template_parser.parse(filenames)

Expand Down Expand Up @@ -97,8 +99,4 @@ def warn_deprecated_template_variables
.join("\n")
progress.puts("\n#{Shell.color("DEPRECATED: #{message}", :yellow)}\n#{list}\n\n")
end

def progress
$stderr
end
end
2 changes: 0 additions & 2 deletions lib/core/maintenance_mode.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "forwardable"

class MaintenanceMode
extend Forwardable

Expand Down
14 changes: 7 additions & 7 deletions lib/core/template_parser.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# frozen_string_literal: true

class TemplateParser
attr_reader :config, :deprecated_variables
extend Forwardable

def initialize(config)
@config = config
def_delegators :@command, :config, :cp

attr_reader :deprecated_variables

def initialize(command)
@command = command
end

def template_dir
Expand Down Expand Up @@ -69,8 +73,4 @@ def new_variables
"APP_IMAGE" => "{{APP_IMAGE}}"
}
end

def cp
@cp ||= Controlplane.new(config)
end
end
7 changes: 5 additions & 2 deletions lib/cpflow.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "date"
require "forwardable"
require "dotenv/load"
require "cgi"
require "json"
Expand Down Expand Up @@ -226,12 +227,14 @@ def self.process_option_params(params)

Cpflow::Cli.show_info_header(config) if with_info_header

command = command_class.new(config)

if validations.any? && ENV.fetch("DISABLE_VALIDATIONS", nil) != "true"
doctor = DoctorService.new(config)
doctor = DoctorService.new(command)
doctor.run_validations(validations, silent_if_passing: true)
end

command_class.new(config).call
command.call
rescue RuntimeError => e
::Shell.abort(e.message)
end
Expand Down

0 comments on commit c9f6adf

Please sign in to comment.