Module: AstroboaCLI::Command

Extended by:
Util
Defined in:
lib/astroboa-cli/command.rb,
lib/astroboa-cli/command/base.rb,
lib/astroboa-cli/command/version.rb

Defined Under Namespace

Classes: Base, CommandFailed, Help, Model, Repository, Server, Service, Version

Class Method Summary collapse

Methods included from Util

ask, astroboa_running?, check_if_running_with_sudo, create_postgresql_db, delete_file_content_between_regex, delete_file_lines, dir_writable?, display, drop_postgresql_db, error, extract_archive_command, fail, format_with_bang, gem_available?, get_password, get_postgresql_config, get_server_conf_file, get_server_configuration, has_executable, has_executable_with_version, has_version_in_grep, jruby_ok?, jruby_version_ok?, linux?, load_pg_library, longest, mac_os_x?, output_with_bang, postgres_connectivity?, process_os_command, render_template_to_file, repository?, repository_in_repos_config?, repository_in_server_config?, ruby_ok?, ruby_version_ok?, running_with_sudo?, runs_with_jruby?, save_server_configuration, shell, strip_text_nodes, unzip_file, windows?, write_xml

Class Method Details

.commandsObject



62
63
64
# File 'lib/astroboa-cli/command.rb', line 62

def self.commands
  @@commands ||= {}
end

.current_argsObject



70
71
72
# File 'lib/astroboa-cli/command.rb', line 70

def self.current_args
  @current_args
end

.current_commandObject



66
67
68
# File 'lib/astroboa-cli/command.rb', line 66

def self.current_command
  @current_command
end

.current_optionsObject



74
75
76
# File 'lib/astroboa-cli/command.rb', line 74

def self.current_options
  @current_options
end

.global_option(name, *args) ⇒ Object



82
83
84
# File 'lib/astroboa-cli/command.rb', line 82

def self.global_option(name, *args)
  global_options << { :name => name, :args => args }
end

.global_optionsObject



78
79
80
# File 'lib/astroboa-cli/command.rb', line 78

def self.global_options
  @global_options ||= []
end

.loadObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/astroboa-cli/command.rb', line 39

def self.load
  Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do |file|
    require file
  end
  
  @@log_file = '/tmp/astroboa-cli-log.txt'
  f = File.new(@@log_file, 'w+', 0644)
  @@log = Logger.new(f)
  @@log.level = Logger::INFO
end

.logObject



50
51
52
# File 'lib/astroboa-cli/command.rb', line 50

def self.log
  @@log
end

.log_fileObject



54
55
56
# File 'lib/astroboa-cli/command.rb', line 54

def self.log_file
  @@log_file
end

.map_command_to_method(cmd, args = []) ⇒ Object

Raises:

  • (OptionParser::ParseError)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/astroboa-cli/command.rb', line 96

def self.map_command_to_method(cmd, args=[])
  command = commands[cmd]

  unless command
    if %w( -v --version ).include?(cmd)
      display AstroboaCLI::VERSION
      exit
    end

    output_with_bang("`#{cmd}` is not an astroboa command.")

    output_with_bang("run `astroboa-cli help` to see available commands.")
    exit 1
  end

  @current_command = cmd

  opts = {}
  invalid_options = []

  parser = OptionParser.new do |parser|
    global_options.each do |global_option|
      parser.on(*global_option[:args]) do |value|
        opts[global_option[:name]] = value
      end
    end
    command[:options].each do |name, option|
      parser.on("-#{option[:short]}", "--#{option[:long]}", option[:desc]) do |value|
        opts[name.gsub("-", "_").to_sym] = value
      end
    end
  end

  begin
    parser.order!(args) do |nonopt|
      invalid_options << nonopt
    end
  rescue OptionParser::InvalidOption => ex
    invalid_options << ex.args.first
    retry
  end

  raise OptionParser::ParseError if opts[:help]

  args.concat(invalid_options)

  @current_args = args
  @current_options = opts

  [ command[:klass].new(args.dup, opts.dup), command[:method] ]
end

.namespacesObject



58
59
60
# File 'lib/astroboa-cli/command.rb', line 58

def self.namespaces
  @@namespaces ||= {}
end

.register_command(command) ⇒ Object



88
89
90
# File 'lib/astroboa-cli/command.rb', line 88

def self.register_command(command)
  commands[command[:command]] = command
end

.register_namespace(namespace) ⇒ Object



92
93
94
# File 'lib/astroboa-cli/command.rb', line 92

def self.register_namespace(namespace)
  namespaces[namespace[:name]] = namespace
end

.run(command, arguments = []) ⇒ Object



149
150
151
152
153
154
# File 'lib/astroboa-cli/command.rb', line 149

def self.run(command, arguments=[])
  object, method = map_command_to_method(command, arguments.dup)
  object.send(method)
rescue => e
  error "An error has occured \n#{e.inspect}\n#{e.backtrace}"
end