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 (100 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download | duplicates (3)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'optparse'

module VagrantPlugins
  module CommandGlobalStatus
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "outputs status Vagrant environments for this user"
      end

      def execute
        options = {}

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant global-status"
          o.separator ""
          o.on("--prune", "Prune invalid entries.") do |p|
            options[:prune] = true
          end
        end

        # Parse the options
        argv = parse_options(opts)
        return if !argv

        columns = [
          ["id", :id],
          ["name", :name],
          ["provider", :provider],
          ["state", :state],
          ["directory", :vagrantfile_path],
        ]

        widths = {}
        widths[:id] = 8
        widths[:name] = 6
        widths[:provider] = 6
        widths[:state] = 6
        widths[:vagrantfile_path] = 35

        entries = []
        prune   = []
        @env.machine_index.each do |entry|
          # If we're pruning and this entry is invalid, skip it
          # and prune it later.
          if options[:prune] && !entry.valid?(@env.home_path)
            prune << entry
            next
          end

          entries << entry

          columns.each do |_, method|
            # Skip the id
            next if method == :id

            widths[method] ||= 0
            cur = entry.send(method).to_s.length
            widths[method] = cur if cur > widths[method]
          end
        end

        # Prune all the entries to prune
        prune.each do |entry|
          deletable = @env.machine_index.get(entry.id)
          @env.machine_index.delete(deletable) if deletable
        end

        total_width = 0
        columns.each do |header, method|
          header = header.ljust(widths[method]) if widths[method]
          @env.ui.info("#{header} ", new_line: false)
          total_width += header.length + 1
        end
        @env.ui.info("")
        @env.ui.info("-" * total_width)

        if entries.empty?
          @env.ui.info(I18n.t("vagrant.global_status_none"))
          return 0
        end

        entries.each do |entry|
          columns.each do |_, method|
            v = entry.send(method).to_s
            v = v[0...7] if method == :id
            v = v.ljust(widths[method]) if widths[method]
            @env.ui.info("#{v} ", new_line: false)
          end

          @env.ui.info("")
        end

        @env.ui.info(" \n" + I18n.t("vagrant.global_status_footer"))

        # Success, exit status 0
        0
      end
    end
  end
end