class Xapixctl::SyncCli

Public Instance Methods

diff(dir) click to toggle source
# File lib/xapixctl/sync_cli.rb, line 106
def diff(dir)
  sync_path = SyncPath.new(shell, dir, prj_connection.resource_types_for_export, excluded_types)

  sync_path.load_resource('project') do |desc|
    desc['metadata']['id'] = prj_connection.project
    res_details = prj_connection.project_resource
    show_diff(desc, res_details)
  end

  sync_path.types_to_sync.each do |type|
    res_path = sync_path.resource_path(type)
    local_resource_ids = []
    remote_resource_ids = prj_connection.resource_ids(type)
    res_path.load_resources do |desc|
      resource_id = desc['metadata']['id']
      local_resource_ids << resource_id
      if remote_resource_ids.include?(resource_id)
        res_details = prj_connection.resource(type, desc['metadata']['id'])
        show_diff(desc, res_details)
      else
        say "v #{type} #{resource_id}"
      end
    end
    (remote_resource_ids - local_resource_ids).each do |resource_id|
      say "^ #{type} #{resource_id}"
    end
  end
end
from_dir(dir) click to toggle source
# File lib/xapixctl/sync_cli.rb, line 58
def from_dir(dir)
  sync_path = SyncPath.new(shell, dir, prj_connection.resource_types_for_export, excluded_types)

  sync_path.load_resource('project') do |desc|
    say "applying #{desc['kind']} #{desc.dig('metadata', 'id')} to #{prj_connection.project}"
    desc['metadata']['id'] = prj_connection.project
    prj_connection.organization.apply(desc)
  end

  outdated_resources = {}
  sync_path.types_to_sync.each do |type|
    res_path = sync_path.resource_path(type)
    updated_resource_ids = []
    res_path.load_resources do |desc|
      say "applying #{desc['kind']} #{desc.dig('metadata', 'id')}"
      updated_resource_ids += prj_connection.apply(desc)
    end
    outdated_resources[type] = prj_connection.resource_ids(type) - updated_resource_ids
  end

  outdated_resources.each do |type, resource_ids|
    resource_ids.each do |resource_id|
      say "removing #{type} #{resource_id}"
      prj_connection.delete(type, resource_id)
    end
  end
end
to_dir(dir) click to toggle source
# File lib/xapixctl/sync_cli.rb, line 27
def to_dir(dir)
  sync_path = SyncPath.new(shell, dir, prj_connection.resource_types_for_export, excluded_types)

  res_details = prj_connection.project_resource
  sync_path.write_file(generate_readme(res_details), 'README.md')
  sync_path.write_resource_yaml(res_details, 'project')

  sync_path.types_to_sync.each do |type|
    res_path = sync_path.resource_path(type)
    prj_connection.resource_ids(type).each do |res_id|
      res_details = prj_connection.resource(type, res_id)
      res_path.write_resource_yaml(res_details, res_id)
    end
    res_path.remove_outdated_resources
  end
  sync_path.update_excluded_types_file
end

Private Instance Methods

excluded_types() click to toggle source
# File lib/xapixctl/sync_cli.rb, line 226
def excluded_types
  excluded = options[:exclude_types] || []
  excluded += ['Credential'] unless options[:credentials]
  excluded
end
generate_readme(res_details) click to toggle source
# File lib/xapixctl/sync_cli.rb, line 232
    def generate_readme(res_details)
      <<~EOREADME
        # #{res_details.dig('definition', 'name')}
        #{res_details.dig('definition', 'description')}

        Project exported from #{File.join(prj_connection.public_project_url)} by xapixctl v#{Xapixctl::VERSION}.
      EOREADME
    end
show_diff(local, remote) click to toggle source
# File lib/xapixctl/sync_cli.rb, line 137
def show_diff(local, remote)
  kind, id = local['kind'], local['metadata']['id']
  local, remote = local.slice('definition'), remote.slice('definition')
  changed = local != remote
  status = changed ? "~" : "="
  say "#{status} #{kind} #{id}"
  return unless changed && options[:details]
  shell.indent do
    Hashdiff.diff(local, remote).each do |change|
      status = change[0].tr('+-', '^v')
      key = change[1]
      say "#{status} #{key}"
      shell.indent do
        case status
        when "~" then say "^ #{change[3]}"; say "v #{change[2]}"
        else say "#{status} #{change[2]}" if change[2]
        end
      end
    end
  end
end