class Vcloud::Walker::Cli

Public Class Methods

new(argv_array) click to toggle source
# File lib/vcloud/walker/cli.rb, line 7
def initialize(argv_array)
  @usage_text = nil
  @resource_type = nil
  @options = {
    :yaml => false,
  }

  parse(argv_array)
end

Public Instance Methods

run() click to toggle source
# File lib/vcloud/walker/cli.rb, line 17
def run
  begin
    out = Vcloud::Walker.walk(@resource_type)
    if @options[:yaml]
      print(out.to_yaml)
    else
      print(JSON.pretty_generate(out))
    end
  rescue => e
    $stderr.puts(e)
    exit 1
  end
end

Private Instance Methods

exit_error_usage(error) click to toggle source
# File lib/vcloud/walker/cli.rb, line 75
def exit_error_usage(error)
  $stderr.puts "#{$0}: #{error}"
  $stderr.puts @usage_text
  exit 2
end
parse(args) click to toggle source
# File lib/vcloud/walker/cli.rb, line 33
      def parse(args)
        opt_parser = OptionParser.new do |opts|
          opts.banner = <<-EOS
Usage: #{$0} [options] resource_type
Vcloud-walker is a command line tool, to describe different VMware vCloud Director 5.1 resources. It uses Fog under the hood.

Resources that can be walked using vcloud-walker are:
  catalogs
  vdcs
  networks
  edgegateways
  organization

See https://github.com/gds-operations/vcloud-walker for more info
          EOS

          opts.on("--yaml",   "Yaml output") do
            @options[:yaml] = true
          end

          opts.on("-h", "--help", "Print usage and exit") do
            $stderr.puts opts
            exit
          end

          opts.on("--version", "Display version and exit") do
            puts Vcloud::Walker::VERSION
            exit
          end
        end

        @usage_text = opt_parser.to_s
        begin
          opt_parser.parse!(args)
        rescue OptionParser::InvalidOption => e
          exit_error_usage(e)
        end

        exit_error_usage("must supply resource_type") unless args.size == 1
        @resource_type = args.first
      end