Class: Xapixctl::SyncCli::ResourcePath

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

Direct Known Subclasses

SyncPath

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ResourcePath

Returns a new instance of ResourcePath.



88
89
90
91
# File 'lib/xapixctl/sync_cli.rb', line 88

def initialize(path)
  @path = path
  @resource_files = []
end

Instance Method Details

#load_resource(res_name, &block) ⇒ Object



115
116
117
# File 'lib/xapixctl/sync_cli.rb', line 115

def load_resource(res_name, &block)
  Util.resources_from_file(@path.join("#{res_name}.yaml"), ignore_missing: false, &block)
end

#load_resources(&block) ⇒ Object



111
112
113
# File 'lib/xapixctl/sync_cli.rb', line 111

def load_resources(&block)
  Util.resources_from_file(@path, ignore_missing: true, &block)
end

#remove_outdated_resourcesObject



119
120
121
122
123
124
# File 'lib/xapixctl/sync_cli.rb', line 119

def remove_outdated_resources
  (@path.glob('*.yaml') - @resource_files).each do |outdated_file|
    outdated_file.delete
    puts "removed #{outdated_file}"
  end
end

#write_file(content, filename) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xapixctl/sync_cli.rb', line 93

def write_file(content, filename)
  @path.mkpath
  unless @path.directory? && @path.writable?
    warn "Cannot write to #{@path}, please check directory exists and is writable"
    exit 1
  end
  file = @path.join(filename)
  file.write(content)
  puts "updated #{file}..."
  file
end

#write_resource_yaml(res_details, res_name) ⇒ Object



105
106
107
108
109
# File 'lib/xapixctl/sync_cli.rb', line 105

def write_resource_yaml(res_details, res_name)
  file = write_file(res_details.to_yaml, "#{res_name}.yaml")
  @resource_files << file
  file
end