summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPieter van de Bruggen <pieter@puppetlabs.com>2011-03-25 08:59:29 -0700
committerPieter van de Bruggen <pieter@puppetlabs.com>2011-03-25 08:59:29 -0700
commit4c320cc482a33892a688d3a5072081e6d63a8310 (patch)
tree1ca02494756c402321e84e9df2f327beb0b78bcb /lib
parent53b0656048c3227048bdc317c5e917ad0c39e850 (diff)
parent6aea116701b8e03558ef7a5a15766b267af14281 (diff)
downloadpuppet-4c320cc482a33892a688d3a5072081e6d63a8310.tar.gz
puppet-4c320cc482a33892a688d3a5072081e6d63a8310.tar.xz
puppet-4c320cc482a33892a688d3a5072081e6d63a8310.zip
Merge branch 'tickets/master/6770'
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/application/interface_base.rb6
-rw-r--r--lib/puppet/interface.rb23
-rw-r--r--lib/puppet/interface/interface_collection.rb50
3 files changed, 63 insertions, 16 deletions
diff --git a/lib/puppet/application/interface_base.rb b/lib/puppet/application/interface_base.rb
index c1c02040a..841f3ca12 100644
--- a/lib/puppet/application/interface_base.rb
+++ b/lib/puppet/application/interface_base.rb
@@ -72,10 +72,10 @@ class Puppet::Application::InterfaceBase < Puppet::Application
@type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
# TODO: These should be configurable versions.
- unless Puppet::Interface.interface?(@type, '0.0.1')
- raise "Could not find version #{1} of interface '#{@type}'"
+ unless Puppet::Interface.interface?(@type, :latest)
+ raise "Could not find any version of interface '#{@type}'"
end
- @interface = Puppet::Interface[@type, '0.0.1']
+ @interface = Puppet::Interface[@type, :latest]
@format ||= @interface.default_format
# We copy all of the app options to the interface.
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
index 64f1bfe49..a667c6b75 100644
--- a/lib/puppet/interface.rb
+++ b/lib/puppet/interface.rb
@@ -30,15 +30,17 @@ class Puppet::Interface
Puppet::Interface::InterfaceCollection.register(instance)
end
- def define(name, version, &blk)
+ def define(name, version, &block)
if interface?(name, version)
interface = Puppet::Interface::InterfaceCollection[name, version]
- interface.instance_eval(&blk) if blk
else
- interface = self.new(name, :version => version, &blk)
+ interface = self.new(name, version)
Puppet::Interface::InterfaceCollection.register(interface)
interface.load_actions
end
+
+ interface.instance_eval(&block) if block_given?
+
return interface
end
@@ -54,22 +56,21 @@ class Puppet::Interface
attr_accessor :type, :verb, :version, :arguments, :options
attr_reader :name
- def initialize(name, options = {}, &block)
- unless options[:version]
- raise ArgumentError, "Interface #{name} declared without version!"
+ def initialize(name, version, &block)
+ unless Puppet::Interface::InterfaceCollection.validate_version(version)
+ raise ArgumentError, "Cannot create interface with invalid version number '#{version}'!"
end
@name = Puppet::Interface::InterfaceCollection.underscorize(name)
-
+ @version = version
@default_format = :pson
- options.each { |opt, val| send(opt.to_s + "=", val) }
- instance_eval(&block) if block
+ instance_eval(&block) if block_given?
end
# Try to find actions defined in other files.
def load_actions
- path = "puppet/interface/#{name}"
+ path = "puppet/interface/v#{version}/#{name}"
loaded = []
Puppet::Interface.autoloader.search_directories.each do |dir|
@@ -92,6 +93,6 @@ class Puppet::Interface
end
def to_s
- "Puppet::Interface(#{name}, :version => #{version.inspect})"
+ "Puppet::Interface[#{name.inspect}, #{version.inspect}]"
end
end
diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb
index d626c4f72..92e2933fe 100644
--- a/lib/puppet/interface/interface_collection.rb
+++ b/lib/puppet/interface/interface_collection.rb
@@ -1,6 +1,8 @@
require 'puppet/interface'
module Puppet::Interface::InterfaceCollection
+ SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/
+
@interfaces = Hash.new { |hash, key| hash[key] = {} }
def self.interfaces
@@ -9,7 +11,7 @@ module Puppet::Interface::InterfaceCollection
$LOAD_PATH.each do |dir|
next unless FileTest.directory?(dir)
Dir.chdir(dir) do
- Dir.glob("puppet/interface/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file|
+ Dir.glob("puppet/interface/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file|
iname = file.sub(/\.rb/, '')
begin
require iname
@@ -24,12 +26,56 @@ module Puppet::Interface::InterfaceCollection
return @interfaces.keys
end
+ def self.versions(name)
+ versions = []
+ $LOAD_PATH.each do |dir|
+ next unless FileTest.directory?(dir)
+ v_dir = File.join dir, %w[puppet interface v*]
+ Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f|
+ v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1')
+ if validate_version(v)
+ versions << v
+ else
+ warn "'#{v}' (#{f}) is not a valid version string; skipping"
+ end
+ end
+ end
+ return versions.uniq.sort { |a, b| compare_versions(a, b) }
+ end
+
+ def self.validate_version(version)
+ !!(SEMVER_VERSION =~ version.to_s)
+ end
+
+ def self.compare_versions(a, b)
+ a, b = [a, b].map do |x|
+ parts = SEMVER_VERSION.match(x).to_a[1..4]
+ parts[0..2] = parts[0..2].map { |e| e.to_i }
+ parts
+ end
+
+ cmp = a[0..2] <=> b[0..2]
+ if cmp == 0
+ cmp = a[3] <=> b[3]
+ cmp = +1 if a[3].empty? && !b[3].empty?
+ cmp = -1 if b[3].empty? && !a[3].empty?
+ end
+ cmp
+ end
+
def self.[](name, version)
- @interfaces[underscorize(name)][version] if interface?(name, version)
+ version = versions(name).last if version == :latest
+ unless version.nil?
+ @interfaces[underscorize(name)][version] if interface?(name, version)
+ end
end
def self.interface?(name, version)
+ version = versions(name).last if version == :latest
+ return false if version.nil?
+
name = underscorize(name)
+
unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version)
require "puppet/interface/v#{version}/#{name}"
end