Sha256: ef1dffa78989d0ca5090c0ea43797347a22e3595bac276bd5f80bcf67a9e0650

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

require 'optparse'
require 'highline'

module Vcloud
  module Core
    class LoginCli
      def initialize(argv_array)
        @usage_text = nil

        parse(argv_array)
      end

      def run
        begin
          pass = read_pass
          puts Vcloud::Core::Fog::Login.token_export(pass)
        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]

Utility for obtaining a Fog vCloud Director session token. It will output a
shell `export` command that can be consumed with:

    eval $(FOG_CREDENTIAL=example #{$0})

It requires a Fog credentials file (e.g. `~/.fog`) with the host and user
set, but the password set to an empty string. The password can either be
entered interactively or piped in, for example from an environment variable:

    printenv PASSWORD_VAR | FOG_CREDENTIAL=example #{$0}

          EOS

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

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

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

        if args.size > 0
          exit_error_usage("too many arguments")
        elsif args.size == 1
          @type = args.first
        end
      end

      def read_pass
        hl = HighLine.new($stdin, $stderr)
        if STDIN.tty?
          hl.ask("vCloud password: ") { |q| q.echo = "*" }
        else
          hl.ask("Reading password from pipe..")
        end
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vcloud-core-0.13.0 lib/vcloud/core/login_cli.rb
vcloud-core-0.12.0 lib/vcloud/core/login_cli.rb
vcloud-core-0.11.0 lib/vcloud/core/login_cli.rb
vcloud-core-0.10.0 lib/vcloud/core/login_cli.rb
vcloud-core-0.9.0 lib/vcloud/core/login_cli.rb
vcloud-core-0.8.0 lib/vcloud/core/login_cli.rb