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(shell, path) ⇒ ResourcePath

Returns a new instance of ResourcePath.



162
163
164
165
166
# File 'lib/xapixctl/sync_cli.rb', line 162

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

Instance Method Details

#load_resource(res_name, &block) ⇒ Object



190
191
192
# File 'lib/xapixctl/sync_cli.rb', line 190

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

#load_resources(&block) ⇒ Object



186
187
188
# File 'lib/xapixctl/sync_cli.rb', line 186

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

#remove_outdated_resourcesObject



194
195
196
197
198
199
# File 'lib/xapixctl/sync_cli.rb', line 194

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

#write_file(content, filename) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/xapixctl/sync_cli.rb', line 168

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)
  say "updated #{file}..."
  file
end

#write_resource_yaml(res_details, res_name) ⇒ Object



180
181
182
183
184
# File 'lib/xapixctl/sync_cli.rb', line 180

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