Class: Enviera::Subcommand

Inherits:
Object
  • Object
show all
Defined in:
lib/enviera/subcommand.rb

Constant Summary collapse

@@global_options =
[
 {:name          => :version,
    :description => "Show version information"},
 {:name          => :verbose,
    :description => "Be more verbose",
    :short       => 'v'},
 {:name          => :help,
    :description => "Information on how to use this command",
    :short       => 'h'}
]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.global_optionsObject

Returns the value of attribute global_options.



10
11
12
# File 'lib/enviera/subcommand.rb', line 10

def global_options
  @global_options
end

.helptextObject

Returns the value of attribute helptext.



10
11
12
# File 'lib/enviera/subcommand.rb', line 10

def helptext
  @helptext
end

.optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/enviera/subcommand.rb', line 10

def options
  @options
end

Class Method Details

.all_optionsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/enviera/subcommand.rb', line 38

def self.all_options
  options = @@global_options.dup
  options += self.options if self.options
  # merge in defaults from configuration files
  config_file = self.load_config_file
  options.map!{ | opt|
    key_name = "#{opt[:name]}"
    if config_file.has_key? key_name
      opt[:default] = config_file[key_name]
      opt
    else
      opt
    end
  }
  options
end

.attach_option(opt) ⇒ Object



55
56
57
# File 'lib/enviera/subcommand.rb', line 55

def self.attach_option opt
  self.suboptions += opt
end

.descriptionObject



114
115
116
# File 'lib/enviera/subcommand.rb', line 114

def self.description
  "no description"
end

.executeObject

Raises:

  • (StandardError)


122
123
124
# File 'lib/enviera/subcommand.rb', line 122

def self.execute
  raise StandardError, "This command is not implemented yet (#{self.to_s.split('::').last})"
end

.find(commandname = "unknown_command") ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/enviera/subcommand.rb', line 59

def self.find commandname = "unknown_command"
  begin
    require "enviera/subcommands/#{commandname.downcase}"
  rescue Exception => e
    require "enviera/subcommands/unknown_command"
    return Enviera::Subcommands::UnknownCommand
  end
  command_module = Module.const_get('Enviera').const_get('Subcommands')
  command_class = Utils.find_closest_class :parent_class => command_module, :class_name => commandname
  command_class || Enviera::Subcommands::UnknownCommand
end

.hidden?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/enviera/subcommand.rb', line 130

def self.hidden?
  false
end

.load_config_fileObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/enviera/subcommand.rb', line 24

def self.load_config_file
  config = {}
  [ "/etc/enviera/config.yaml", "#{ENV['HOME']}/.enviera/config.yaml", "#{ENV['ENVIERA_CONFIG']}" ].each do |config_file|
    begin
      yaml_contents = YAML.load_file(config_file)
      Utils::info "Loaded config from #{config_file}"
      config.merge! yaml_contents
    rescue
      raise StandardError, "Could not open config file \"#{config_file}\" for reading"
    end if config_file and File.file? config_file
  end
  config
end

.parseObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/enviera/subcommand.rb', line 71

def self.parse

  me = self

  options = Trollop::options do

    version "Enviera version " + Enviera::VERSION.to_s
    banner ["enviera #{me.prettyname}: #{me.description}", me.helptext, "Options:"].compact.join("\n\n")

    me.all_options.each do |available_option|

      skeleton = {:description => "",
                  :short => :none}

      skeleton.merge! available_option
      opt skeleton[:name],
          skeleton[:desc] || skeleton[:description],  #legacy plugins
          :short => skeleton[:short],
          :default => skeleton[:default],
          :type => skeleton[:type]

    end

    stop_on Enviera.subcommands

  end

  if options[:verbose]
    Enviera.verbosity_level += 1
  end

  if options[:quiet]
    Enviera.verbosity_level = 0
  end

  options

end

.prettynameObject



126
127
128
# File 'lib/enviera/subcommand.rb', line 126

def self.prettyname
  Utils.snakecase self.to_s.split('::').last
end

.validate(args) ⇒ Object



110
111
112
# File 'lib/enviera/subcommand.rb', line 110

def self.validate args
  args
end