lib/rezept/actions.rb in rezept-0.3.1 vs lib/rezept/actions.rb in rezept-0.4.0

- old
+ new

@@ -34,13 +34,59 @@ def apply(options) @converter.set_options(options) @client.set_options(options) dry_run = options['dry_run'] ? '[Dry run] ' : '' - _apply_docs(@converter.dslfile_to_h(options['file']), @client.get_documents, dry_run) + local = @converter.dslfile_to_h(options['file']) + remote = @client.get_documents + + unless options['prefix'].nil? + prefix = Regexp.new("^#{Regexp.escape(options['prefix'])}") + local.reject!{|d| not d['name'] =~ prefix } + remote.reject!{|d| not d['name'] =~ prefix } + end + + _apply_docs(local, remote, dry_run) end + def _apply_docs(local, remote, dry_run) + local.each do |l| + l_ids = l.delete('account_ids') + r_ids = [] + r = _choice_by_name(remote, l['name']) + + if r.nil? + info("#{dry_run}Create the new document #{l['name'].inspect}") + @client.create_document(l) if dry_run.empty? + else + r_ids = r.delete('account_ids') + diff = Rezept::Utils.diff(@converter, r, l) + + if diff == "\n" + info("#{dry_run}No changes '#{l['name']}'") + else + warn("#{dry_run}Update the document #{l['name'].inspect}") + STDERR.puts diff + @client.version_up_document(l) if dry_run.empty? + end + end + + add_ids = l_ids - r_ids + del_ids = r_ids - l_ids + info("#{dry_run}Add permission of #{l['name'].inspect} to #{add_ids.join(', ')}") if add_ids.length > 0 + warn("#{dry_run}Remove permission of #{l['name'].inspect} from #{add_ids.join(', ')}") if del_ids.length > 0 + @client.modify_document_permission(l, add_ids, del_ids) if dry_run.empty? + end + + remote.each do |r| + if _choice_by_name(local, r['name']).nil? + warn("#{dry_run}Delete the document #{r['name'].inspect}") + @client.delete_document(r) if dry_run.empty? + end + end + end + def convert(options) @converter.set_options(options) fmt = 'unknown' @@ -215,46 +261,9 @@ end end end exit(1) if failure - end - - def _apply_docs(local, remote, dry_run) - local.each do |l| - l_ids = l.delete('account_ids') - r_ids = [] - r = _choice_by_name(remote, l['name']) - - if r.nil? - info("#{dry_run}Create the new document #{l['name'].inspect}") - @client.create_document(l) if dry_run.empty? - else - r_ids = r.delete('account_ids') - diff = Rezept::Utils.diff(@converter, r, l) - - if diff == "\n" - info("#{dry_run}No changes '#{l['name']}'") - else - warn("#{dry_run}Update the document #{l['name'].inspect}") - STDERR.puts diff - @client.version_up_document(l) if dry_run.empty? - end - end - - add_ids = l_ids - r_ids - del_ids = r_ids - l_ids - info("#{dry_run}Add permission of #{l['name'].inspect} to #{add_ids.join(', ')}") if add_ids.length > 0 - warn("#{dry_run}Remove permission of #{l['name'].inspect} from #{add_ids.join(', ')}") if del_ids.length > 0 - @client.modify_document_permission(l, add_ids, del_ids) if dry_run.empty? - end - - remote.each do |r| - if _choice_by_name(local, r['name']).nil? - warn("#{dry_run}Delete the document #{r['name'].inspect}") - @client.delete_document(r) if dry_run.empty? - end - end end def _choice_by_name(docs, name) docs.each do |d| return d if d['name'] == name