diff --git a/lib/command.rb b/lib/command.rb index a5496b0..3f5568f 100644 --- a/lib/command.rb +++ b/lib/command.rb @@ -16,6 +16,14 @@ def execute with_target_vms(argv, {:single_target=>true}) do |machine| ip = machine.provider.capability(:public_address) + + if machine.state.id != :running + raise ::VagrantPlugins::CommandAddress::Errors::NotRunning + end + if ip == nil + raise ::VagrantPlugins::CommandAddress::Errors::Unknown + end + message = ENV['TEAMCITY_VERSION'] ? "##teamcity[setParameter name='env.VAGRANT_ADDRESS' value='#{ip}']" : ip @env.ui.info(message) end diff --git a/lib/errors.rb b/lib/errors.rb new file mode 100644 index 0000000..a477eb2 --- /dev/null +++ b/lib/errors.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module CommandAddress + module Errors + class AddressError < Vagrant::Errors::VagrantError + error_namespace("vagrant_address.errors") + end + + class NotRunning < AddressError + error_key(:not_running) + end + class Unknown < AddressError + error_key(:unknown) + end + end + end +end diff --git a/lib/locales/en.yml b/lib/locales/en.yml new file mode 100644 index 0000000..ae4c095 --- /dev/null +++ b/lib/locales/en.yml @@ -0,0 +1,7 @@ +en: + vagrant_address: + errors: + not_running: |- + A machine is not running + unknown: |- + The address is unknown. Refer to https://github.com/mkuzmin/vagrant-address/issues/5 diff --git a/lib/vagrant-address.rb b/lib/vagrant-address.rb index 7919d79..d910f19 100644 --- a/lib/vagrant-address.rb +++ b/lib/vagrant-address.rb @@ -2,6 +2,8 @@ module VagrantPlugins module CommandAddress + autoload :Errors, File.expand_path("../errors", __FILE__) + class Plugin < Vagrant.plugin("2") name "address" description <<-DESC @@ -10,8 +12,19 @@ class Plugin < Vagrant.plugin("2") command("address", primary: false) do require_relative "command" + init! Command end + + protected + + def self.init! + return if defined?(@_init) + I18n.load_path << File.expand_path("../locales/en.yml", __FILE__) + I18n.reload! + @_init = true + end + end end end diff --git a/vagrant-address.gemspec b/vagrant-address.gemspec index ee3e3ef..532db94 100644 --- a/vagrant-address.gemspec +++ b/vagrant-address.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = 'vagrant-address' - spec.version = '0.3' + spec.version = '0.3.1' spec.authors = ['Michael Kuzmin'] spec.email = ['mkuzmin@gmail.com'] spec.summary = 'Vagrant plugin for obtaining IP address of a guest machine'