Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuzmin committed Dec 26, 2015
1 parent 4cb6961 commit e1b2635
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/errors.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/vagrant-address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module VagrantPlugins
module CommandAddress
autoload :Errors, File.expand_path("../errors", __FILE__)

class Plugin < Vagrant.plugin("2")
name "address"
description <<-DESC
Expand All @@ -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
2 changes: 1 addition & 1 deletion vagrant-address.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']
spec.summary = 'Vagrant plugin for obtaining IP address of a guest machine'
Expand Down

0 comments on commit e1b2635

Please sign in to comment.