lib/doggy/cli/pull.rb in doggy-2.0.17 vs lib/doggy/cli/pull.rb in doggy-2.0.18

- old
+ new

@@ -1,39 +1,63 @@ # encoding: utf-8 module Doggy class CLI::Pull - def initialize(options, ids) - @options = options - @ids = ids - @updated_by_last_action = false + def initialize(options, ids_or_names) + @options = options + @ids_or_names = ids_or_names end def run - pull_resources('dashboards', Models::Dashboard, @ids) if should_pull?('dashboards') - pull_resources('monitors', Models::Monitor, @ids) if should_pull?('monitors') - pull_resources('screens', Models::Screen, @ids) if should_pull?('screens') + if @ids_or_names.empty? + pull_resources('dashboards', Models::Dashboard) + pull_resources('monitors', Models::Monitor) + pull_resources('screens', Models::Screen) + return + end - Doggy.ui.say "Nothing to pull: please specify object ID or use '-a' to pull everything" unless @updated_by_last_action + @ids_or_names.each do |id_or_name| + @local_resources = Doggy::Model.all_local_resources + if id_or_name =~ /^\d+$/ + pull_by_id(id_or_name.to_i) + else + pull_by_file(id_or_name) + end + end end private - def should_pull?(resource) - !@options.empty? || @options[resource] || @ids.any? - end + def pull_by_id(id) + local_resource = @local_resources.find { |l| l.id == id } + if !local_resource + remote_resource = [Models::Dashboard, Models::Monitor, Models::Screen].map do |klass| + klass.find(id) + end.compact.first - def pull_resources(name, klass, ids) - if ids.any? - Doggy.ui.say "Pulling #{ name }: #{ids.join(', ')}" - remote_resources = klass.all.find_all { |m| ids.include?(m.id.to_s) } + remote_resource.save_local else - Doggy.ui.say "Pulling #{ name }" - remote_resources = klass.all + remote_resource = local_resource.class.find(local_resource.id) + remote_resource.path = local_resource.path + remote_resource.save_local end + end + + def pull_by_file(file) + resolved_path = Doggy.resolve_path(file) + local_resource = @local_resources.find { |l| l.path == resolved_path } + + remote_resource = local_resource.class.find(local_resource.id) + remote_resource.path = local_resource.path + remote_resource.save_local + end + + def pull_resources(name, klass) + Doggy.ui.say "Pulling #{ name }..." local_resources = klass.all_local + remote_resources = klass.all + klass.assign_paths(remote_resources, local_resources) - @updated_by_last_action = true remote_resources.each(&:save_local) end end end