Sha256: aa66687fac235476ffb12c039dedf0b3254f47261946e164769a41bf5b7fc97c

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

require 'vagrant-deltacloud-provider/client/deltacloud'

module VagrantPlugins
  module Deltacloud
    module Command
      class AbstractCommand < Vagrant.plugin('2', :command)
        def initialize(argv, env)
          @env = env
          super(normalize_args(argv), env)
        end

        def execute(name)
          env = {}
          with_target_vms(nil, provider: :deltacloud) do |machine|
            env[:deltacloud_client] = Deltacloud::DeltacloudClient.instance
            env[:machine] = machine
            env[:ui] = @env.ui
          end

          cmd(name, @argv, env)
          @env.ui.info('')
        # rubocop:disable Lint/RescueException
        rescue Errors::VagrantDeltacloudError => e
          raise e
        rescue Exception => e
          puts I18n.t('vagrant_deltacloud.global_error').red unless e.message && e.message.start_with?('Catched Error:')
          raise e
        end
        # rubocop:enable Lint/RescueException

        #
        # Before Vagrant 1.5, args list ends with an extra arg '--'. It removes it if present.
        #
        def normalize_args(args)
          return args if args.nil?
          args.pop if args.size > 0 && args.last == '--'
          args
        end

        def cmd(_name, _argv, _env)
          fail 'Command not implemented. \'cmd\' method must be overridden in all subclasses'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vagrant-deltacloud-provider-0.0.5 lib/vagrant-deltacloud-provider/command/abstract_command.rb
vagrant-deltacloud-provider-0.0.4 lib/vagrant-deltacloud-provider/command/abstract_command.rb
vagrant-deltacloud-provider-0.0.3 lib/vagrant-deltacloud-provider/command/abstract_command.rb
vagrant-deltacloud-provider-0.0.2 lib/vagrant-deltacloud-provider/command/abstract_command.rb