class ThreeScaleToolbox::Commands::ProductCommand::ExportSubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 7
def self.command
  Cri::Command.define do
    name        'export'
    usage       'export [opts] <remote> <product>'
    summary     'Export product to serialized format'
    description 'This command serializes the referenced product and associated backends into a yaml format'

    option      :f, :file, 'Write to file instead of stdout', argument: :required
    param       :remote
    param       :product_ref

    runner ExportSubcommand
  end
end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 22
def run
  select_output do |output|
    output.write(serialized_object.to_yaml)
  end
end

Private Instance Methods

file() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 75
def file
  options[:file]
end
find_product() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 69
def find_product
  Entities::Service.find(remote: remote, ref: product_ref).tap do |prd|
    raise ThreeScaleToolbox::Error, "Product #{product_ref} does not exist" if prd.nil?
  end
end
product() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 55
def product
  @product ||= find_product
end
product_backends() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 59
def product_backends
  product.backend_usage_list.map do |backend_usage|
    Entities::Backend.new(id: backend_usage.backend_id, remote: remote)
  end
end
product_ref() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 65
def product_ref
  arguments[:product_ref]
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 30
def remote
  @remote ||= threescale_client(arguments[:remote])
end
select_output() { |ios| ... } click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 42
def select_output
  ios = if file
          File.open(file, 'w')
        else
          $stdout
        end
  begin
    yield(ios)
  ensure
    ios.close
  end
end
serialized_object() click to toggle source
# File lib/3scale_toolbox/commands/product_command/export_command.rb, line 34
def serialized_object
  {
    'apiVersion' => 'v1',
    'kind' => 'List',
    'items' => [product.to_cr] + product_backends.map(&:to_cr)
  }
end