Class: Xapixctl::SyncCli

Inherits:
BaseCli
  • Object
show all
Defined in:
lib/xapixctl/sync_cli.rb

Defined Under Namespace

Classes: ResourcePath, SyncPath

Instance Method Summary collapse

Methods inherited from BaseCli

exit_on_failure?, start

Instance Method Details

#from_dir(dir) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xapixctl/sync_cli.rb', line 57

def from_dir(dir)
  sync_path = SyncPath.new(dir, prj_connection.resource_types_for_export, excluded_types)

  sync_path.load_resource('project') do |desc|
    puts "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|
      puts "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|
      puts "removing #{type} #{resource_id}"
      prj_connection.delete(type, resource_id)
    end
  end
end

#to_dir(dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xapixctl/sync_cli.rb', line 26

def to_dir(dir)
  sync_path = SyncPath.new(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