]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
improve `leap inspect`
authorelijah <elijah@riseup.net>
Sat, 22 Jun 2013 22:52:48 +0000 (15:52 -0700)
committerelijah <elijah@riseup.net>
Sat, 22 Jun 2013 22:52:48 +0000 (15:52 -0700)
lib/core_ext/json.rb
lib/leap_cli/commands/inspect.rb
lib/leap_cli/config/manager.rb
lib/leap_cli/config/node.rb
lib/leap_cli/config/object.rb

index 3b08a045f61f6a9ea7ef0e74b3b534738e8ee5ce..1a82bd9507d37ec4270e19e80558b23a64a156a5 100644 (file)
@@ -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
 
index 0c4035651f049418f22d3993153b4f3157708d29..23c75bb790f91d0b9090950aaefd9f70c00b3b98 100644 (file)
@@ -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)
index 8ab8e2fc9473b979bdfd15f5943c9bfaf0c76dcb..29721e7c9ae87a9a69a434f7e1d9fb474fea6a8c 100644 (file)
@@ -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)
index 15a2d3dda315438f4cea69c394405963d991663b..5b911bf28a529894b27273261333ddb576370a65 100644 (file)
@@ -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
     #
index 1edef3f0a4630a3e22b3395bbdc6a70188d802dc..00997b1f0d15a5fbc273f4d18da168841194cc73 100644 (file)
@@ -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
 
       #