File: command.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (75 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'optparse'

module VagrantPlugins
  module CommandCap
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "checks and executes capability"
      end

      def execute
        options = {}
        options[:check] = false

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant cap [options] TYPE NAME [args]"
          o.separator ""
          o.separator "This is an advanced command. If you don't know what this"
          o.separator "does and you aren't explicitly trying to use it, you probably"
          o.separator "don't want to use this."
          o.separator ""
          o.separator "This command checks or executes arbitrary capabilities that"
          o.separator "Vagrant has for hosts, guests, and providers."
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("--check", "Only checks for a capability, does not execute") do |f|
            options[:check] = f
          end
        end

        # Parse the options
        argv = parse_options(opts)
        return if !argv
        if argv.length < 2
          raise Vagrant::Errors::CLIInvalidUsage,
            help: opts.help.chomp
        end

        type = argv.shift.to_sym
        name = argv.shift.to_sym

        # Get the proper capability host to check
        cap_host = nil
        if type == :host
          cap_host = @env.host
        else
          with_target_vms([]) do |vm|
            cap_host = case type
                       when :provider
                         vm.provider
                       when :guest
                         vm.guest
                       else
                         raise Vagrant::Errors::CLIInvalidUsage,
                           help: opts.help.chomp
                       end
          end
        end

        # If we're just checking, then just return exit codes
        if options[:check]
          return 0 if cap_host.capability?(name)
          return 1
        end

        # Otherwise, call it
        cap_host.capability(name, *argv)

        # Success, exit status 0
        0
      end
    end
  end
end