From 0ffdf42cf2171deb0bdd4c54f8281c1c1257ee86 Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 22 Jun 2013 15:52:48 -0700 Subject: improve `leap inspect` --- lib/core_ext/json.rb | 13 ++++++++----- lib/leap_cli/commands/inspect.rb | 39 ++++++++++++++++++++++++++++++--------- lib/leap_cli/config/manager.rb | 9 +++++---- lib/leap_cli/config/node.rb | 15 --------------- lib/leap_cli/config/object.rb | 15 +++++++++++++++ 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/lib/core_ext/json.rb b/lib/core_ext/json.rb index 3b08a04..1a82bd9 100644 --- a/lib/core_ext/json.rb +++ b/lib/core_ext/json.rb @@ -12,18 +12,21 @@ module JSON # def self.sorted_generate(obj) # modify hash and array - Hash.class_eval do + Array.class_eval do alias_method :each_without_sort, :each def each(&block) - keys.sort {|a,b| a.to_s <=> b.to_s }.each do |key| - yield key, self[key] + sorted = sort {|a,b| a.to_s <=> b.to_s } + for i in 0..(sorted.length-1) do + yield sorted[i] end end end - Array.class_eval do + Hash.class_eval do alias_method :each_without_sort, :each def each(&block) - sort {|a,b| a.to_s <=> b.to_s }.each_without_sort &block + self.keys.each do |key| + yield key, self.fetch(key) # fetch is used so we don't trigger Config::Object auto-eval + end end end diff --git a/lib/leap_cli/commands/inspect.rb b/lib/leap_cli/commands/inspect.rb index 0c40356..23c75bb 100644 --- a/lib/leap_cli/commands/inspect.rb +++ b/lib/leap_cli/commands/inspect.rb @@ -3,6 +3,7 @@ module LeapCli; module Commands desc 'Prints details about a file. Alternately, the argument FILE can be the name of a node, service or tag.' arg_name 'FILE' command :inspect do |c| + c.switch 'base', :desc => 'Inspect the FILE from the provider_base (i.e. without local inheritance).', :negatable => false c.action do |global_options,options,args| object = args.first assert! object, 'A file path or node/service/tag name is required' @@ -42,6 +43,8 @@ module LeapCli; module Commands :inspect_provider elsif path_match?(:common_config, full_path) :inspect_common + else + nil end end elsif manager.nodes[object] @@ -83,33 +86,51 @@ module LeapCli; module Commands #end def inspect_node(arg, options) - inspect_json(arg, options) {|name| manager.nodes[name] } + inspect_json manager.nodes[name(arg)] end def inspect_service(arg, options) - inspect_json(arg, options) {|name| manager.services[name] } + if options[:base] + inspect_json manager.base_services[name(arg)] + else + inspect_json manager.services[name(arg)] + end end def inspect_tag(arg, options) - inspect_json(arg, options) {|name| manager.tags[name] } + if options[:base] + inspect_json manager.base_tags[name(arg)] + else + inspect_json manager.tags[name(arg)] + end end def inspect_provider(arg, options) - inspect_json(arg, options) {|name| manager.provider } + if options[:base] + inspect_json manager.base_provider + else + inspect_json manager.provider + end end def inspect_common(arg, options) - inspect_json(arg, options) {|name| manager.common } + if options[:base] + inspect_json manager.base_common + else + inspect_json manager.common + end end # # helpers # - def inspect_json(arg, options) - name = File.basename(arg).sub(/\.json$/, '') - config = yield name - puts config.dump_json + def name(arg) + File.basename(arg).sub(/\.json$/, '') + end + + def inspect_json(config) + puts JSON.sorted_generate(config) end def path_match?(path_symbol, path) diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 8ab8e2f..29721e7 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -17,6 +17,7 @@ module LeapCli ## attr_reader :services, :tags, :nodes, :provider, :common, :secrets + attr_reader :base_services, :base_tags, :base_provider, :base_common def facts @facts ||= JSON.parse(Util.read_file(:facts) || "{}") @@ -33,10 +34,10 @@ module LeapCli @provider_dir = Path.provider # load base - base_services = load_all_json(Path.named_path([:service_config, '*'], Path.provider_base), Config::Tag) - base_tags = load_all_json(Path.named_path([:tag_config, '*'], Path.provider_base), Config::Tag) - base_common = load_json(Path.named_path(:common_config, Path.provider_base), Config::Object) - base_provider = load_json(Path.named_path(:provider_config, Path.provider_base), Config::Object) + @base_services = load_all_json(Path.named_path([:service_config, '*'], Path.provider_base), Config::Tag) + @base_tags = load_all_json(Path.named_path([:tag_config, '*'], Path.provider_base), Config::Tag) + @base_common = load_json(Path.named_path(:common_config, Path.provider_base), Config::Object) + @base_provider = load_json(Path.named_path(:provider_config, Path.provider_base), Config::Object) # load provider provider_path = Path.named_path(:provider_config, @provider_dir) diff --git a/lib/leap_cli/config/node.rb b/lib/leap_cli/config/node.rb index 15a2d3d..5b911bf 100644 --- a/lib/leap_cli/config/node.rb +++ b/lib/leap_cli/config/node.rb @@ -15,21 +15,6 @@ module LeapCli; module Config @file_paths = [] end - # - # Make a copy of ourselves, except only including the specified keys. - # - # Also, the result is flattened to a single hash, so a key of 'a.b' becomes 'a_b' - # - def pick(*keys) - keys.map(&:to_s).inject(self.class.new(@manager)) do |hsh, key| - value = self.get(key) - if !value.nil? - hsh[key.gsub('.','_')] = value - end - hsh - end - end - # # returns true if this node has an ip address in the range of the vagrant network # diff --git a/lib/leap_cli/config/object.rb b/lib/leap_cli/config/object.rb index 1edef3f..00997b1 100644 --- a/lib/leap_cli/config/object.rb +++ b/lib/leap_cli/config/object.rb @@ -184,6 +184,21 @@ module LeapCli self.deep_merge!(object, true) end + # + # Make a copy of ourselves, except only including the specified keys. + # + # Also, the result is flattened to a single hash, so a key of 'a.b' becomes 'a_b' + # + def pick(*keys) + keys.map(&:to_s).inject(self.class.new(@manager)) do |hsh, key| + value = self.get(key) + if !value.nil? + hsh[key.gsub('.','_')] = value + end + hsh + end + end + protected # -- cgit v1.2.3