Sha256: d50e23416676e7e8af2a4590149589e46895c50c56e89f34bdaeaf19229eb4f9

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

require 'yaml'
require 'thor'
require 'thor/group'
require 'solusvm/version'

module Solusvm
  class BaseCli < Thor
    include Thor::Actions

    # Retrieves default options coming from a configuration file, if any.
    def self.default_option(key)
      @@yaml ||= begin
        file = File.join(File.expand_path(ENV['HOME']), '.solusvm.yml')
        if File.exists?(file)
          YAML::load(File.open(file))
        else
          {}
        end
      end

      @@yaml[key.to_s]
    end

    # Convenience method to get the namespace from the class name. It's the
    # same as Thor default except that the "_cli" at the end of the class
    # is removed.
    def self.namespace(name=nil)
      return super if name
      @namespace ||= super.sub(/_cli$/, '')
    end
    
    class_option :api_login, :type => :string, :desc => "API ID; Required.",  :aliases => ["-I", "--api-login"], :default => default_option(:id)
    class_option :api_key,   :type => :string, :desc => "API KEY; Required.", :aliases => ["-K", "--api-key"], :default => default_option(:key)
    class_option :api_url,   :type => :string, :desc => "API URL; Required.", :aliases => ["-U", "--api-url"], :default => default_option(:url)

    # Overrides the default banner implementation to output the whole command
    def self.banner(task, namespace = true, subcommand = false)
      "#{self.namespace.split(":").join(" ")} #{task.formatted_usage(self, false, false)}"
    end

    protected

    def configure
      Solusvm.config(options[:api_login], options[:api_key], :url => options[:api_url])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solusvm-1.0.0.beta2 lib/solusvm/cli/base_cli.rb
solusvm-1.0.0.beta lib/solusvm/cli/base_cli.rb