Sha256: d0a4d48f2f962c2966390832080fdea128385e8957bf043c3cbb8f89aed21365

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

require 'optparse'
require 'json'

module Vcloud
  module Walker
    class Cli
      def initialize(argv_array)
        @usage_text = nil
        @resource_type = nil
        @options = {
          :yaml => false,
        }

        parse(argv_array)
      end

      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

      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

      def exit_error_usage(error)
        $stderr.puts "#{$0}: #{error}"
        $stderr.puts @usage_text
        exit 2
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vcloud-walker-5.1.0 lib/vcloud/walker/cli.rb
vcloud-walker-5.0.0 lib/vcloud/walker/cli.rb
vcloud-walker-4.0.0 lib/vcloud/walker/cli.rb
vcloud-walker-3.6.0 lib/vcloud/walker/cli.rb
vcloud-walker-3.5.0 lib/vcloud/walker/cli.rb
vcloud-walker-3.4.0 lib/vcloud/walker/cli.rb
vcloud-walker-3.3.1 lib/vcloud/walker/cli.rb
vcloud-walker-3.3.0 lib/vcloud/walker/cli.rb
vcloud-walker-3.2.3 lib/vcloud/walker/cli.rb