Sha256: 2682ebd2c552913d54a4e2ff2f0bff347b3358aa58221f63ae81cc629e2e40f8

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'cloudstack_client/client'
require 'connection_helper'
require 'thor'
require 'yaml'

module CloudstackClient
  class CommandGenerator < Thor
    include Thor::Actions

    class_option :config_file,
      default: File.join(Dir.home, '.cloudstack-cli.yml'),
      aliases: '-c',
      desc: 'location of your cloudstack-cli configuration file'

    class_option :env,
      aliases: '-e',
      desc: 'environment to use'

    class_option :debug,
      desc: 'enable debug output',
      type: :boolean

    desc "generate", "generate api commands using the Cloudstack API Discovery service"
    def generate
      json = client.send_request('command' => 'listApis')
      commands = json['api'] || []
      commands.each do |command|
        puts "#{command['name']} : #{command['related']}"
      end
    end

    no_commands do  
      def client(opts = {})
        @config ||= load_configuration
        @client ||= CloudstackClient::Connection.new(
          @config[:url],
          @config[:api_key],
          @config[:secret_key],
          {no_commands: true}
        )
      end

      def load_configuration(config_file = options[:config_file], env = options[:env])
        unless File.exists?(config_file)
          say "Configuration file #{config_file} not found.", :red
          say "Please run \'cs environment add\' to create one."
          exit 1
        end

        begin
          config = YAML::load(IO.read(config_file))
        rescue
          say "Can't load configuration from file #{config_file}.", :red
          exit 1
        end
        
        env ||= config[:default]
        if env
          unless config = config[env]
            say "Can't find environment #{env}.", :red
            exit 1
          end
        end

        unless config.key?(:url) && config.key?(:api_key) && config.key?(:secret_key)
          say "The environment #{env || '\'-\''} contains no valid data.", :red
          exit 1
        end
        config
      end

    end # no_commands

	end # class
end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cloudstack_client-0.7.0 lib/cloudstack_client/command_generator.rb
cloudstack_client-0.6.1 lib/cloudstack_client/command_generator.rb